In C++ The end product is generating a maze and “playing” the maze, that is, fin
ID: 3838308 • Letter: I
Question
In C++
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!
Explanation / Answer
#include <iostream> //in and output libary
#include <cmath> // math libary for rand
#include <time.h> // time libary for time(NULL)
#include <cstdlib>
using namespace std;
struct character
{
int posX ;
int posY ;
char symbol;
int hp;
} player; // struct for the player
struct enemies
{
int posX;
int posY;
char enemy_symbol;
};// struct for the enemies
void player_hit()
{
player.posX = 1;
player.posY =1;
player.hp --;
}// function for when the player hits an enemy
int main ()
{
enemies enemy_horizontal = {13, 9, 'H'}; //placing enemy_horizontal at location [9][13] and gives it the sign H
enemies enemy_vertical = { 11 , 9, 'V'};
enemies enemy_jump = {1, 15, 'J'};
const char HEIGHT = 18, WIDTH = 18; // the maze width and height
bool update = false; // boolean to control when the map needs to be updated
char move_key = 'd';
player.posX = 1;
player.posY = 1;
player.symbol = 'O';
player.hp = 3; // players different attributes
unsigned char maze[WIDTH][HEIGHT] =
{
'#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#',
'#',' ','#',' ',' ',' ','#','#','#',' ',' ','#','#',' ','#',' ','#','#',
'#',' ','#','#',' ','#','#',' ','#',' ','#','#',' ',' ','#',' ','#',' ',
'#',' ','#','#',' ',' ',' ',' ','#',' ','#','#',' ','#','#',' ',' ',' ',
'#',' ',' ',' ',' ','#',' ','#',' ',' ',' ',' ',' ','#','#','#',' ','#',
'#',' ','#','#',' ','#',' ','#','#',' ','#','#',' ',' ','#',' ',' ','#',
'#','#','#',' ',' ','#',' ','#','#',' ',' ','#',' ','#','#',' ','#','#',
'#','#',' ',' ','#','#','#','#',' ',' ','#',' ',' ','#','#',' ','#','#',
'#','#','#',' ','#','#','#','#','#',' ','#',' ','#','#','#',' ','#','#',
'#',' ','#',' ',' ',' ','#','#','#',' ',' ',' ','#',' ','#',' ','#','#',
'#',' ','#','#',' ','#','#',' ','#',' ','#','#',' ',' ','#',' ','#','#',
'#',' ','#','#',' ',' ',' ',' ',' ',' ','#','#',' ','#','#',' ',' ','#',
'#',' ',' ',' ',' ','#',' ','#','#',' ','#',' ',' ','#','#','#',' ','#',
'#',' ','#','#',' ','#',' ','#','#',' ','#','#',' ',' ',' ',' ',' ','#',
'#','#','#',' ',' ','#',' ','#','#',' ',' ','#',' ','#','#',' ','#','#',
'#','#','#','#',' ','#','#','#','#',' ','#',' ',' ','#','#',' ','#','#',
'#','#','#',' ',' ',' ',' ','#','#',' ',' ',' ','#','#','#',' ','#','#',
'#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#','#',
};
cout << "press (d) (a) (w) or (s) and then return to start the game ";
cout << "presse (Q) then return to quit the game ";
while (move_key !='q') // as long as move_key is not q then this loop will run and thereby everysingle little thing, which is happening in the maze
{
srand(time(NULL)); // random seed based on some time thingy
int random_move = rand() %2 +1 ; // random number between 1 and 2
cin >> move_key;
update = true; // map updates
if (update == true) // if map is updated, then the enemies will be printed onto the map and the player
// the maze will be created by a nested for loop, but the x and y are at the wrong places in order to create the map I have "painted" with #
// the health will be outputted, so the gamer can see the life of the "player"
// update stopped
{
system("CLS");
maze[enemy_horizontal.posX][enemy_horizontal.posY] = enemy_horizontal.enemy_symbol;
maze[enemy_vertical.posX][enemy_vertical.posY] = enemy_vertical.enemy_symbol;
maze[enemy_jump.posX][enemy_jump.posY] = enemy_jump.enemy_symbol;
maze[player.posX][player.posY] = player.symbol;
for(int y=0; y<HEIGHT; y++)
{
cout << endl;
for(int x=0; x<WIDTH; x++)
{
cout << maze[x][y];
}
}
cout << endl << endl << " health: " << player.hp << endl << "____________________ ";
update = false;
}
switch (move_key) // different key pressed in order to move the player and to see if the player collides with a # or an enemy
// it will be printed out the "player's" location, because the game are asynchron
// it will also be printed if the player hits a wall
{
case 'w':
update = true;
if (maze[player.posX][player.posY -1] != '#')
{
maze[player.posX][player.posY] = ' ';
player.posY --;
if ( maze[player.posX ][player.posY ] == maze[enemy_horizontal.posX][enemy_horizontal.posY] ||
maze[player.posX ][player.posY ] == maze[enemy_vertical.posX][enemy_vertical.posY] ||
maze[player.posX ][player.posY ] == maze[enemy_jump.posX][enemy_jump.posY])
{
player_hit();
}
cout << player.posX << " " << player.posY << endl;
}
else
cout << " you hit a wall";
break;
case 's':
update = true;
if (maze[player.posX][player.posY +1] != '#')
{
maze[player.posX][player.posY] = ' ';
player.posY ++;
if ( maze[player.posX ][player.posY] == maze[enemy_horizontal.posX][enemy_horizontal.posY] ||
maze[player.posX ][player.posY] == maze[enemy_vertical.posX][enemy_vertical.posY] ||
maze[player.posX ][player.posY] == maze[enemy_jump.posX][enemy_jump.posY])
{
player_hit();
}
cout << player.posX << " " << player.posY << endl;
}
else
cout << " you hit a wall";
break;
case 'a':
update = true;
if (maze[player.posX -1][player.posY] != '#')
{
maze[player.posX][player.posY] = ' ';
player.posX --;
if ( maze[player.posX ][player.posY] == maze[enemy_horizontal.posX][enemy_horizontal.posY] ||
maze[player.posX ][player.posY] == maze[enemy_vertical.posX][enemy_vertical.posY] ||
maze[player.posX ][player.posY] == maze[enemy_jump.posX][enemy_jump.posY])
{
player_hit();
}
cout << player.posX << " " << player.posY << endl;
}
else
cout << " you hit a wall";
break;
case 'd':
update = true;
if ( maze[player.posX +1][player.posY] != '#')
{
maze[player.posX][player.posY] = ' ';
player.posX ++;
if ( maze[player.posX ][player.posY] == maze[enemy_horizontal.posX][enemy_horizontal.posY] ||
maze[player.posX ][player.posY] == maze[enemy_vertical.posX][enemy_vertical.posY] ||
maze[player.posX ][player.posY] == maze[enemy_jump.posX][enemy_jump.posY])
{
player_hit();
}
cout << player.posX << " " << player.posY << endl;
}
else
cout << " you hit a wall";
break;
}
switch (random_move) // the horizontal moving enemy H moves in a random pattern do to the rand() function and thereby making him unpredictable.
{
case 1:
if
(maze[enemy_horizontal.posX +1][enemy_horizontal.posY] != '#')
{
maze[enemy_horizontal.posX][enemy_horizontal.posY] = ' ';
enemy_horizontal.posX ++;
}
break;
case 2:
if
(maze[enemy_horizontal.posX -1][enemy_horizontal.posY] != '#')
{
maze[enemy_horizontal.posX][enemy_horizontal.posY] = ' ';
enemy_horizontal.posX --;
}
break;
}
if (maze[enemy_vertical.posX][enemy_vertical.posY +1] != '#')// the vertical moving enemy he returns to his stsarting point if he híts a wall, seeing that he only moves in one simple pattern (downwards) he is predictable.
{
maze[enemy_vertical.posX][enemy_vertical.posY] = ' ';
enemy_vertical.posY ++;
}
else if
(maze[enemy_vertical.posX][enemy_vertical.posY -5] != '#')
{
maze[enemy_vertical.posX][enemy_vertical.posY] = ' ';
enemy_vertical.posY -=6;
}
if (maze[enemy_jump.posX +2][enemy_jump.posY] != '#')// this enemy jumps every seconf tile over, so you have to be aware of where he is and the location there have been printed out in order to avoid him.
// he also returns to his starting point when he can't get any further do to #
{
maze[enemy_jump.posX][enemy_jump.posY] = ' ';
enemy_jump.posX += 2;
}
else if
(maze[enemy_jump.posX -14][enemy_jump.posY] != '#')
{
maze[enemy_jump.posX][enemy_jump.posY] = ' ';
enemy_jump.posX -= 14;
}
if (player.hp == 0)// when the player rund out of hp the ganmes stops and print out you have lost the game.
{
move_key = 'q';
cout <<" you have lost the game ";
}
if (player.posY == 17)// when the player reach the bottom line he winsd the game
{
cout << " congrats!!! you have won the game ";
}
}// while loop ends here.
return 0;
}