The end product is generating a maze and playing the maze, that is, finding a wa
ID: 3838323 • 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. you do not finish the whole problem, please indicate which step you are at. This will make grading much easier. step o 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 print0 that will print to the screen: cout o: Step 2Explanation / Answer
The explanation of the maze game with comments is given in the code below :
code :
#include <iostream>
using std::endl;
using std::cout;
using std::cin;
#include <string>
using std::string;
#include<Windows.h>
#include<conio.h>
void wel();
char getKeyPresses();
void printLev(int);
void setMee(int);
bool isExit(int, int, int);
bool isWall(int, int, int);
int getPos(int, int&);
int getX(int, int &);
void updates(int, int, int);
void makeSpaces(int, int, int);
const char space = ' ';
const char me = '@';
char lvl1[15][15] = { { '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#' },
{ 'X', ' ', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#' },
{ '#', ' ', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#' },
{ '#', ' ', ' ', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#' },
{ '#', '#', ' ', ' ', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#' },
{ '#', '#', '#', ' ', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#' },
{ '#', '#', '#', ' ', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#' },
{ '#', '#', '#', ' ', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#' },
{ '#', '#', '#', ' ', '#', '#', '#', '#', '#', ' ', ' ', ' ', ' ', ' ', 'O' },
{ '#', '#', '#', ' ', '#', '#', '#', '#', '#', ' ', '#', '#', '#', '#', '#' },
{ '#', '#', '#', ' ', ' ', ' ', '#', '#', '#', ' ', '#', '#', '#', '#', '#' },
{ '#', '#', '#', '#', '#', ' ', '#', '#', '#', ' ', '#', '#', '#', '#', '#' },
{ '#', '#', '#', '#', '#', ' ', '#', '#', '#', ' ', '#', '#', '#', '#', '#' },
{ '#', '#', '#', '#', '#', ' ', ' ', ' ', ' ', ' ', '#', '#', '#', '#', '#' },
{ '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#' } };
char lvl2[15][15] = { { '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#' },
{ '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#' },
{ '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#' },
{ '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#' },
{ '#', ' ', ' ', ' ', ' ', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#' },
{ '#', ' ', '#', '#', ' ', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#' },
{ '#', ' ', '#', '#', ' ', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#' },
{ '#', ' ', ' ', '#', ' ', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#' },
{ '#', '#', ' ', '#', ' ', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#' },
{ '#', '#', ' ', '#', ' ', '#', '#', '#', '#', '#', '#', '#', '#', ' ', 'O' },
{ '#', '#', ' ', '#', ' ', '#', '#', '#', '#', '#', '#', '#', '#', ' ', '#' },
{ '#', '#', ' ', '#', ' ', '#', '#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#' },
{ '#', '#', ' ', '#', ' ', '#', ' ', ' ', '#', '#', '#', '#', '#', '#', '#' },
{ 'X', ' ', ' ', '#', ' ', ' ', ' ', '#', '#', '#', '#', '#', '#', '#', '#' },
{ '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#' } };
int main(void){
wel();
begin:
system("CLS");
cout << " ";
cout << "PLEASE SELECT A LEVEL 1---2---3---4---5---6 ";
int lvl;
cin >> lvl;
if (lvl != 2 && lvl != 1 && lvl > 0){
cout << endl << endl;
cout << " ";
cout << "Level not available yet, please check back later." << endl;
Sleep(1000);
goto begin;
}
system("CLS");
setMee(lvl);
printLev(lvl);
int x, y;
while (1){
char move = getKeyPresses();
switch (move){
case 'u':
x = getPos(lvl, y);
if (!isWall(x - 1, y, lvl)){
if (isExit(x - 1, y, lvl)){
system("CLS");
cout << "You Win!" << endl;
Sleep(2000);
goto begin;;
}
system("CLS");
makeSpaces(lvl, x, y);
updates(lvl, x - 1, y);
}
break;
case 'd':
x = getPos(lvl, y);
if (!isWall(x + 1, y, lvl)){
if (isExit(x + 1, y, lvl)){
system("CLS");
cout << "You Win!" << endl;
Sleep(2000);
goto begin;;
}
system("CLS");
makeSpaces(lvl, x, y);
updates(lvl, x + 1, y);
}
break;
case 'l':
x = getPos(lvl, y);
if (!isWall(x, y - 1, lvl)){
if (isExit(x, y - 1, lvl)){
system("CLS");
cout << "You Win!" << endl;
Sleep(2000);
goto begin;;
}
system("CLS");
makeSpaces(lvl, x, y);
updates(lvl, x, y - 1);
}
break;
case 'r':
x = getPos(lvl, y);
if (!isWall(x, y + 1, lvl)){
if (isExit(x, y + 1, lvl)){
system("CLS");
cout << "You Win!" << endl;
Sleep(2000);
goto begin;;
}
system("CLS");
makeSpaces(lvl, x, y);
updates(lvl, x, y + 1);
}
break;
default:
break;
}
}
return 0;
}
void wel(){
string start = "wel TO MAZE RUNNER ";
string indev = "only two levels available.";
string howto = "Use the arrow keys and traverse through the maze. Exit is marked 'O' ok.";
cout << endl;
cout << " ";
for (auto ch : start){
cout << ch;
Sleep(40);
} cout << endl << endl;
cout << " ";
for (auto ch : indev){
cout << ch;
Sleep(40);
} cout << endl << endl << endl;
cout << " ";
for (auto ch : howto){
cout << ch;
Sleep(40);
}
Sleep(1500);
}
void printLev(int lvl){
cout << " ";
if (lvl == 1){
for (int i = 0; i != 15; ++i){
cout << endl << " ";
for (int j = 0; j != 15; ++j){
cout << lvl1[i][j];
}
} cout << endl;
}
if (lvl == 2){
for (int i = 0; i != 15; ++i){
cout << endl << " ";
for (int j = 0; j != 15; ++j){
cout << lvl2[i][j];
}
} cout << endl;
}
}
void setMee(int lvl){
int x, y;
if (lvl == 1){
x = getX(lvl, y);
lvl1[x][y] = me;
}
if (lvl == 2){
x = getX(lvl, y);
lvl2[x][y] = me;
}
}
//got this function from a CPP forum
char getKeyPresses(){
char key = 127;
key = _getch();
if (key == 0 || key == -32){
key = _getch();
if (key == 72) {
key = 'u';
} else if (key == 75){
key = 'l';
} else if (key == 77){
key = 'r';
} else if (key == 80){
key = 'd';
}
}
return key;
}
bool isExit(int x, int y, int lvl){
if (lvl == 1){
if (lvl1[x][y] == 'O'){
return true;
}
else {
return false;
}
}
if (lvl == 2){
if (lvl2[x][y] == 'O'){
return true;
}
else {
return false;
}
}
return true;
}
int getPos(int lvl, int &y){
int xCoord;
if (lvl == 1){
for (int i = 0; i != 15; ++i){
for (int j = 0; j != 15; ++j){
if (lvl1[i][j] == '@'){
xCoord = i;
y = j;
return xCoord;
}
}
}
}
if (lvl == 2){
for (int i = 0; i != 15; ++i){
for (int j = 0; j != 15; ++j){
if (lvl2[i][j] == '@'){
xCoord = i;
y = j;
return xCoord;
}
}
}
}
return 0;
}
bool isWall(int x, int y, int lvl){
if (lvl == 1){
if (lvl1[x][y] == '#'){
cout << " Cannot move! That is a wall / boundary.";
Sleep(400);
system("CLS");
printLev(lvl);
return true;
}
else {
return false;
}
}
if (lvl == 2){
if (lvl2[x][y] == '#'){
cout << " Cannot move! That is a wall / boundary.";
Sleep(400);
system("CLS");
printLev(lvl);
return true;
}
else {
return false;
}
}
return true;
}
int getX(int lvl, int &y){
int xCoord;
if (lvl == 1){
for (int i = 0; i != 15; ++i){
for (int j = 0; j != 15; ++j){
if (lvl1[i][j] == 'X'){
xCoord = i;
y = j;
return xCoord;
}
}
}
}
if (lvl == 2){
for (int i = 0; i != 15; ++i){
for (int j = 0; j != 15; ++j){
if (lvl2[i][j] == 'X'){
xCoord = i;
y = j;
return xCoord;
}
}
}
}
return 0;
}
void updates(int lvl, int x, int y){
if (lvl == 1){
lvl1[x][y] = me;
printLev(lvl);
}
if (lvl == 2){
lvl2[x][y] = me;
printLev(lvl);
}
}
void makeSpaces(int lvl, int x, int y){
if (lvl == 1){
lvl1[x][y] = space;
}
if (lvl == 2){
lvl2[x][y] = space;
}
}
Sample output :
the game will be runned clearly
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.