Advertisement
wild4fun88

Untitled

Nov 24th, 2014
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.37 KB | None | 0 0
  1. //include standard libraries
  2. #include <iostream> //for output and input: cout <<, cin >>
  3. #include <iomanip>  //for output manipulators
  4. #include <conio.h>  //for getch()
  5. #include <string>   //for string
  6. using namespace std;
  7.  
  8. //include our own libraries
  9. #include "ConsoleUtils.h"   //for Clrscr, Gotoxy, etc.
  10.  
  11. //---------------------------------------------------------------------------
  12. //----- define constants
  13. //---------------------------------------------------------------------------
  14.  
  15. //defining the size of the grid
  16.  
  17. const int  SIZEY(6);    //vertical dimension
  18. const int  SIZEX(10);    //horizontal dimension
  19. //defining symbols used for display of the grid & content
  20. const char MOUSE('@');   //mouse
  21. const char TUNNEL(' ');  //tunnel
  22. const char WALL('#');    //border
  23. //defining the command letters to move the mouse on the maze
  24. const int  UP(72);       //up arrow
  25. const int  DOWN(80);     //down arrow
  26. const int  RIGHT(77);   //right arrow
  27. const int  LEFT(75);    //left arrow
  28. //defining the other command letters
  29. const char QUIT('Q');   //to end the game
  30.  
  31. struct Item {
  32.     int x, y;
  33.     const char symbol;
  34. };
  35.  
  36. //---------------------------------------------------------------------------
  37. //----- run game
  38. //---------------------------------------------------------------------------
  39.  
  40. int main()
  41. {
  42.     //function declarations (prototypes)
  43.     void initialiseGame(char g[][SIZEX + 1], char m[][SIZEX + 1], Item& mouse);
  44.     void paintGame(const char g[][SIZEX + 1], string& mess);
  45.     bool wantsToQuit(int key);
  46.     bool isArrowKey(int k);
  47.     int  getKeyPress();
  48.     void moveMouse(const char g[][SIZEX + 1], Item& mouse, int key, string& mess);
  49.     void updateGrid(char g[][SIZEX + 1], const char m[][SIZEX + 1], Item mouse);
  50.     void endProgram();
  51.  
  52. //local variable declarations
  53. char grid[SIZEY + 1][SIZEX + 1];    //grid for display
  54. char maze[SIZEY + 1][SIZEX + 1];    //structure of the maze
  55. Item mouse = { (SIZEX / 2) + 1, (SIZEY / 2) + 1, MOUSE }; //mouse's position and symbol
  56. string message("LET'S START...");   //current message to player
  57.  
  58.     //action...
  59.     Clrscr();
  60.     initialiseGame(grid, maze, mouse);  //initialise grid (incl. walls & mouse)
  61.     paintGame(grid, message);   //display game info, modified grid & messages
  62.     int key(getKeyPress());     //read in  selected key: arrow or letter command
  63.     while (!wantsToQuit(key))       //while user does not want to quit
  64.     {
  65.         if (isArrowKey(key))
  66.         {
  67.             int dx(0), dy(0);
  68.     moveMouse(grid, mouse, key, message);   //move mouse in that direction
  69.     updateGrid(grid, maze, mouse);  //update grid information
  70.         }
  71.         else
  72.     message = "INVALID KEY!";   //set 'Invalid key' message
  73.     paintGame(grid, message);   //display game info, modified grid & messages
  74.     key = getKeyPress();    //display menu & read in next option
  75.     }
  76.     endProgram();       //display final message
  77.     return 0;
  78. }
  79.  
  80. //---------------------------------------------------------------------------
  81. //----- initialise game state
  82. //---------------------------------------------------------------------------
  83.  
  84. void initialiseGame(char grid[][SIZEX + 1], char maze[][SIZEX + 1], Item& mouse)
  85. { //initialise grid & place mouse in middle
  86.     void setInitialMazeStructure(char g[][SIZEX + 1]);
  87.     void setInitialMouseCoordinates(Item& mouse);
  88.     void updateGrid(char g[][SIZEX + 1], const char m[][SIZEX + 1], Item mouse);
  89.  
  90.     setInitialMazeStructure(maze);  //initialise maze
  91.     setInitialMouseCoordinates(mouse);  //initialise mouse's position
  92.     updateGrid(grid, maze, mouse);  //prepare grid
  93. }
  94.  
  95.  
  96. void setInitialMazeStructure(char maze[][SIZEX + 1])
  97.  
  98.  
  99. { //set the position of the walls in the maze
  100.   //initialise maze configuration
  101.  
  102. char initialMaze[SIZEY + 1][SIZEX + 1]  //local array to store the maze structure
  103.  
  104.         = { { 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X' },
  105.         { 'X', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#' },
  106.         { 'X', '#', '#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#' },
  107.         { 'X', '#', ' ', ' ', '#', ' ', ' ', '#', ' ', ' ', '#' },
  108.         { 'X', '#', ' ', ' ', '#', '#', ' ', '#', ' ', '#', '#' },
  109.         { 'X', '#', '#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#' },
  110.         { 'X', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#' } };
  111.     // with '#' for wall, ' ' for tunnel and 'X' for unused part of array
  112.     //copy into maze structure
  113.     for (int row(1); row <= SIZEY; ++row)   //for each row (vertically)
  114.     for (int col(1); col <= SIZEX; ++col)   //for each column (horizontally)
  115.         maze[row][col] = initialMaze[row][col];
  116. }
  117.  
  118. void setInitialMouseCoordinates(Item& mouse)
  119. { //calculate mouse's coordinates at beginning of game
  120.     mouse.y = 3;    //y-coordinate: vertically
  121.     mouse.x = 5;    //x-coordinate: horizontally
  122. }
  123.  
  124. //---------------------------------------------------------------------------
  125. //----- update grid state
  126. //---------------------------------------------------------------------------
  127.  
  128. void updateGrid(char grid[][SIZEX + 1], const char maze[][SIZEX + 1], Item mouse)
  129. { //update grid configuration after each move
  130.     void setMaze(char g[][SIZEX + 1], const char m[][SIZEX + 1]);
  131.     void placeMouse(char g[][SIZEX + 1], Item mouse);
  132.  
  133.     setMaze(grid, maze);    //reset the empty maze configuration into grid
  134.     placeMouse(grid, mouse);    //set mouse in grid
  135. }
  136.  
  137. void setMaze(char grid[][SIZEX + 1], const char maze[][SIZEX + 1])
  138. { //reset the empty/fixed maze configuration into grid
  139.     for (int row(1); row <= SIZEY; ++row)   //for each row (vertically)
  140.     for (int col(1); col <= SIZEX; ++col)   //for each column (horizontally)
  141.         grid[row][col] = maze[row][col];
  142. }
  143.  
  144. void placeMouse(char g[][SIZEX + 1], Item m)
  145. { //place mouse at its new position in grid
  146.     g[m.y][m.x] = m.symbol;
  147. }
  148.  
  149. //---------------------------------------------------------------------------
  150. //----- move the mouse
  151. //---------------------------------------------------------------------------
  152. void moveMouse(const char g[][SIZEX + 1], Item& m, int key, string& mess)
  153. { //move mouse in required direction
  154.     void setKeyDirection(int k, int& dx, int& dy);
  155.     //calculate direction of movement required by key - if any
  156.     int dx(0), dy(0);
  157.     setKeyDirection(key, dx, dy);   //find direction indicated by key
  158.     //check new target position in grid & update mouse coordinates if move is possible
  159.     switch (g[m.y + dy][m.x + dx])
  160.     {           //...depending on what's on the target position in grid...
  161.     case TUNNEL:        //can move
  162.         m.y += dy;      //go in that Y direction
  163.         m.x += dx;      //go in that X direction
  164.         break;
  165.  
  166.     case WALL:          //hit a wall & stay there
  167.         cout << '\a';   //beep the alarm
  168.         mess = "CANNOT GO THERE!";
  169.         break;
  170.     }
  171. }
  172.  
  173. //---------------------------------------------------------------------------
  174. //----- process key
  175. //---------------------------------------------------------------------------
  176. void setKeyDirection(int key, int& dx, int& dy)
  177. { //
  178.     switch (key)        //...depending on the selected key...
  179.     {
  180.     case LEFT:      //when LEFT arrow pressed...
  181.         dx = -1;    //decrease the X coordinate
  182.         dy = 0;
  183.         break;
  184.     case RIGHT:     //when RIGHT arrow pressed...
  185.         dx = +1;    //increase the X coordinate
  186.         dy = 0;
  187.         break;
  188.     case UP:        //when UP arrow pressed...
  189.         dx = 0;
  190.         dy = -1;    //decrease the Y coordinate
  191.         break;
  192.     case DOWN:      //when DOWN arrow pressed...
  193.         dx = 0;
  194.         dy = +1;    //increase the Y coordinate
  195.         break;
  196.     }
  197. }
  198.  
  199. int getKeyPress()
  200. { //get key or command selected by user
  201.     int keyPressed;
  202.     keyPressed = getch();   //read in the selected arrow key or command letter
  203.     while (keyPressed == 224)   //ignore symbol following cursor key
  204.         keyPressed = getch();
  205.     return(toupper(keyPressed));    //return it in uppercase
  206. }
  207.  
  208. bool isArrowKey(int key)
  209. {   //check if the key pressed is an arrow key (also accept 'K', 'M', 'H' and 'P')
  210.     return ((key == LEFT) || (key == RIGHT) || (key == UP) || (key == DOWN));
  211. }
  212. bool wantsToQuit(int key)
  213. {   //check if the user wants to quit (when key is 'Q' or 'q')
  214.     return (toupper(key) == QUIT);
  215. }
  216.  
  217. //---------------------------------------------------------------------------
  218. //----- display info on screen
  219. //---------------------------------------------------------------------------
  220. void paintGame(const char gd[][SIZEX + 1], string& mess)
  221. { //display game title, messages, maze, mouse & apples on screen
  222.     void paintGrid(const char g[][SIZEX + 1]);
  223.  
  224.     //clear screen
  225.     Clrscr();
  226.  
  227.     //display game title
  228.     SelectTextColour(clYellow);
  229.     Gotoxy(0, 0);
  230.     cout << "___MOUSE AND APPLES GAME___\n" << endl;
  231.     SelectBackColour(clWhite);
  232.     SelectTextColour(clRed);
  233.     Gotoxy(40, 0);
  234.     cout << "Pascale Vacher: March 14";  //PUT YOUR GROUP NUMBER AND NAMES HERE
  235.  
  236.     // display grid contents
  237.     paintGrid(gd);
  238.  
  239.     //display menu options available
  240.     SelectBackColour(clRed);
  241.     SelectTextColour(clYellow);
  242.     Gotoxy(40, 3);
  243.     cout << "TO MOVE USE KEYBOARD ARROWS ";
  244.     Gotoxy(40, 4);
  245.     cout << "TO QUIT ENTER 'Q'           ";
  246.  
  247.     //print auxiliary messages if any
  248.     SelectBackColour(clBlack);
  249.     SelectTextColour(clWhite);
  250.     Gotoxy(40, 8);
  251.     cout << mess;   //display current message
  252.     mess = "";      //reset message to blank
  253. }
  254.  
  255.  
  256. void paintGrid(const char g[][SIZEX + 1])
  257. {
  258. //display grid content on screen
  259. SelectBackColour(clBlack);
  260. SelectTextColour(clWhite);
  261. Gotoxy(0, 2);
  262. for (int row(1); row <= SIZEY; ++row)   //for each row (vertically)
  263. {
  264.  for (int col(1); col <= SIZEX; ++col)  //for each column (horizontally)
  265.  cout << g[row][col];   //output cell content
  266.  cout << endl;
  267.   }
  268. }
  269.  
  270. void endProgram()
  271. {
  272.     SelectBackColour(clRed);
  273.     SelectTextColour(clYellow);
  274.     Gotoxy(40, 8);
  275.     //hold output screen until a keyboard key is hit
  276.     cout << "\n";
  277.     system("pause");
  278. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement