Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

fix the following code using the right hand rule in C + + so the rat would seceesfully exit the maze: #include using namespace std;

fix the following code using the right hand rule in C++ so the rat would seceesfully exit the maze: #include
using namespace std;
const int ROWS =12;
const int COLS =12;
void printMaze(const char maze[][COLS], int xLoc, int yLoc);
bool isValidMove(const char maze[][COLS], int x, int y);
void moveRight(int& x, int& y, int& facing);
void moveForward(int& x, int& y, int& facing);
void moveLeft(int& x, int& y, int& facing);
void moveBackward(int& x, int& y, int& facing);
int main()
{
char maze[ROWS][COLS]=
{{'#','#','#','#','#','#','#','#','#','#','#','#'},
{'#','.','.','.','#','.','.','.','.','.','.','#'},
{'.','.','#','.','#','.','#','#','#','#','.','#'},
{'#','#','#','.','#','.','.','.','.','#','.','#'},
{'#','.','.','.','.','#','#','#','.','#','.','.'},
{'#','#','#','#','.','#','.','#','.','#','.','#'},
{'#','.','.','#','.','#','.','#','.','#','.','#'},
{'#','#','.','#','.','#','.','#','.','#','.','#'},
{'#','.','.','.','.','.','.','.','.','#','.','#'},
{'#','#','#','#','#','#','.','#','#','#','.','#'},
{'#','.','.','.','.','.','.','#','.','.','.','#'},
{'#','#','#','#','#','#','#','#','#','#','#','#'}};
int xLoc =2; // Starting point
int yLoc =0;
int facing =1; //0- up,1- right, 2- down, 3- left
while (true){
printMaze(maze, xLoc, yLoc);
if (xLoc ==4 && yLoc ==11){
cout << "Congratulations! The maze has been solved.
";
break;
}
// Try to move forward
moveForward(xLoc, yLoc, facing);
if (isValidMove(maze, xLoc, yLoc)){
maze[xLoc][yLoc]='*';
continue;
}
// Try to move right
moveRight(xLoc, yLoc, facing);
if (isValidMove(maze, xLoc, yLoc)){
maze[xLoc][yLoc]='*';
continue;
}
// Try to move left
moveLeft(xLoc, yLoc, facing);
if (isValidMove(maze, xLoc, yLoc)){
maze[xLoc][yLoc]='*';
continue;
}
// If unable to move in any direction, backtrack
moveBackward(xLoc, yLoc, facing);
maze[xLoc][yLoc]='-';
}
return 0;
}
void printMaze(const char maze[][COLS], int xLoc, int yLoc)
{
for (int i =0; i < ROWS; ++i){
for (int j =0; j < COLS; ++j){
if (i == xLoc && j == yLoc)
cout <<'X'<<'';
else
cout << maze[i][j]<<'';
}
cout <<'
';
}
cout <<"
Hit return to see next move
";
cin.get();
}
bool isValidMove(const char maze[][COLS], int x, int y)
{
return (x >=0 && x < ROWS && y >=0 && y < COLS && maze[x][y]!='#');
}
void moveRight(int& x, int& y, int& facing)
{
switch (facing){
case 0: // up
y++;
break;
case 1: // right
x++;
break;
case 2: // down
y--;
break;
case 3: // left
x--;
break;
}
}
void moveForward(int& x, int& y, int& facing)
{
switch (facing){
case 0: // up
x--;
break;
case 1: // right
y++;
break;
case 2: // down
x++;
break;
case 3: // left
y--;
break;
}
}
void moveLeft(int& x, int& y, int& facing)
{
switch (facing){
case 0: // up
y--;
break;
case 1: // right
x--;
break;
case 2: // down
y++;
break;
case 3: // left
x++;
break;
}
}
void moveBackward(int& x, int& y, int& facing)
{
switch (facing){
case 0: // up
x++;
break;
case 1: // right
y--;
break;
case 2: // down
x--;
break;
case 3: // left
y++;
break;
}
}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions