Guest User

Untitled

a guest
Apr 24th, 2018
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 15.64 KB | None | 0 0
  1. //Program Updates/Fixes
  2.  
  3. //Sep 18, 2007. Total wipe of file. Making second version prototype, which
  4. //includes an expanded map array type (struct data types), improved funcionality
  5. //which includes player movement (and yes a player object, and possibly other
  6. //fixes.
  7. #include <iostream>
  8. #include <ctime>
  9. #include <cmath>
  10. #include <stdlib.h>
  11. #include <curses.h>
  12. #include "uniform.h"
  13. #include "uniform.cpp"
  14. #include "stacktype.cpp"
  15. #include "PlayerObject.cpp"
  16.  
  17. //Amount of rooms in map
  18. #define ROOMLOWERLIMIT 5
  19. #define ROOMUPPERLIMIT 10
  20. //Array "Map Attributes"
  21. #define XLENGTH 40
  22. #define YLENGTH 80
  23.  
  24. struct MapArrayType
  25. {
  26.     bool        AreaAccess;
  27.     PlayerType  *Player;
  28. };
  29.  
  30. class MapType
  31. {
  32.     public:
  33.  
  34. //defualt constructor  
  35. MapType();
  36. //destructor
  37. ~MapType();
  38. //Post-Condition...Room is created
  39. void    CreateRoom(int,int);
  40. //Post-Condition...Random Number is generated and returned as
  41. //value of function
  42. int     RandomNumberGenerator(int, int);
  43. //Post-Condition...True or False returned as result
  44. bool    CanRoomBeCreated(int, int);
  45. //Post-Condition...Cordinates for the new room are plotted
  46. void    FindNextRoom(int &, int &);
  47. //Post-Condition...A new path within bounds is created
  48. void    CreatePath(int, int);
  49. //Post-Condition...Prints Out MapArrayType
  50. void    PrintMap();
  51.  
  52. //Post - Condition...User input is captured and sent to validation function
  53. //UserInput Is created, and the user input is stored
  54. void    CaptureInput();
  55.  
  56. //Change as of Sep 18
  57. //Pre-Condition...Capture Input was called and has altered the data member
  58. //User Input
  59. //Post-Condition...True/False value is returned to determine if the user
  60. //Is able to move to selected spot.
  61. bool    IsInputValid(char);
  62.  
  63. //Change as of Sep 18
  64. //Pre-Condition...function IsInputValid has been called and returned a true/false
  65. //value
  66. //Post-Condition...either the user moves from one cell to the other, or not and
  67. //an error message is displayed.
  68. void    ProcessInput(char);
  69.  
  70.  
  71. private:
  72.  
  73. int TotalNumberOfRooms;
  74. int RoomLength;
  75. int RoomWidth;
  76. int RoomCounter;
  77.  
  78. //Stacks to hold old map movements
  79. stackType<int> StackX;
  80. stackType<int> StackY;
  81.  
  82. //User Related Variables
  83. //Change as of Sep 18
  84. int UserCurrentXPos;
  85. int UserCurrentYPos;
  86.  
  87.  
  88. //NEW map Array, based on struct. This "holds" the entire contents of the map.
  89. MapArrayType MapArray[XLENGTH][YLENGTH];
  90. };
  91.  
  92. //client program
  93. int main()
  94. {
  95.     //Declare local variables
  96. MapType MapGenerator;
  97.  
  98. char done = 'n';
  99. int  testvar = 1;  
  100. MapGenerator.CreateRoom(0,0);
  101. MapGenerator.PrintMap();
  102.    
  103. while (done == 'n')
  104. {
  105.     if (testvar % 100 == 0)
  106.         {
  107.             clear();           
  108.             addstr("Would You Like to Quit Now? ");
  109.             addch('\n');
  110.             addch('\r');
  111.             addstr("Press any but n to quit!");
  112.             refresh();
  113.             //Allow continous input!
  114.             cbreak();
  115.             done = getch();        
  116.         }
  117. MapGenerator.CaptureInput();
  118. testvar++;
  119. }
  120. //Turn off terminal color
  121. attroff(COLOR_PAIR(1));
  122. //Refresh output to standard screen!
  123. refresh();
  124. //pause the fing screen!!!
  125. //getch();
  126. //MAY HAVE TO REMOVE THIS LINE!!!
  127. //KILL CURSES TERMINAL MANIUPULATION!
  128. endwin();
  129. return 0;
  130. }
  131.  
  132. /**************************************************
  133. UPDATE 11/16/2011
  134. MapType::MapType defualt constructor
  135.  
  136. Changes:
  137.  
  138. 1) Put all constants as define macros
  139. ***************************************************/
  140.  
  141.  
  142. //defualt Constructor
  143. MapType::MapType()
  144. {
  145.  
  146.  
  147.  
  148. //Set all cells in map to walls
  149. for (int counterx = 0; counterx < XLENGTH; counterx++){
  150.     for (int countery = 0; countery < YLENGTH; countery++)
  151.         {
  152.     MapArray[counterx][countery].AreaAccess = 0;
  153.     MapArray[counterx][countery].Player = NULL ;
  154.         }
  155.     }
  156. }
  157.  
  158. MapType::~MapType()
  159. {
  160. //Uh...well there aren't any pointers...
  161. }
  162.  
  163. //RandomNumberGenerator!
  164. int MapType::RandomNumberGenerator(int LowNumber, int HighNumber)
  165. {
  166. //Reseed Random Number Generator
  167. IntitializeSeed();
  168. return Uniform(LowNumber,HighNumber);
  169. }
  170.  
  171. /**************************************************
  172. UPDATE 11/16/2011
  173. MapType::CanRoomBeCreated
  174.  
  175. Changes:
  176.  
  177. 1) Put all constants as define macros
  178. ***************************************************/
  179.  
  180.  
  181. bool MapType::CanRoomBeCreated(int xcoord, int ycoord)
  182. {
  183. //determine if room exceeds the x and y chords of the room
  184. if ((RoomWidth + xcoord > XLENGTH) || (RoomLength + ycoord > YLENGTH))
  185.     {
  186.         return false;
  187.     }
  188. //Determine if this room will colide with another and return false if it does!
  189. else if (RoomWidth >= RoomLength)
  190.     {
  191.                                              for (int i = xcoord,countx = 0;countx < RoomWidth;i++,countx++){
  192.                                              for (int j = ycoord,county = 0; county < RoomLength; j++,county++){
  193.                                              if (MapArray[i][j].AreaAccess == 1){
  194.                                              return false;
  195.                                              }}}
  196.     }
  197. else if (RoomWidth < RoomLength)
  198.     {
  199.                                              for (int i = ycoord,county = 0;county < RoomLength;i++,county++){
  200.                                              for (int j = xcoord,countx = 0; countx < RoomWidth; j++,countx++){
  201.                                              if (MapArray[i][j].AreaAccess == 1){
  202.                                              return false;
  203.                                              }}}
  204.     }
  205. return true;
  206.  
  207. }
  208. /**************************************************
  209. UPDATE 11/16/2011
  210. MapType::`
  211.  
  212. Changes:
  213.  
  214. 1) Put all constants as define macros
  215. ***************************************************/
  216.  
  217.  
  218. void MapType::CreateRoom(int xcoord, int ycoord)
  219. {
  220.      //Find Total Number of rooms
  221.      TotalNumberOfRooms = RandomNumberGenerator(ROOMLOWERLIMIT,ROOMUPPERLIMIT);
  222.    
  223.     while (TotalNumberOfRooms != RoomCounter){
  224.  
  225.           //Get new chords for new rooms
  226.           RoomLength = RandomNumberGenerator(ROOMLOWERLIMIT,ROOMUPPERLIMIT);
  227.           RoomWidth = RandomNumberGenerator(ROOMLOWERLIMIT,ROOMUPPERLIMIT);
  228.           FindNextRoom(xcoord,ycoord);
  229.           if ((RoomCounter >= 1) && (CanRoomBeCreated(xcoord,ycoord) == true)){
  230.                //Create new rooms
  231.                //Push new chrds of room onto stacktype
  232.        
  233.                StackX.Push(xcoord);
  234.                StackY.Push(ycoord);
  235.    
  236.                //Make Room
  237.    
  238.                for (int i = 0; i < RoomWidth; i++){
  239.                     for (int j = 0; j < RoomLength; j++){
  240.                          MapArray[xcoord+i][ycoord+j].AreaAccess = 1;
  241.                                             }
  242.                                         }
  243.                RoomCounter++;
  244.                CreatePath(xcoord,ycoord);
  245.     }
  246.    
  247.     else if ( (RoomCounter < 1) && (CanRoomBeCreated(xcoord,ycoord) == true)){
  248.         //Push Start of room onto stack
  249.         StackX.Push(xcoord);
  250.          StackY.Push(ycoord);
  251.         //Update as of Sep 18
  252.         //Log Players position on map and set starting point
  253.         UserCurrentXPos = xcoord;
  254.         UserCurrentYPos = ycoord;
  255.         //Place Player On Map Object
  256.         MapArray[xcoord][ycoord].Player = new PlayerType;
  257.        
  258.           //Make Room                                                                    
  259.           for (int i = 0;i < RoomWidth;i++){
  260.                for (int j = 0; j < RoomLength; j++){
  261.                     MapArray[xcoord+i][ycoord+j].AreaAccess = 1;
  262.                     }
  263.                }
  264.           //Incrament Room Counter
  265.           RoomCounter++;
  266.           }
  267.      }
  268.     return;
  269. }
  270. //Room Finder Function
  271. void MapType::FindNextRoom(int &xcoord, int &ycoord)
  272. {
  273. //Get Randomly generated chords for next room though Random Number Gen Function
  274. xcoord = RandomNumberGenerator(0,XLENGTH);
  275. ycoord = RandomNumberGenerator(0,YLENGTH);
  276.  
  277. //Randomize if the cords are the same
  278. while(xcoord == ycoord)
  279. ycoord = RandomNumberGenerator(0,80);
  280.  
  281. return;
  282. }
  283.  
  284. //Function to draw pathlines between rooms
  285. void MapType::CreatePath(int xcoord, int ycoord)
  286. {
  287. int oldxcoord = 0;
  288. int oldycoord = 0;
  289.    
  290. //Return previous room chords
  291. oldxcoord = StackX.Top();
  292. oldycoord = StackY.Top();
  293.    
  294. if ((xcoord == oldxcoord) && (ycoord == oldycoord))
  295. {
  296.   return;
  297. }
  298. //both NEW x and y chords are greater than old ones
  299. else if ((xcoord >= oldxcoord) && (ycoord >= oldycoord))
  300. {
  301.     for (xcoord; xcoord > oldxcoord; xcoord--)
  302.     {
  303.     MapArray[xcoord][ycoord].AreaAccess = 1;
  304.     }
  305.     for (ycoord; ycoord >= oldycoord; ycoord--)
  306.     {
  307.     MapArray[xcoord][ycoord].AreaAccess = 1;
  308.     }
  309. }
  310. //both NEW x and y chords are less than old ones
  311. else if ((xcoord <= oldxcoord) && (ycoord <= oldycoord))
  312. {
  313.     for (xcoord; xcoord < oldxcoord; xcoord++)
  314.     {
  315.     MapArray[xcoord][ycoord].AreaAccess = 1;
  316.     }
  317.     for (ycoord; ycoord < oldycoord; ycoord++)
  318.     {
  319.     MapArray[xcoord][ycoord].AreaAccess = 1;
  320.     }
  321. }
  322.  
  323. //new and old xcoords are the same, new Y chords are greater or equal to old Y
  324. else if ((xcoord == oldxcoord) && (ycoord >= oldycoord)){
  325.     for (int i = ycoord; ycoord > oldycoord; i--)
  326.     MapArray[xcoord][i].AreaAccess = 1;
  327. }
  328. //xcoords both the same, New Y chord is less than old one  
  329. else if ((xcoord == oldxcoord) && (ycoord < oldycoord)){
  330.     for (int i = ycoord; ycoord < oldycoord; i++)
  331.     MapArray[xcoord][i].AreaAccess = 1;
  332. }
  333. //Y chords both the same, Old X greater or equal to new one
  334. else if ((oldycoord == ycoord) && (xcoord >= oldxcoord))
  335. {
  336.      for (int i = xcoord; i > oldxcoord; i++)
  337.      MapArray[i][ycoord].AreaAccess = 1;
  338. }
  339. //ycoords both the same, New X chord is less than old one
  340. else if ((ycoord == oldycoord) && (xcoord < oldxcoord))
  341. {
  342.     for (int i = xcoord; xcoord < oldxcoord; i++)
  343.     MapArray[i][ycoord].AreaAccess = 1;
  344. }
  345. //if newX is less than old X, new Y greater than old y
  346. else if ((xcoord <= oldxcoord) && (ycoord >= oldycoord))
  347. {
  348.      for (xcoord; xcoord < oldxcoord; xcoord++)
  349.         {
  350.         MapArray[xcoord][ycoord].AreaAccess = 1;
  351.         }
  352.   for (ycoord; ycoord > oldycoord; ycoord--)
  353.         {
  354.         MapArray[xcoord][ycoord].AreaAccess = 1;
  355.         }
  356. }
  357. //if newX is greater than than old X, new Y less than than old y
  358. else if ((xcoord > oldxcoord) && (ycoord < oldycoord))
  359. {
  360. for (xcoord; xcoord > oldxcoord; xcoord--)
  361.     {
  362.     MapArray[xcoord][ycoord].AreaAccess = 1;
  363.     }
  364. for (ycoord; ycoord < oldycoord; ycoord++)
  365.     {
  366.   MapArray[xcoord][ycoord].AreaAccess = 1;
  367.     }  
  368. }
  369. }
  370. //PrintMap Function
  371. void MapType::PrintMap()
  372. {
  373. //Clear screen
  374. //Keep in mind that curses seems to have a problem with
  375. //Refreshing the terminal screen if we do not send a clear
  376. //macro to the screen buffer.
  377. clear();
  378. //Prepare Terminal for manipulation
  379. initscr();
  380.    
  381. //Check to see if terminal supports color
  382. if(has_colors() == false)
  383.     {
  384.         endwin();
  385.         printf("Your terminal does not support color \n");
  386.         exit(1);
  387.     }
  388. else
  389.     start_color();
  390.  
  391. //Select pairs of colors, forground and background
  392. init_pair(1, COLOR_WHITE, COLOR_BLACK);
  393. //Apply Color Set
  394. attron(COLOR_PAIR(1));
  395.  
  396. //Print out entire map
  397. for (int counterx = 0; counterx < 40; counterx++)
  398.     {
  399.         for (int countery = 0; countery < 80; countery++)
  400.         {
  401.             //IF PLAYER IS ON SPOT!
  402.             if (MapArray[counterx][countery].Player != NULL)
  403.                 //Display Player Icon
  404.                 addch('@');
  405.             //If cell is WALL
  406.             else if (MapArray[counterx][countery].AreaAccess == 0)
  407.                 addch('#');
  408.             //If Spot Is Open for movement (nothing there)
  409.             else if (MapArray[counterx][countery].AreaAccess == 1)
  410.                 //Display Open Cell Icon
  411.                 addch('.');
  412.         }
  413.         //Knock logical Cursor down to a new line
  414.         addch('\n');
  415.         //Return logical cursor back to left of screen
  416.         addch('\r');
  417.     }
  418. //Turn off terminal color
  419. //attroff(COLOR_PAIR(1));
  420.  
  421. //Refresh output to standard screen!
  422. refresh();
  423.  
  424. //pause the fing screen!!!
  425. //getch();
  426. //MAY HAVE TO REMOVE THIS LINE!!!
  427. //KILL CURSES TERMINAL MANIUPULATION!
  428. //endwin();
  429. }
  430. void MapType::CaptureInput()
  431. {
  432. //Declare Variable
  433. char UserInput = ' ';
  434. //Allow Continious Input
  435. cbreak();
  436. //Stop Character Echo
  437. noecho();
  438.  
  439. //Get User Input Key
  440. UserInput = getch();
  441. //Find out if function is valid.
  442. while (IsInputValid(UserInput) == false ){
  443. clear();
  444. addstr("\nInput is Invalid. Please try again\n");
  445. addstr("\nPress a Key to continue\n");
  446. PrintMap();
  447. UserInput = getch();
  448. }
  449. //Preform Movement
  450. ProcessInput(UserInput);
  451. }
  452.  
  453. bool MapType::IsInputValid(char UserInput)
  454. {
  455. //If User tries to move to the lower left
  456. if (UserInput == '1')
  457. {
  458.     if (MapArray[UserCurrentXPos+1][UserCurrentYPos-1].AreaAccess == 0)
  459.         return false;
  460.     else
  461.     return true;
  462. }
  463. //If User Tries to move down
  464. else if (UserInput == '2')
  465. {
  466.      if (MapArray[UserCurrentXPos+1][UserCurrentYPos].AreaAccess == 0)
  467.     return false;
  468.      else
  469.     return true;
  470. }
  471. //If user Tries to move to lower right
  472. else if (UserInput == '3')
  473. {
  474.     if (MapArray[UserCurrentXPos+1][UserCurrentYPos+1].AreaAccess == 0)
  475.     return false;
  476.     else
  477.     return true;
  478. }
  479. //If user Tries to move to left
  480. else if (UserInput == '4')
  481. {
  482.     if (MapArray[UserCurrentXPos][UserCurrentYPos-1].AreaAccess == 0)
  483.         return false;
  484.     else
  485.         return true;
  486. }
  487. //If user tries to move right
  488. else if (UserInput == '6')
  489. {
  490.     if (MapArray[UserCurrentXPos][UserCurrentYPos+1].AreaAccess == 0)
  491.         return false;
  492.     else
  493.         return true;
  494. }
  495. //If user tries to move to upper left
  496. else if (UserInput == '7')
  497. {
  498.     if (MapArray[UserCurrentXPos-1][UserCurrentYPos-1].AreaAccess == 0)
  499.         return false;
  500.     else
  501.         return true;
  502. }
  503. //If User tries to move up
  504. else if (UserInput == '8')
  505. {
  506.     if (MapArray[UserCurrentXPos-1][UserCurrentYPos].AreaAccess == 0)
  507.         return false;
  508.     else
  509.         return true;
  510. }
  511. //If User tries to move upper left
  512. else if (UserInput == '9')
  513. {
  514.     if (MapArray[UserCurrentXPos-1][UserCurrentYPos+1].AreaAccess == 0)
  515.         return false;
  516.     else
  517.         return true;
  518. }
  519. //Movement is invalid!!
  520. return false;
  521. }
  522. //If User tries to move upper right
  523. void MapType::ProcessInput(char UserInput)
  524. {
  525. //If user tries to move to lower left
  526. if (UserInput == '1'){
  527. MapArray[UserCurrentXPos+1][UserCurrentYPos-1].Player = MapArray[UserCurrentXPos][UserCurrentYPos].Player;
  528. MapArray[UserCurrentXPos][UserCurrentYPos].Player = NULL;
  529. UserCurrentXPos = UserCurrentXPos++;
  530. UserCurrentYPos = UserCurrentYPos--;   
  531. }      
  532. //If User Tries to move down
  533. else if (UserInput == '2')
  534. {
  535. MapArray[UserCurrentXPos+1][UserCurrentYPos].Player = MapArray[UserCurrentXPos][UserCurrentYPos].Player;
  536. MapArray[UserCurrentXPos][UserCurrentYPos].Player = NULL;
  537. UserCurrentXPos = UserCurrentXPos++;
  538. }
  539.  
  540. //If User tries to move to the lower right
  541. else if (UserInput == '3')
  542. {
  543. MapArray[UserCurrentXPos+1][UserCurrentYPos+1].Player = MapArray[UserCurrentXPos][UserCurrentYPos].Player;
  544. MapArray[UserCurrentXPos][UserCurrentYPos].Player = NULL;
  545. UserCurrentXPos = UserCurrentXPos++;
  546. UserCurrentYPos = UserCurrentYPos++;
  547. }
  548. //If user tries to move left
  549. else if (UserInput == '4'){
  550. MapArray[UserCurrentXPos][UserCurrentYPos-1].Player = MapArray[UserCurrentXPos][UserCurrentYPos].Player;
  551. MapArray[UserCurrentXPos][UserCurrentYPos].Player = NULL;
  552. UserCurrentYPos = UserCurrentYPos--;
  553. }
  554. //If user tries to move right
  555. else if (UserInput == '6')
  556. {
  557. MapArray[UserCurrentXPos][UserCurrentYPos+1].Player = MapArray[UserCurrentXPos][UserCurrentYPos].Player;
  558. MapArray[UserCurrentXPos][UserCurrentYPos].Player = NULL;
  559. UserCurrentYPos = UserCurrentYPos++;
  560. }
  561. //If user tries to move upper left
  562. else if (UserInput == '7')
  563. {
  564. MapArray[UserCurrentXPos-1][UserCurrentYPos-1].Player = MapArray[UserCurrentXPos][UserCurrentYPos].Player;
  565. MapArray[UserCurrentXPos][UserCurrentYPos].Player = NULL;
  566. UserCurrentXPos = UserCurrentXPos--;
  567. UserCurrentYPos = UserCurrentYPos--;
  568. }
  569. //If user tries to move up
  570. else if (UserInput == '8')
  571. {
  572. MapArray[UserCurrentXPos-1][UserCurrentYPos].Player = MapArray[UserCurrentXPos][UserCurrentYPos].Player;
  573. MapArray[UserCurrentXPos][UserCurrentYPos].Player = NULL;
  574. UserCurrentXPos = UserCurrentXPos--;   
  575. }
  576. //If user tries to move upper right
  577. else if (UserInput == '9')
  578. {
  579. MapArray[UserCurrentXPos-1][UserCurrentYPos+1].Player = MapArray[UserCurrentXPos][UserCurrentYPos].Player;
  580. MapArray[UserCurrentXPos][UserCurrentYPos].Player = NULL;
  581. UserCurrentXPos = UserCurrentXPos--;
  582. UserCurrentYPos = UserCurrentYPos++;   
  583. }
  584. PrintMap();
  585. }
Add Comment
Please, Sign In to add comment