Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //include standard libraries
- #include <iostream> //for output and input: cout <<, cin >>
- #include <iomanip> //for output manipulators
- #include <conio.h> //for getch()
- #include <string> //for string
- using namespace std;
- //include our own libraries
- #include "ConsoleUtils.h" //for Clrscr, Gotoxy, etc.
- //---------------------------------------------------------------------------
- //----- define constants
- //---------------------------------------------------------------------------
- //defining the size of the grid
- const int SIZEY(6); //vertical dimension
- const int SIZEX(10); //horizontal dimension
- //defining symbols used for display of the grid & content
- const char MOUSE('@'); //mouse
- const char TUNNEL(' '); //tunnel
- const char WALL('#'); //border
- //defining the command letters to move the mouse on the maze
- const int UP(72); //up arrow
- const int DOWN(80); //down arrow
- const int RIGHT(77); //right arrow
- const int LEFT(75); //left arrow
- //defining the other command letters
- const char QUIT('Q'); //to end the game
- struct Item {
- int x, y;
- const char symbol;
- };
- //---------------------------------------------------------------------------
- //----- run game
- //---------------------------------------------------------------------------
- int main()
- {
- //function declarations (prototypes)
- void initialiseGame(char g[][SIZEX + 1], char m[][SIZEX + 1], Item& mouse);
- void paintGame(const char g[][SIZEX + 1], string& mess);
- bool wantsToQuit(int key);
- bool isArrowKey(int k);
- int getKeyPress();
- void moveMouse(const char g[][SIZEX + 1], Item& mouse, int key, string& mess);
- void updateGrid(char g[][SIZEX + 1], const char m[][SIZEX + 1], Item mouse);
- void endProgram();
- //local variable declarations
- char grid[SIZEY + 1][SIZEX + 1]; //grid for display
- char maze[SIZEY + 1][SIZEX + 1]; //structure of the maze
- Item mouse = { (SIZEX / 2) + 1, (SIZEY / 2) + 1, MOUSE }; //mouse's position and symbol
- string message("LET'S START..."); //current message to player
- //action...
- Clrscr();
- initialiseGame(grid, maze, mouse); //initialise grid (incl. walls & mouse)
- paintGame(grid, message); //display game info, modified grid & messages
- int key(getKeyPress()); //read in selected key: arrow or letter command
- while (!wantsToQuit(key)) //while user does not want to quit
- {
- if (isArrowKey(key))
- {
- int dx(0), dy(0);
- moveMouse(grid, mouse, key, message); //move mouse in that direction
- updateGrid(grid, maze, mouse); //update grid information
- }
- else
- message = "INVALID KEY!"; //set 'Invalid key' message
- paintGame(grid, message); //display game info, modified grid & messages
- key = getKeyPress(); //display menu & read in next option
- }
- endProgram(); //display final message
- return 0;
- }
- //---------------------------------------------------------------------------
- //----- initialise game state
- //---------------------------------------------------------------------------
- void initialiseGame(char grid[][SIZEX + 1], char maze[][SIZEX + 1], Item& mouse)
- { //initialise grid & place mouse in middle
- void setInitialMazeStructure(char g[][SIZEX + 1]);
- void setInitialMouseCoordinates(Item& mouse);
- void updateGrid(char g[][SIZEX + 1], const char m[][SIZEX + 1], Item mouse);
- setInitialMazeStructure(maze); //initialise maze
- setInitialMouseCoordinates(mouse); //initialise mouse's position
- updateGrid(grid, maze, mouse); //prepare grid
- }
- void setInitialMazeStructure(char maze[][SIZEX + 1])
- { //set the position of the walls in the maze
- //initialise maze configuration
- char initialMaze[SIZEY + 1][SIZEX + 1] //local array to store the maze structure
- = { { 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X' },
- { 'X', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#' },
- { 'X', '#', '#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#' },
- { 'X', '#', ' ', ' ', '#', ' ', ' ', '#', ' ', ' ', '#' },
- { 'X', '#', ' ', ' ', '#', '#', ' ', '#', ' ', '#', '#' },
- { 'X', '#', '#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#' },
- { 'X', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#' } };
- // with '#' for wall, ' ' for tunnel and 'X' for unused part of array
- //copy into maze structure
- for (int row(1); row <= SIZEY; ++row) //for each row (vertically)
- for (int col(1); col <= SIZEX; ++col) //for each column (horizontally)
- maze[row][col] = initialMaze[row][col];
- }
- void setInitialMouseCoordinates(Item& mouse)
- { //calculate mouse's coordinates at beginning of game
- mouse.y = 3; //y-coordinate: vertically
- mouse.x = 5; //x-coordinate: horizontally
- }
- //---------------------------------------------------------------------------
- //----- update grid state
- //---------------------------------------------------------------------------
- void updateGrid(char grid[][SIZEX + 1], const char maze[][SIZEX + 1], Item mouse)
- { //update grid configuration after each move
- void setMaze(char g[][SIZEX + 1], const char m[][SIZEX + 1]);
- void placeMouse(char g[][SIZEX + 1], Item mouse);
- setMaze(grid, maze); //reset the empty maze configuration into grid
- placeMouse(grid, mouse); //set mouse in grid
- }
- void setMaze(char grid[][SIZEX + 1], const char maze[][SIZEX + 1])
- { //reset the empty/fixed maze configuration into grid
- for (int row(1); row <= SIZEY; ++row) //for each row (vertically)
- for (int col(1); col <= SIZEX; ++col) //for each column (horizontally)
- grid[row][col] = maze[row][col];
- }
- void placeMouse(char g[][SIZEX + 1], Item m)
- { //place mouse at its new position in grid
- g[m.y][m.x] = m.symbol;
- }
- //---------------------------------------------------------------------------
- //----- move the mouse
- //---------------------------------------------------------------------------
- void moveMouse(const char g[][SIZEX + 1], Item& m, int key, string& mess)
- { //move mouse in required direction
- void setKeyDirection(int k, int& dx, int& dy);
- //calculate direction of movement required by key - if any
- int dx(0), dy(0);
- setKeyDirection(key, dx, dy); //find direction indicated by key
- //check new target position in grid & update mouse coordinates if move is possible
- switch (g[m.y + dy][m.x + dx])
- { //...depending on what's on the target position in grid...
- case TUNNEL: //can move
- m.y += dy; //go in that Y direction
- m.x += dx; //go in that X direction
- break;
- case WALL: //hit a wall & stay there
- cout << '\a'; //beep the alarm
- mess = "CANNOT GO THERE!";
- break;
- }
- }
- //---------------------------------------------------------------------------
- //----- process key
- //---------------------------------------------------------------------------
- void setKeyDirection(int key, int& dx, int& dy)
- { //
- switch (key) //...depending on the selected key...
- {
- case LEFT: //when LEFT arrow pressed...
- dx = -1; //decrease the X coordinate
- dy = 0;
- break;
- case RIGHT: //when RIGHT arrow pressed...
- dx = +1; //increase the X coordinate
- dy = 0;
- break;
- case UP: //when UP arrow pressed...
- dx = 0;
- dy = -1; //decrease the Y coordinate
- break;
- case DOWN: //when DOWN arrow pressed...
- dx = 0;
- dy = +1; //increase the Y coordinate
- break;
- }
- }
- int getKeyPress()
- { //get key or command selected by user
- int keyPressed;
- keyPressed = getch(); //read in the selected arrow key or command letter
- while (keyPressed == 224) //ignore symbol following cursor key
- keyPressed = getch();
- return(toupper(keyPressed)); //return it in uppercase
- }
- bool isArrowKey(int key)
- { //check if the key pressed is an arrow key (also accept 'K', 'M', 'H' and 'P')
- return ((key == LEFT) || (key == RIGHT) || (key == UP) || (key == DOWN));
- }
- bool wantsToQuit(int key)
- { //check if the user wants to quit (when key is 'Q' or 'q')
- return (toupper(key) == QUIT);
- }
- //---------------------------------------------------------------------------
- //----- display info on screen
- //---------------------------------------------------------------------------
- void paintGame(const char gd[][SIZEX + 1], string& mess)
- { //display game title, messages, maze, mouse & apples on screen
- void paintGrid(const char g[][SIZEX + 1]);
- //clear screen
- Clrscr();
- //display game title
- SelectTextColour(clYellow);
- Gotoxy(0, 0);
- cout << "___MOUSE AND APPLES GAME___\n" << endl;
- SelectBackColour(clWhite);
- SelectTextColour(clRed);
- Gotoxy(40, 0);
- cout << "Pascale Vacher: March 14"; //PUT YOUR GROUP NUMBER AND NAMES HERE
- // display grid contents
- paintGrid(gd);
- //display menu options available
- SelectBackColour(clRed);
- SelectTextColour(clYellow);
- Gotoxy(40, 3);
- cout << "TO MOVE USE KEYBOARD ARROWS ";
- Gotoxy(40, 4);
- cout << "TO QUIT ENTER 'Q' ";
- //print auxiliary messages if any
- SelectBackColour(clBlack);
- SelectTextColour(clWhite);
- Gotoxy(40, 8);
- cout << mess; //display current message
- mess = ""; //reset message to blank
- }
- void paintGrid(const char g[][SIZEX + 1])
- {
- //display grid content on screen
- SelectBackColour(clBlack);
- SelectTextColour(clWhite);
- Gotoxy(0, 2);
- for (int row(1); row <= SIZEY; ++row) //for each row (vertically)
- {
- for (int col(1); col <= SIZEX; ++col) //for each column (horizontally)
- cout << g[row][col]; //output cell content
- cout << endl;
- }
- }
- void endProgram()
- {
- SelectBackColour(clRed);
- SelectTextColour(clYellow);
- Gotoxy(40, 8);
- //hold output screen until a keyboard key is hit
- cout << "\n";
- system("pause");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement