Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The end product is generating a maze and “playing” the maze, that is, finding a

ID: 3838310 • 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:

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

Exam3 Problem Studenti isryante.com/apzybockioKSTATECs2433Chan Tinspring2017Ichapter17/section/6 zyBookos ofors more resources to hep you. 17.6 Exam Problem2 This content is concerns about this content to your inntnactor you have any technical oortoled by your inntructor andis not zyBooks oontent Dinct questions with the zylab subrmission shstem use the "Wouble with lab?" button atthe bottom of the lab, The end product is generating a maze and "playing the maze, that is, finding a way out. The maze s 2-dimensional and contains many 'spaces Each space is asx and yooordinate Folow each step one at a time. Myou do not finish the whole problem, please indcate which step you are at. This will make grading much easier create four global integer constanta: const int carat int PLAYER 11 carat int EXIT const int NALL 31 Space that has tree vaatles Mointegersxand y and one integer type Don forget public mutator functions and constructors. One constructor must tako two arguments to set thexand y variables. The variable type can be set to be equal to EMPTY Don't forgot the default constructor Includo a public vitual funcion printo that wil print "o" to the sooen oout

Explanation / Answer

Following is the required C++ code :

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

const int EMPTY = 0;
const int PLAYER = 1;
const int EXIT = 2;
const int WALL = 3;

class Space{
   int x,y;
   int type;
public:
   Space( int x1, int y1 ){
       x = x1;   y = y1;   type = EMPTY;
   };
   Space(){
       x = 0;   y = 0;   type = EMPTY;
   }
   int get_x(){ return x; }
   int get_y(){ return y; }
   int get_type(){ return type; }
   int set_x( int x1 ){ x = x1; }
   int set_y( int y1 ){ y = y1; }
   void virtual print(){
       cout << "O";
   }
};

class Player: public Space{
   int x,y,type;
public:
   Player( int x1, int y1 ){
       x = x1;   y = y1;   type = PLAYER;
   }
   Player(){
       x = 0;   y = 0;   type = PLAYER;
   }
   void print(){
       cout << "P";
   }
};

class Exit: public Space{
   int x,y,type;
public:
   Exit( int x1, int y1 ){
       x = x1;   y = y1;   type = EXIT;
   }
   Exit(){
       x = 0;   y = 0;   type = EXIT;
   }
   void print(){
       cout << "X";
   }
};

class Grid{
   Space*** grid;
   int width;
   int height;
   int player_x, player_y;
   int exit_x, exit_y;
public:
   Grid(){
       width = 0;
       height = 0;
       grid = NULL;
   }
   Grid( int w, int h ){
       width = w;
       height = h;
       grid = new Space**[ height ];
       for( int i = 0; i < height; i++ ){
           grid[i] = new Space*[ width ];
       }
   }
   ~Grid(){
       for( int i = 0; i < height; i++ ){
           for( int j = 0; j < width; j++ ){
               delete grid[i][j];
           }
       }
       for( int i = 0; i < height; i++ ){
           delete grid[i];
       }
       delete grid;
   }
   void generateSpaces(){
       for( int i = 0; i < height; i++ ){
           for( int j = 0; j < width; j++ ){
               grid[i][j] = new Space( i, j );
           }
       }
   }
   void generatePlayer(){
       int pos_x = rand()%height;
       int pos_y = rand()%width;
       Player *is = new Player( pos_x, pos_y );
       grid[ pos_x][ pos_y ] = is;
       player_x = pos_x;
       player_y = pos_y;
   }
   void generateExit(){
       int pos_x = rand()%height;
       int pos_y = rand()%width;
       while( grid[pos_x][pos_y]->get_type() == PLAYER ){
           int pos_x = rand()%height;
           int pos_y = rand()%width;
       }
       Exit* is = new Exit( pos_x, pos_y );
       grid[ pos_x][ pos_y ] = is;
       exit_x = pos_x;
       exit_y = pos_y;
   }
   void printGrid(){
       for( int i = 0; i < height; i++ ){
           for( int j = 0; j < width; j++ ){
               grid[i][j]->print();
           }
           cout << " ";
       }
   }
   void navigatePlayer(){
       if(player_x==exit_x and player_y == exit_y ){
           cout << " Out of the maze ";
           return;
       }

       int old_player_x = player_x;
       int old_player_y = player_y;

       char move;
       do{
           cout << "What is your move ( L/R/U/D ) ? ";
           cin >> move;
       }while( not(move=='L' or move=='R' or move=='U' or move=='D' ));
       if(move == 'L' and player_y> 0 ){
           player_y = player_y- 1;
       }
       if( move == 'R' and player_y < width ){
           player_y = player_y + 1;
       }
       if( move == 'U' and player_x > 0 ){
           player_x = player_x - 1;
       }
       if( move == 'D' and player_x < height ){
           player_x = player_x + 1;
       }

       Space* player_object = grid[ old_player_x ][ old_player_y ];
       grid[ old_player_x ][ old_player_y ] = grid[ player_x ][ player_y ];
       grid[ player_x ][ player_y ] = player_object;
   }

};


int main(){
   int w,h;
   cout << "Enter width of the grid : ";
   cin >> w;
   cout << "Enter height of the grid : ";
   cin >> h;
   Grid newGrid( w, h );  
   srand( time(NULL) );
   newGrid.generateSpaces();
   newGrid.generatePlayer();
   newGrid.generateExit();
   newGrid.printGrid();
   for(int i = 0; i < 5; i++ ){
       newGrid.navigatePlayer();
       newGrid.printGrid();
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote