I'm not exactly sure what is wrong with my code. It will run and then it stops without completing the maze. Any ideas?
Thanks in advance.
Here is my code:
#include <iostream>
#include <ctime>
using namespace std;
void mazeTraverse(char [][12], int, int);
int main()
{
char maze[12][12] =
{
'#','#','#','#','#','#','#','#','#','#','#','#',
'#','.','.','.','#','.','.','.','.','.','.','#',
'.','.','#','.','#','.','#','#','#','#','.','#',
'#','#','#','.','#','.','.','.','.','#','.','#',
'#','.','.','.','.','#','#','#','.','#','.','.',
'#','#','#','#','.','#','.','#','.','#','.','#',
'#','.','.','#','.','#','.','#','.','#','.','#',
'#','#','.','#','.','#','.','#','.','#','.','#',
'#','.','.','.','.','.','.','.','.','#','.','#',
'#','#','#','#','#','#','.','#','#','#','.','#',
'#','.','.','.','.','.','.','#','.','.','.','#',
'#','#','#','#','#','#','#','#','#','#','#','#'
};
mazeTraverse(maze,2,0);
return 0;
}
void mazeTraverse(char m[][12], int posY, int posX)
{
static int update = time(0) % 10;
while(time(0) % 10 != update);
if(time(0) % 10 == 9)
update = 0;
else
update = (time(0) % 10) + 1;
m[posY][posX] = 'X';
system("cls");
//print maze
for(int row = 0; row < 12; row++)
{
for(int col = 0; col < 12; col++)
cout << m[row][col];
cout << '\n';
}
if(m[posY][posX] == 'E')
{
cout << "\n FOUND IT!! \n";
return;
}
else
{
while(m[posY][posX] != 'E')
{
if(m[posY-1][posX] != '#')
{
mazeTraverse(m, posY-1, posX);
}
else if(m[posY-1][posX] == '#' && m[posY][posX+1] != '#')
{
mazeTraverse(m, posY, posX+1);
}
if(m[posY][posX+1] != '#')
{
mazeTraverse(m, posY, posX+1);
}
else if(m[posY][posX+1] == '#' && m[posY+1][posX] != '#')
{
mazeTraverse(m, posY+1, posX);
}
if(m[posY+1][posX] != '#')
{
mazeTraverse(m, posY+1, posX);
}
else if(m[posY+1][posX] == '#' && m[posY][posX-1] != '#')
{
mazeTraverse(m, posY, posX-1);
}
if(m[posY][posX-1] != '#')
{
mazeTraverse(m, posY, posX-1);
}
else if(m[posY][posX-1] == '#' && m[posY-1][posX] != '#')
{
mazeTraverse(m, posY-1, posX);
}
m[posY][posX] = '.';
return;
}
}
}