Answered step by step
Verified Expert Solution
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 ;
const int COLS ;
void printMazeconst char mazeCOLS int xLoc, int yLoc;
bool isValidMoveconst char mazeCOLS int x int y;
void moveRightint& x int& y int& facing;
void moveForwardint& x int& y int& facing;
void moveLeftint& x int& y int& facing;
void moveBackwardint& x int& y int& facing;
int main
char mazeROWSCOLS
############
###
#######
######
#####
########
######
#######
###
##########
###
############;
int xLoc ; Starting point
int yLoc ;
int facing ; up right, down, left
while true
printMazemaze xLoc, yLoc;
if xLoc && yLoc
cout "Congratulations! The maze has been solved.
;
break;
Try to move forward
moveForwardxLoc yLoc, facing;
if isValidMovemaze xLoc, yLoc
mazexLocyLoc;
continue;
Try to move right
moveRightxLoc yLoc, facing;
if isValidMovemaze xLoc, yLoc
mazexLocyLoc;
continue;
Try to move left
moveLeftxLoc yLoc, facing;
if isValidMovemaze xLoc, yLoc
mazexLocyLoc;
continue;
If unable to move in any direction, backtrack
moveBackwardxLoc yLoc, facing;
mazexLocyLoc;
return ;
void printMazeconst char mazeCOLS int xLoc, int yLoc
for int i ; i ROWS; i
for int j ; j COLS; j
if i xLoc && j yLoc
cout X;
else
cout mazeij;
cout
;
cout
Hit return to see next move
;
cin.get;
bool isValidMoveconst char mazeCOLS int x int y
return x && x ROWS && y && y COLS && mazexy#;
void moveRightint& x int& y int& facing
switch facing
case : up
y;
break;
case : right
x;
break;
case : down
y;
break;
case : left
x;
break;
void moveForwardint& x int& y int& facing
switch facing
case : up
x;
break;
case : right
y;
break;
case : down
x;
break;
case : left
y;
break;
void moveLeftint& x int& y int& facing
switch facing
case : up
y;
break;
case : right
x;
break;
case : down
y;
break;
case : left
x;
break;
void moveBackwardint& x int& y int& facing
switch facing
case : up
x;
break;
case : right
y;
break;
case : down
x;
break;
case : left
y;
break;
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started