Maze Traverse

Hello, I've been trying to write this program for my Object Oriented Class with C++, but I can't figure out how to solve this problem. So I have my code here, I was supposed to write a recursive wall follower function that always follows the right side of a wall and leaves an 'X' behind so that we see where the traverse is moving. Well, I didn't use the right hand rule, instead, I used an algorithm that checks the spaces to the south, west, north and finally east, every time in the same order and then moves one space if there is a spot to move there until it finds the exit, here is the program (I commented out the part where the problem lies)
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// Maze Traverse.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
using namespace std;

void printMaze(char maze[][12]);
void mazeTraverse(char maze[][12], int startPointY, int startPointX);

int _tmain(int argc, _TCHAR* argv[])
{
	int startPointY = 2;
	int startPointX = 0;
	char maze[12][12] = 
	{   { '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#' }, 
		{ '#', '.', '.', '.', '#', '.', '.', '.', '.', '.', '.', '#' },
		{ '.', '.', '#', '.', '#', '.', '#', '#', '#', '#', '.', '#' }, 
		{ '#', '#', '#', '.', '#', '.', '.', '.', '.', '#', '.', '#' },
		{ '#', '.', '.', '.', '.', '#', '#', '#', '.', '#', '.', '.' }, 
		{ '#', '#', '#', '#', '.', '#', '.', '#', '.', '#', '.', '#' },
		{ '#', '.', '.', '#', '.', '#', '.', '#', '.', '#', '.', '#' }, 
		{ '#', '#', '.', '#', '.', '#', '.', '#', '.', '#', '.', '#' },
		{ '#', '.', '.', '.', '.', '.', '.', '.', '.', '#', '.', '#' }, 
		{ '#', '#', '#', '#', '#', '#', '.', '#', '#', '#', '.', '#' },
		{ '#', '.', '.', '.', '.', '.', '.', '#', '.', '.', '.', '#' }, 
		{ '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#' }
	};

	printMaze(maze);

	system("Pause");
	mazeTraverse(maze, startPointY, startPointX);

	system("Pause");
	return 0;
}


void printMaze(char maze[][12])
{
	for (int i = 0; i < 12; i++)
	{
		for (int j = 0; j < 12; j++)
		{
			cout << maze[i][j];
		}
		cout << endl;
	}
	cout << endl;
}

void mazeTraverse(char maze[][12], int startPointY, int startPointX)
{

	int previousPointY;
	int previousPointX;
	int currentPointY = startPointY;
	int currentPointX = startPointX;

	maze[startPointY][startPointX] = 'X';
	previousPointX = startPointX;
	previousPointY = startPointY;
	

	if(currentPointX != 11 || currentPointY != 4)
	{
		if (maze[currentPointY + 1][currentPointX] == '.') // Looks south first, if there is space to move, moves that way, marks previous space with X and moves on.
		{
			maze[currentPointY][currentPointX] = 'X';
			currentPointY = currentPointY + 1;
			currentPointX = currentPointX;
		}

		if (maze[currentPointY + 1][currentPointX] != '.')
		{
			if (maze[currentPointY][currentPointX + 1] == '.') // Looks west next, if there is space to move, moves that way, marks previous space with X and moves on.
			{
				maze[currentPointY][currentPointX] = 'X';
				currentPointY = currentPointY;
				currentPointX = currentPointX + 1;
			}
			if (maze[currentPointY][currentPointX + 1] != '.')
			{
				if (maze[currentPointY - 1][currentPointX] == '.') // Looks north next, if there is space to move, moves that way, marks previous space with X and moves on.
				{
					maze[currentPointY][currentPointX] = 'X';
					currentPointY = currentPointY - 1;
					currentPointX = currentPointX;
				}
				if (maze[currentPointY - 1][currentPointX] != '.')
				{
					if (maze[currentPointY][currentPointX - 1] == '.') // Looks east last, if there is space to move, moves that way, marks previous space with X and moves on.
					{
						maze[currentPointY][currentPointX] = 'X';
						currentPointY = currentPointY;
						currentPointX = currentPointX - 1;
					}

				}
			}
		}
		/* if (maze[currentPointY - 1][currentPointX] != '.')
		{
			if (maze[currentPointY + 1][currentPointX] == 'X') // Looks south first, if there is space to move, moves that way, marks previous space with X and moves on.
			{
				maze[currentPointY][currentPointX] = 'X';
				currentPointY = currentPointY + 1;
				currentPointX = currentPointX;
			}

			else if (maze[currentPointY][currentPointX + 1] == 'X') // Looks west next, if there is space to move, moves that way, marks previous space with X and moves on.
			{
				maze[currentPointY][currentPointX] = 'X';
				currentPointY = currentPointY;
				currentPointX = currentPointX + 1;
			}
			else if (maze[currentPointY - 1][currentPointX] == 'X') // Looks north next, if there is space to move, moves that way, marks previous space with X and moves on.
			{
				maze[currentPointY][currentPointX] = 'X';
				currentPointY = currentPointY - 1;
				currentPointX = currentPointX;
			}

			else if (maze[currentPointY][currentPointX - 1] == 'X') // Looks east last, if there is space to move, moves that way, marks previous space with X and moves on.
			{
				maze[currentPointY][currentPointX] = 'X';
				currentPointY = currentPointY;
				currentPointX = currentPointX - 1;
			}
		}*/
		system("CLS");
		printMaze(maze);

		mazeTraverse(maze, currentPointY, currentPointX);

	}
	system("CLS");
	printMaze(maze);

}



# # # # # # # # # # # #  
# . . . # . . . . . . # 
. . # . # . # # # # . # 
# # # . # . . . . # . #
# . . . . # # # . # . . 
# # # # . # . # . # . #
# . . # . # . # . # . # 
# # . # . # . # . # . #
# . . . . . . . . # . # 
# # # # # # . # # # . #
# . . . . . . # . . . # 
# # # # # # # # # # # #
Press any key to continue...

*Presses any key*

# # # # # # # # # # # #  
# X X X # . . . . . . # 
X X # X # . # # # # . # 
# # # X # . . . . # . #
# . . X X # # # . # . . 
# # # # X # . # . # . #
# . . # X # . # . # . # 
# # . # X # . # . # . #
# . . . X X X . . # . # 
# # # # # # X # # # . #
# X X X X X X # . . . # 
# # # # # # # # # # # #


The part I commented out was supposed to get me out of the place I am stuck by having the recursive function go through the X's in case there was no .'s, but it just makes it worse... This is the part of the code that is wrong and the whole output when its implemented to the whole code.

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
 if (maze[currentPointY - 1][currentPointX] != '.')
		{
			if (maze[currentPointY + 1][currentPointX] == 'X') // Looks south first, if there is space to move, moves that way, marks previous space with X and moves on.
			{
				maze[currentPointY][currentPointX] = 'X';
				currentPointY = currentPointY + 1;
				currentPointX = currentPointX;
			}

			else if (maze[currentPointY][currentPointX + 1] == 'X') // Looks west next, if there is space to move, moves that way, marks previous space with X and moves on.
			{
				maze[currentPointY][currentPointX] = 'X';
				currentPointY = currentPointY;
				currentPointX = currentPointX + 1;
			}
			else if (maze[currentPointY - 1][currentPointX] == 'X') // Looks north next, if there is space to move, moves that way, marks previous space with X and moves on.
			{
				maze[currentPointY][currentPointX] = 'X';
				currentPointY = currentPointY - 1;
				currentPointX = currentPointX;
			}

			else if (maze[currentPointY][currentPointX - 1] == 'X') // Looks east last, if there is space to move, moves that way, marks previous space with X and moves on.
			{
				maze[currentPointY][currentPointX] = 'X';
				currentPointY = currentPointY;
				currentPointX = currentPointX - 1;
			}
		}


# # # # # # # # # # # #  
# X X . # . . . . . . # 
X X # . # . # # # # . # 
# # # . # . . . . # . #
# . . . . # # # . # . . 
# # # # . # . # . # . #
# . . # . # . # . # . # 
# # . # . # . # . # . #
# . . . . . . . . # . # 
# # # # # # . # # # . #
# . . . . . . # . . . # 
# # # # # # # # # # # #


and it gets stuck in an infinite loop right there... I have no idea what to do now or what's causing the problem. Any help will be appreciated. Thank you so much.
Topic archived. No new replies allowed.