The end product is generating a maze and “playing” the maze, that is, finding a
ID: 3838281 • Letter: T
Question
The end product is generating a maze and “playing” the maze, that is, finding a way out. The maze is 2-dimensional and contains many “spaces”. Each space is a x and y coordinate. Follow each step one at a time. If you do not finish the whole problem, please indicate which step you are at. This will make grading much easier. Step 0: Create four global integer constants: const int EMPTY = 0; const int PLAYER = 1; const int EXIT = 2; const int WALL = 3;
Step 1: Create a class Space that has three private variables: two integers x and y, and one integer type. Don’t forget public accessor, mutator functions and constructors. One constructor must take two arguments to set the x and y variables. The variable type can be set to be equal to EMPTY. Don’t forget the default constructor. Include a public virtual function print() that will print “O” to the screen: cout << “O”;
Step 2: Create a new class Player which inherits Space. Variable type should be set to PLAYER. Don’t forget constructors. Override the function print() to cout “P”. Create new class Exit which inherits Space. Variable type should be set to EXIT. Don’t forget constructors. Override the function print() to cout “X”.
Step 3: Create a new class Grid. It has one private variable spaces which is a two-dimensional dynamic array of Space. You also will need two more private variables to store the width and height of the grid. Remember to include constructors (especially one that takes two arguments which are the sizes of the dynamic array). Include a destructor as well. You might find it useful to define a new alias type: typedef Space * P_Space;
Step 4: In your main function, ask the user to input the size of the grid (x and y sizes). Then create a new Grid object and initialize. Step 5: Go back to your Grid class.
Step5.1: Create a new public member method generateSpaces() to generate Space objects for the whole grid.
Step 5.2: Create a new public member function generatePlayer() that creates one Player space. This will place the Player on a random position in the grid. Create a new public member function generateExit() that creates one Exit space. Make sure the Exit space is placed on a Space object (not a Player object).
Step 5.3: Implement a printGrid() function Step 5.4: Create a new public member function navigatePlayer() to ask user for input (left, right, up, or down) to navigate Player to Exit. Check for out of bounds (the grid does not wrap around), and when Player == Exit.
Step 6: Test that your code work correctly. Make sure to complete this step before you move on.
Step 7: That's it, folks! The rest of the steps are optional and can be completed for extra credit.
Step 8: Create Wall class that inherits Space. Override print() to cout “W”.
Step 9: In Grid class, create a new function generateWall(int k) to generate k number of Wall objects. k is input from the user from the main() function. Make sure it is placed on a Space object (not Player, Exit, or another Wall object) Create grid. Generate maze. Ask user if it is okay. If yes, go to
Step 10. If not, go to Step 9.
Step 10: Ask user for input (left, right, up, down) to navigate Player to Exit. Similar to Step 6, except Player != Wall.
Step 11: Test your code! If it works, submit and go to
Step 12. Else, debug… Step 12: Do the robot dance and enjoy your summer! Sample maze game output is provided in the handouts
Explanation / Answer
#include<stdio.h>
#include<iostream>
#include <cstdlib>
#include<ctime>
#define FALSE 0
#define TRUE 1
using namespace std;
const int SIZE=12;
const int EMPTY = 0;
const int PLAYER = 1;
const int EXIT = 2;
const int WALL = 3;
class Space{
public:
int X;
int Y;
typedef space Z = EMPTY;
Space(); // constructor
Space(int X, int Y){
cout << "O";
}
}
class Player : public Space{
typedef int player = PLAYER;
Player(); // constructor
cout << "P";
}
class Exit : public Space{
typedef int exit = EXIT;
Exit(); // constructor
cout << "X";
}
class Grid {
private:
char M[SIZE][SIZE]; // array of random numbers
int size; // size of the array
public:
Grid(); // constructor
void read_Grid();
int Grid_traverse(int row, int col);
void display_Grid();
};
int main() {
int i, j;
Grid M;
M.display_Grid();
M.Grid_traverse(2,0);
M.display_Grid();
}
// constructor
Grid::Grid() {
read_Grid();
}
// constructor
void Grid::read_Grid() {
int i, j;
FILE* ifp;
ifp=fopen("Grid.txt", "r");
for(i=0; i< SIZE; i++)
for(j=0; j< SIZE; j++)
fscanf(ifp, " %c ", &M[i][j]);
}
int Grid::Grid_traverse(int row, int col) {
// return TRUE if at the end of the Grid
if (M[row][col] == 'E') return TRUE;
// return FALSE if not at the end of the Grid
if (M[row][col] > SIZE-1 || M[row][col] == 'S') return FALSE;
// mark + for part of the path
M[row][col] = '+';
// return TRUE if the path goes to the North
if (M[row-1][col] == TRUE) return TRUE;
// return TRUE if the path goes to the South
if (M[row+1][col] == TRUE) return TRUE;
// return TRUE if the path goes to the East
if (M[row][col+1] == TRUE) return TRUE;
// return TRUE if the path goes to the West
if (M[row][col-1] == TRUE) return TRUE;
// mark x for the wrong path
M[row][col] = 'x';
return FALSE;
}
void Grid::display_Grid(){
int i, j;
for(i=0; i<SIZE; i++) {
for(j=0; j< SIZE; j++)
printf("%c ", M[i][j]);
cout << endl;
}
cout << endl;
}
/*
// Pause the console so it doesn't close (when running in Windows)
cout << endl << endl << endl << endl << endl;
system("pause");
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.