Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Program Updates/Fixes
- //Sep 18, 2007. Total wipe of file. Making second version prototype, which
- //includes an expanded map array type (struct data types), improved funcionality
- //which includes player movement (and yes a player object, and possibly other
- //fixes.
- #include <iostream>
- #include <ctime>
- #include <cmath>
- #include <stack>
- #include <limits>
- #include <stdlib.h>
- #include <curses.h>
- #include "uniform.h"
- #include "uniform.cpp"
- #include "PlayerObject.cpp"
- //Amount of rooms in map
- #define ROOMLOWERLIMIT 5
- #define ROOMUPPERLIMIT 10
- //Array "Map Attributes"
- #define XLENGTH 40
- #define YLENGTH 80
- struct MapArrayType
- {
- bool AreaAccess;
- };
- class MapType
- {
- public:
- //defualt constructor
- MapType();
- //destructor
- ~MapType();
- //Post-Condition...Room is created
- void CreateRoom(int,int);
- //Post-Condition...Random Number is generated and returned as
- //value of function
- int RandomNumberGenerator(int, int);
- //Post-Condition...True or False returned as result
- bool CanRoomBeCreated(int, int);
- //Post-Condition...Cordinates for the new room are plotted
- void FindNextRoom(int &, int &);
- //Post-Condition...A new path within bounds is created
- void CreatePath(int, int);
- //Post-Condition...Prints Out MapArrayType
- void PrintMap();
- //Post - Condition...User input is captured and sent to validation function
- //UserInput Is created, and the user input is stored
- void CaptureInput();
- //Change as of Sep 18
- //Pre-Condition...Capture Input was called and has altered the data member
- //User Input
- //Post-Condition...True/False value is returned to determine if the user
- //Is able to move to selected spot.
- bool IsInputValid(char);
- //Change as of Sep 18
- //Pre-Condition...function IsInputValid has been called and returned a true/false
- //value
- //Post-Condition...either the user moves from one cell to the other, or not and
- //an error message is displayed.
- void ProcessInput(char);
- private:
- int TotalNumberOfRooms;
- int RoomLength;
- int RoomWidth;
- int RoomCounter;
- //Stacks to hold old map movements
- stack<int> StackX;
- stack<int> StackY;
- //User Related Variables
- //Change as of Sep 18
- int UserCurrentXPos;
- int UserCurrentYPos;
- //NEW map Array, based on struct. This "holds" the entire contents of the map.
- MapArrayType MapArray[XLENGTH][YLENGTH];
- };
- //client program
- int main()
- {
- //Declare local variables
- MapType MapGenerator;
- char done = 'n';
- int testvar = 1;
- MapGenerator.CreateRoom(0,0);
- MapGenerator.PrintMap();
- while (done == 'n')
- {
- if (testvar / 100000 == 1)
- {
- clear();
- addstr("Would You Like to Quit Now? ");
- addch('\n');
- addch('\r');
- addstr("Press any but n to quit!");
- refresh();
- //Allow continous input!
- cbreak();
- done = getch();
- }
- MapGenerator.CaptureInput();
- testvar++;
- }
- //Turn off terminal color
- attroff(COLOR_PAIR(1));
- //Refresh output to standard screen!
- refresh();
- //pause the fing screen!!!
- //getch();
- //MAY HAVE TO REMOVE THIS LINE!!!
- //KILL CURSES TERMINAL MANIUPULATION!
- endwin();
- return 0;
- }
- /**************************************************
- UPDATE 11/16/2011
- MapType::MapType defualt constructor
- Changes:
- 1) Put all constants as define macros
- ***************************************************/
- //defualt Constructor
- MapType::MapType()
- {
- //Set all cells in map to walls
- for (int counterx = 0; counterx < XLENGTH; counterx++){
- for (int countery = 0; countery < YLENGTH; countery++)
- {
- MapArray[counterx][countery].AreaAccess = 0;
- }
- }
- }
- MapType::~MapType()
- {
- //Uh...well there aren't any pointers...
- }
- //RandomNumberGenerator!
- int MapType::RandomNumberGenerator(int LowNumber, int HighNumber)
- {
- //Reseed Random Number Generator
- IntitializeSeed();
- return Uniform(LowNumber,HighNumber);
- }
- /**************************************************
- UPDATE 11/16/2011
- MapType::CanRoomBeCreated
- Changes:
- 1) Put all constants as define macros
- ***************************************************/
- bool MapType::CanRoomBeCreated(int XCoord, int YCoord)
- {
- //determine if room exceeds the x and y chords of the room
- if ((RoomWidth + XCoord > XLENGTH) || (RoomLength + YCoord > YLENGTH))
- {
- return false;
- }
- //Determine if this room will colide with another and return false if it does!
- else if (RoomWidth >= RoomLength)
- {
- cout << "crash 1" << endl;
- for (int i = XCoord,countx = 0;countx < RoomWidth;i++,countx++){
- for (int j = YCoord,county = 0; county < RoomLength; j++,county++){
- if (MapArray[i][j].AreaAccess == 1){
- return false;
- }}}
- }
- else if (RoomWidth < RoomLength)
- {
- cout << "crash 2" << endl;
- for (int j = YCoord,county = 0;county < RoomLength;j++,county++){
- for (int i = XCoord,countx = 0; countx < RoomWidth; i++,countx++){
- if (MapArray[i][j].AreaAccess == 1){
- return false;
- }}}
- }
- return true;
- }
- /**************************************************
- UPDATE 11/16/2011
- MapType::`
- Changes:
- 1) Put all constants as define macros
- ***************************************************/
- void MapType::CreateRoom(int XCoord, int YCoord)
- {
- int OldXCoord;
- int OldYCoord;
- //Find Total Number of rooms
- TotalNumberOfRooms = RandomNumberGenerator(ROOMLOWERLIMIT,ROOMUPPERLIMIT);
- while (TotalNumberOfRooms != RoomCounter){
- //Get new chords for new rooms
- RoomLength = RandomNumberGenerator(ROOMLOWERLIMIT,ROOMUPPERLIMIT);
- RoomWidth = RandomNumberGenerator(ROOMLOWERLIMIT,ROOMUPPERLIMIT);
- FindNextRoom(XCoord,YCoord);
- if ((RoomCounter >= 1) && (CanRoomBeCreated(XCoord,YCoord) == true)){
- cout << "MORE THAN ONE ROOM CREATED! " << endl;
- //Create new rooms
- //push new chrds of room onto stacktype
- StackX.push(XCoord);
- StackY.push(YCoord);
- //Make Room
- for (int i = 0; i < RoomWidth; i++){
- for (int j = 0; j < RoomLength; j++){
- MapArray[XCoord+i][YCoord+j].AreaAccess = 1;
- }
- }
- PrintMap();
- //Clean the stream and ask for input
- cin.ignore().get();
- RoomCounter++;
- cout << "create path func called " << endl;
- CreatePath(XCoord,YCoord);
- PrintMap();
- //Clean the stream and ask for input
- cin.ignore().get();
- }
- else if ( (RoomCounter < 1) && (CanRoomBeCreated(XCoord,YCoord) == true)){
- //push Start of room onto stack
- StackX.push(XCoord);
- StackY.push(YCoord);
- //Update as of Sep 18
- //Log Players position on map and set starting point
- UserCurrentXPos = XCoord;
- UserCurrentYPos = YCoord;
- //Make Room
- for (int i = 0;i < RoomWidth;i++){
- for (int j = 0; j < RoomLength; j++){
- MapArray[XCoord+i][YCoord+j].AreaAccess = 1;
- }
- }
- //Place Player On Map Object
- MapArray[XCoord][YCoord].AreaAccess = 2;
- PrintMap();
- cout << "Player placed on map!" << endl;
- //Clean the stream and ask for input
- cin.ignore().get();
- //Incrament Room Counter
- RoomCounter++;
- }
- }
- return;
- }
- //Room Finder Function
- void MapType::FindNextRoom(int &XCoord, int &YCoord)
- {
- //Get Randomly generated chords for next room though Random Number Gen Function
- XCoord = RandomNumberGenerator(0,XLENGTH);
- YCoord = RandomNumberGenerator(0,YLENGTH);
- //Randomize if the cords are the same
- while(XCoord == YCoord)
- YCoord = RandomNumberGenerator(0,80);
- return;
- }
- //Function to draw pathlines between rooms
- void MapType::CreatePath(int XCoord, int YCoord)
- {
- int oldXCoord = StackX.top();
- int oldYCoord = StackY.top();
- cout << "XCoord = " << XCoord << endl;
- cout << "YCoord = " << YCoord << endl;
- cout << "oldXCoord = " << oldXCoord << endl;
- cout << "oldYCoord = " << oldYCoord << endl;
- if ((XCoord == oldXCoord) && (YCoord == oldYCoord))
- {
- return;
- }
- //both NEW x and y chords are greater than old ones
- else if ((XCoord >= oldXCoord) && (YCoord >= oldYCoord))
- {
- cout << "both NEW x and y chords are greater than old ones" << endl;
- //Clean the stream and ask for input
- cin.ignore().get();
- PrintMap();
- for (; XCoord > oldXCoord; XCoord--){
- if (MapArray[XCoord][YCoord].AreaAccess != 2)
- MapArray[XCoord][YCoord].AreaAccess = 1;
- }
- for (; YCoord > oldYCoord; YCoord--){
- if (MapArray[XCoord][YCoord].AreaAccess != 2)
- MapArray[XCoord][YCoord].AreaAccess = 1;
- }
- }
- //both NEW x and y chords are less or equal to then old ones
- else if ((XCoord <= oldXCoord) && (YCoord <= oldYCoord))
- {
- cout << "both NEW x and y chords are less or equal to then old ones" << endl;
- //Clean the stream and ask for input
- cin.ignore().get();
- PrintMap();
- for (; XCoord < oldXCoord; XCoord++){
- if (MapArray[XCoord][YCoord].AreaAccess != 2)
- MapArray[XCoord][YCoord].AreaAccess = 1;
- }
- for (; YCoord < oldYCoord; YCoord++){
- if (MapArray[XCoord][YCoord].AreaAccess != 2)
- MapArray[XCoord][YCoord].AreaAccess = 1;
- }
- }
- //new and old XCoords are the same, new Y chords are greater or equal to old Y
- else if ((XCoord == oldXCoord) && (YCoord >= oldYCoord)){
- cout << "new and old XCoords are the same, new Y chords are greater or equal to old Y " << endl;
- //Clean the stream and ask for input
- cin.ignore().get();
- PrintMap();
- for (; YCoord > oldYCoord; YCoord--)
- if (MapArray[XCoord][YCoord].AreaAccess != 2)
- MapArray[XCoord][YCoord].AreaAccess = 1;
- }
- //XCoords both the same, New Y chord is less than old one
- else if ((XCoord == oldXCoord) && (YCoord < oldYCoord)){
- cout << "XCoords both the same, New Y chord is less than old one" << endl;
- //Clean the stream and ask for input
- cin.ignore().get();
- PrintMap();
- for (; YCoord < oldYCoord; YCoord++)
- if (MapArray[XCoord][YCoord].AreaAccess != 2)
- MapArray[XCoord][YCoord].AreaAccess = 1;
- }
- //Y chords both the same, Old X greater or equal to new one
- else if ((oldYCoord == YCoord) && (XCoord >= oldXCoord))
- {
- cout << "Y chords both the same, Old X greater or equal to new one" << endl;
- //Clean the stream and ask for input
- cin.ignore().get();
- PrintMap();
- for (; XCoord > oldXCoord; XCoord++)
- if (MapArray[XCoord][YCoord].AreaAccess != 2)
- MapArray[XCoord][YCoord].AreaAccess = 1;
- }
- //YCoords both the same, New X chord is less than old one
- else if ((YCoord == oldYCoord) && (XCoord < oldXCoord))
- {
- cout << "YCoords both the same, New X chord is less than old one" << endl;
- //Clean the stream and ask for input
- cin.ignore().get();
- PrintMap();
- for (; XCoord < oldXCoord; XCoord++)
- if (MapArray[XCoord][YCoord].AreaAccess != 2)
- MapArray[XCoord][YCoord].AreaAccess = 1;
- }
- //if newX is less than old X, new Y greater than old y
- else if ((XCoord <= oldXCoord) && (YCoord >= oldYCoord))
- {
- cout << "if newX is less than old X, new Y greater than old y" << endl;
- //Clean the stream and ask for input
- cin.ignore().get();
- PrintMap();
- for (; XCoord < oldXCoord; XCoord++)
- {
- if (MapArray[XCoord][YCoord].AreaAccess != 2)
- MapArray[XCoord][YCoord].AreaAccess = 1;
- }
- for (; YCoord > oldYCoord; YCoord--)
- {
- if (MapArray[XCoord][YCoord].AreaAccess != 2)
- MapArray[XCoord][YCoord].AreaAccess = 1;
- }
- }
- //if newX is greater than than old X, new Y less than than old y
- else if ((XCoord > oldXCoord) && (YCoord < oldYCoord))
- {
- cout << "if newX is greater than than old X, new Y less than than old y" << endl;
- //Clean the stream and ask for input
- cin.ignore().get();
- PrintMap();
- for (XCoord; XCoord > oldXCoord; XCoord--)
- {
- if (MapArray[XCoord][YCoord].AreaAccess != 2)
- MapArray[XCoord][YCoord].AreaAccess = 1;
- }
- for (YCoord; YCoord < oldYCoord; YCoord++)
- {
- if (MapArray[XCoord][YCoord].AreaAccess != 2)
- MapArray[XCoord][YCoord].AreaAccess = 1;
- }
- }
- }
- //PrintMap Function
- void MapType::PrintMap()
- {
- //Clear screen
- //Keep in mind that curses seems to have a problem with
- //Refreshing the terminal screen if we do not send a clear
- //macro to the screen buffer.
- clear();
- //Prepare Terminal for manipulation
- initscr();
- //Check to see if terminal supports color
- if(has_colors() == false)
- {
- endwin();
- printf("Your terminal does not support color \n");
- exit(1);
- }
- else
- start_color();
- //Select pairs of colors, forground and background
- init_pair(1, COLOR_WHITE, COLOR_BLACK);
- //Apply Color Set
- attron(COLOR_PAIR(1));
- //Print out entire map
- for (int counterx = 0; counterx < 40; counterx++)
- {
- for (int countery = 0; countery < 80; countery++)
- {
- //IF PLAYER IS ON SPOT!
- if (MapArray[counterx][countery].AreaAccess == 2)
- //Display Player Icon
- addch('@');
- //If cell is WALL
- else if (MapArray[counterx][countery].AreaAccess == 0)
- addch('#');
- //If Spot Is Open for movement (nothing there)
- else if (MapArray[counterx][countery].AreaAccess == 1)
- //Display Open Cell Icon
- addch('.');
- }
- //Knock logical Cursor down to a new line
- addch('\n');
- //Return logical cursor back to left of screen
- addch('\r');
- }
- //Turn off terminal color
- //attroff(COLOR_PAIR(1));
- //Refresh output to standard screen!
- refresh();
- //pause the fing screen!!!
- //getch();
- //MAY HAVE TO REMOVE THIS LINE!!!
- //KILL CURSES TERMINAL MANIUPULATION!
- //endwin();
- }
- void MapType::CaptureInput()
- {
- //Declare Variable
- char UserInput = ' ';
- //Allow Continious Input
- cbreak();
- //Stop Character Echo
- noecho();
- //Get User Input Key
- UserInput = getch();
- //Find out if function is valid.
- while (IsInputValid(UserInput) == false ){
- clear();
- addstr("\nInput is Invalid. Please try again\n");
- addstr("\nPress a Key to continue\n");
- PrintMap();
- UserInput = getch();
- }
- //Preform Movement
- ProcessInput(UserInput);
- }
- bool MapType::IsInputValid(char UserInput)
- {
- //If User tries to move to the lower left
- if (UserInput == '1')
- {
- if (MapArray[UserCurrentXPos+1][UserCurrentYPos-1].AreaAccess == 0)
- return false;
- else
- return true;
- }
- //If User Tries to move down
- else if (UserInput == '2')
- {
- if (MapArray[UserCurrentXPos+1][UserCurrentYPos].AreaAccess == 0)
- return false;
- else
- return true;
- }
- //If user Tries to move to lower right
- else if (UserInput == '3')
- {
- if (MapArray[UserCurrentXPos+1][UserCurrentYPos+1].AreaAccess == 0)
- return false;
- else
- return true;
- }
- //If user Tries to move to left
- else if (UserInput == '4')
- {
- if (MapArray[UserCurrentXPos][UserCurrentYPos-1].AreaAccess == 0)
- return false;
- else
- return true;
- }
- //If user tries to move right
- else if (UserInput == '6')
- {
- if (MapArray[UserCurrentXPos][UserCurrentYPos+1].AreaAccess == 0)
- return false;
- else
- return true;
- }
- //If user tries to move to upper left
- else if (UserInput == '7')
- {
- if (MapArray[UserCurrentXPos-1][UserCurrentYPos-1].AreaAccess == 0)
- return false;
- else
- return true;
- }
- //If User tries to move up
- else if (UserInput == '8')
- {
- if (MapArray[UserCurrentXPos-1][UserCurrentYPos].AreaAccess == 0)
- return false;
- else
- return true;
- }
- //If User tries to move upper left
- else if (UserInput == '9')
- {
- if (MapArray[UserCurrentXPos-1][UserCurrentYPos+1].AreaAccess == 0)
- return false;
- else
- return true;
- }
- //Movement is invalid!!
- return false;
- }
- //If User tries to move upper right
- void MapType::ProcessInput(char UserInput)
- {
- //If user tries to move to lower left
- if (UserInput == '1'){
- MapArray[UserCurrentXPos+1][UserCurrentYPos-1].AreaAccess = 2;
- MapArray[UserCurrentXPos][UserCurrentYPos].AreaAccess = 1;
- UserCurrentXPos = UserCurrentXPos++;
- UserCurrentYPos = UserCurrentYPos--;
- }
- //If User Tries to move down
- else if (UserInput == '2')
- {
- MapArray[UserCurrentXPos+1][UserCurrentYPos].AreaAccess = 2;
- MapArray[UserCurrentXPos][UserCurrentYPos].AreaAccess = 1;
- UserCurrentXPos = UserCurrentXPos++;
- }
- //If User tries to move to the lower right
- else if (UserInput == '3')
- {
- MapArray[UserCurrentXPos+1][UserCurrentYPos+1].AreaAccess = 2;
- MapArray[UserCurrentXPos][UserCurrentYPos].AreaAccess = 1;
- UserCurrentXPos = UserCurrentXPos++;
- UserCurrentYPos = UserCurrentYPos++;
- }
- //If user tries to move left
- else if (UserInput == '4'){
- MapArray[UserCurrentXPos][UserCurrentYPos-1].AreaAccess = 2;
- MapArray[UserCurrentXPos][UserCurrentYPos].AreaAccess = 1;
- UserCurrentYPos = UserCurrentYPos--;
- }
- //If user tries to move right
- else if (UserInput == '6')
- {
- MapArray[UserCurrentXPos][UserCurrentYPos+1].AreaAccess = 2;
- MapArray[UserCurrentXPos][UserCurrentYPos].AreaAccess = 1;
- UserCurrentYPos = UserCurrentYPos++;
- }
- //If user tries to move upper left
- else if (UserInput == '7')
- {
- MapArray[UserCurrentXPos-1][UserCurrentYPos-1].AreaAccess = 2;
- MapArray[UserCurrentXPos][UserCurrentYPos].AreaAccess = 1;
- UserCurrentXPos = UserCurrentXPos--;
- UserCurrentYPos = UserCurrentYPos--;
- }
- //If user tries to move up
- else if (UserInput == '8')
- {
- MapArray[UserCurrentXPos-1][UserCurrentYPos].AreaAccess = 2;
- MapArray[UserCurrentXPos][UserCurrentYPos].AreaAccess = 1;
- UserCurrentXPos = UserCurrentXPos--;
- }
- //If user tries to move upper right
- else if (UserInput == '9')
- {
- MapArray[UserCurrentXPos-1][UserCurrentYPos+1].AreaAccess = 2;
- MapArray[UserCurrentXPos][UserCurrentYPos].AreaAccess = 1;
- UserCurrentXPos = UserCurrentXPos--;
- UserCurrentYPos = UserCurrentYPos++;
- }
- PrintMap();
- }
Add Comment
Please, Sign In to add comment