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;
		}
	}
}

Recommended Answers

All 2 Replies

Umm you know showing us what the solved maze would look like or what the maze itself would look like would really help us understand the problem and visualize the maze and where it gets stuck...
For the guys who have a tough time visualizing the maze: [Img]http://i.imgur.com/Lw5uO.png[/img]

Hmm I see what u mean.. it gets stuck at position 4.. I'll post back if I solve it.

Hey I did a little debugging just now about 5 minutes ago.. and the problem is as expected of course.. first I re-wrote all your blocking detection to see if your blocked or not.. and I got stuck on the third spot.. then I copy pasted your code and got stuck on the sixth spot.. there was an obvious reason behind it..

Try changing the code to the stuff below.. wait 30 seconds and u will see exactly why your program doesn't do what you want.. trust me the program is still running, the position of the X is changing.. but because you left it as X, it isn't noticeable..

int i = 0;  //just do it.. put this global variable right before the function below..

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;

    i= i+1;

	m[posY][posX] = i;

U will notice some messed up characters! then on the 6-9th try, the maze will get messed up.. ignore it!! the maze goes back to normal and u see the problem clearly! You should notice that the characters just keep going back and forth! That is the problem.. It's not that the X isn't moving, but rather it keeps going back and forth in the same two positions.

I don't really like the algorithm used because it's hard to implement in a good maze solver.. I'd probably use something like A* or something. Iunno but I can't seem to solve your problem of going back and forth.. I tried changing the char to '#' and it goes further.. so u should refine your solution to solving it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.