Question
TRAVERSAL MAZE: Im tring to create a program that replaces . with X with a recursive function ,thereby proving that I finished the maze. I
TRAVERSAL MAZE:
Im tring to create a program that replaces "." with "X" with a recursive function ,thereby proving that I finished the maze. I feel likim messing up in the recursive part of the function. it calls the function like a infinite time and it only calls the first if statement and doestn go down.
#include
#include
#include
using namespace std;
string mazeTraverse(string[12][12], string*, int*, int*);
int main()
{
string maze[12][12] = {
{ "#","#","#","#","#","#", "#","#","#" ,"#","#","#" },
{ "#",".",".",".","#",".",".",".",".",".",".","#" },
{ ".", ".","#",".","#",".","#","#","#","#",".","#" },
{ "#", "#","#",".","#",".",".",".",".","#",".","#" },
{ "#", "#","#","#",".","#",".","#",".","#",".","." },
{ "#", ".",".","#",".","#",".","#",".","#",".","#" },
{ "#", "#",".","#",".","#",".","#",".","#",".","#" },
{ "#", ".",".",".",".",".",".",".",".","#",".","#" },
{ "#", "#","#","#","#","#",".","#","#","#",".","#" },
{ "#", ".",".",".",".",".",".","#",".",".",".","#" },
{ "#", "#","#","#","#","#","#","#","#","#","#","#" } };
string start = maze[4][11];
string *Wptr;
Wptr = &start;
int row = 4;
int col = 11;
int *R = &row;
int *C = &col;
mazeTraverse(maze, Wptr, R, C);
system("pause");
return 0;
}
string mazeTraverse(string MAZE[12][12], string *startingPoint, int * Row, int*Col) {
if (startingPoint == &MAZE[2][0]) { return MAZE[2][0]; }
else
if (*startingPoint == ".") {
MAZE[*Row][*Col] = "X";
for (int i = 0; i < 12; i++) {
for (int j = 0; j < 12; j++) {
cout << MAZE[i][j] << " ";
}
cout << endl;
}
}
else if (MAZE[*Row][*Col-1] == ".") {
MAZE[*Row][*Col - 1] = "X";
for (int i = 0; i < 12; i++) {
for (int j = 0; j < 12; j++) {
cout << MAZE[i][j] << " ";
}
cout << endl;
}
}
else if (*startingPoint == ".") {
*startingPoint =
MAZE[*Row-1][*Col] = "X";
for (int i = 0; i < 12; i++) {
for (int j = 0; j < 12; j++) {
cout << MAZE[i][j] << " ";
}
cout << endl;
}
// Movies through the maze down
}else if (MAZE[*Row + 1][*Col] == ".") {
MAZE[*Row+1][*Col] = "X";
for (int i = 0; i < 12; i++) {
for (int j = 0; j < 12; j++) {
cout << MAZE[i][j] << " ";
}
cout << endl;
}
}
// moves through the maze up.
else if (MAZE[*Row][*Col+1] == ".") {
MAZE[*Row][*Col+1] = "X";
for (int i = 0; i < 12; i++) {
for (int j = 0; j < 12; j++) {
cout << MAZE[i][j] << " ";
}
cout << endl;
}
}
return mazeTraverse(MAZE, startingPoint, Row, Col);
}
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