Maze solving algorithm issue

My code works to a point. It will go then it stop. I'm not sure why. Any Ideas?

Thanks in advance.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#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
	{
		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;
	}
}
Last edited on
Topic archived. No new replies allowed.