Maze Solver (Right Hand rule)

Pages: 12
If you pass a 2D array to a function, you must specify all sizes after the first.

myArray(int array[][10]) <- Fine
myArray(int array[][]) <- Not fine
myArray(int array[10][]) <- Also not fine

For your errors:

Redefinition means you probably already named a variable that.

char x;
int x; <-- Redefinition

The other errors have several possible causes. I suggest you quote one line of code per error, along with the error. e.g.

My compiler says I have an error on line 46.

quote line 46 here.

put error here


Do this once for each error type. That is of course, if you can't Google the error code and work it out for yourself.





Last edited on
here my fixed new code, unfortunately there's still 40 errors, and i included the errors in each line.


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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include <iostream>
using namespace std;

const int SIZE = 12;
	char maze[SIZE][SIZE]={
		{'#','#','#','#','#','#','#','#','#','#','#','#'},
		{'#','.','.','.','#','.','.','.','.','.','.','#'},
		{'.','.','#','.','#','.','#','#','#','#','.','#'},
		{'#','#','#','.','#','.','.','.','.','#','.','#'},
		{'#','.','.','.','.','#','#','#','.','#','.','.'},
		{'#','#','#','#','.','#','.','#','.','#','.','#'},
		{'#','.','.','#','.','#','.','#','.','#','.','#'},
		{'#','#','.','#','.','#','.','#','.','#','.','#'},
		{'#','.','.','.','.','.','.','.','.','#','.','#'},
		{'#','#','#','#','#','#','.','#','#','#','.','#'},
		{'#','.','.','.','.','.','.','#','.','.','.','#'},
		{'#','#','#','#','#','#','#','#','#','#','#','#'},
	};

bool checkpath(int X, int Y){
		
		//turn west
		if(maze[X][Y-1]=='.'){
			return true;}

		//turn north
		if(maze[X][Y-1]!='.' && maze[X+1][Y]!='.' && maze[X][Y+1]!='.' && maze[X-1][Y]=='.'){
			return true;}

		//turn south
		if(maze[X][Y-1]!='.' && maze[X+1][Y]=='.'){
			return true;}

		//turn east
		if(maze[X][Y-1]!='.' && maze[X+1][Y]!='.' && maze[X][Y+1]=='.'){
			return true;}
	

}


int findpath(int x, int y){


		//turn west
		if(maze[x][y-1]=='.'){
			return 1;}

		//turn north
		if(maze[x][y-1]!='.' && maze[x+1][y]!='.' && maze[x][y+1]!='.' && maze[x-1][y]=='.'){
			return 2;}

		//turn south
		if(maze[x][y-1]!='.' && maze[x+1][y]=='.'){
			return 3;}

		//turn east
		if(maze[x][y-1]!='.' && maze[x+1][y]!='.' && maze[x][y+1]=='.'){
			return 4;}
}


int main(){

	
	int startX = 2;
	int startY = 0;
	int endX = 4;
	int endY = 11;
	int i,j;
	bool checkpath;
	int findpath;

	
	while(startX<=endX && startY<=endY){

//	21	IntelliSense: expression preceding parentheses of apparent call must have (pointer-to-) function type
	if(startX>0 && checkpath(startX, startY)==true){ //Error	1	error C2064: term does not evaluate to a function taking 2 arguments
//	22	IntelliSense: expression preceding parentheses of apparent call must have (pointer-to-) function type
		if(findpath(startX , startY)==1){ //Error	2	error C2064: term does not evaluate to a function taking 2 arguments
			maze[startX][startY]='X';
			cout << maze[startX][startY];
			startY-=1;
		}//	23	IntelliSense: expression preceding parentheses of apparent call must have (pointer-to-) function type
		else if(findpath(startX , startY)==2){ //Error	3	error C2064: term does not evaluate to a function taking 2 arguments
			maze[startX][startY]='X';
			cout << maze[startX][startY];
			startX-=1;
		}//	24	IntelliSense: expression preceding parentheses of apparent call must have (pointer-to-) function type
		else if(findpath(startX , startY)==3){ //Error	4	error C2064: term does not evaluate to a function taking 2 arguments
			maze[startX][startY]='X';
			cout << maze[startX][startY];
			startX+=1;
		}//	25	IntelliSense: expression preceding parentheses of apparent call must have (pointer-to-) function type
		else if(findpath(startX , startY)==4){ //Error	5	error C2064: term does not evaluate to a function taking 2 arguments
			maze[startX][startY]='X';
			cout << maze[startX][startY];
			startY+=1;
		}
	}//	26	IntelliSense: expression preceding parentheses of apparent call must have (pointer-to-) function type
	if(startX<13 && checkpath(startX, startY)==true){ //Error	6	error C2064: term does not evaluate to a function taking 2 arguments
		//	27	IntelliSense: expression preceding parentheses of apparent call must have (pointer-to-) function type
if(findpath(startX , startY)==1){ //Error	7	error C2064: term does not evaluate to a function taking 2 arguments
			maze[startX][startY]='X';
			cout << maze[startX][startY];
			startY-=1;
		}//	28	IntelliSense: expression preceding parentheses of apparent call must have (pointer-to-) function type
		else if(findpath(startX , startY)==2){ //Error	8	error C2064: term does not evaluate to a function taking 2 arguments
			maze[startX][startY]='X';
			cout << maze[startX][startY];
			startX-=1;
		}//	29	IntelliSense: expression preceding parentheses of apparent call must have (pointer-to-) function type
		else if(findpath(startX , startY)==3){ //Error	9	error C2064: term does not evaluate to a function taking 2 arguments
			maze[startX][startY]='X';
			cout << maze[startX][startY];
			startX+=1;
		}//	30	IntelliSense: expression preceding parentheses of apparent call must have (pointer-to-) function type
		else if(findpath(startX , startY)==4){ //Error	10	error C2064: term does not evaluate to a function taking 2 arguments
			maze[startX][startY]='X';
			cout << maze[startX][startY];
			startY+=1;
		}
	}//	31	IntelliSense: expression preceding parentheses of apparent call must have (pointer-to-) function type
	if(startY>0 && checkpath(startX, startY)==true){ //Error	11	error C2064: term does not evaluate to a function taking 2 arguments
		//	32	IntelliSense: expression preceding parentheses of apparent call must have (pointer-to-) function type
if(findpath(startX , startY)==1){ //Error	12	error C2064: term does not evaluate to a function taking 2 arguments
			maze[startX][startY]='X';
			cout << maze[startX][startY];
			startY-=1;
		}//	33	IntelliSense: expression preceding parentheses of apparent call must have (pointer-to-) function type
		else if(findpath(startX , startY)==2){ //Error	13	error C2064: term does not evaluate to a function taking 2 arguments
			maze[startX][startY]='X';
			cout << maze[startX][startY];
			startX-=1;
		}//	34	IntelliSense: expression preceding parentheses of apparent call must have (pointer-to-) function type
		else if(findpath(startX , startY)==3){ //Error	14	error C2064: term does not evaluate to a function taking 2 arguments
			maze[startX][startY]='X';
			cout << maze[startX][startY];
			startX+=1;
		}//	35	IntelliSense: expression preceding parentheses of apparent call must have (pointer-to-) function type
		else if(findpath(startX , startY)==4){ //Error	15	error C2064: term does not evaluate to a function taking 2 arguments
			maze[startX][startY]='X';
			cout << maze[startX][startY];
			startY+=1;
		}
	}//	36	IntelliSense: expression preceding parentheses of apparent call must have (pointer-to-) function type
	if(startY<13 && checkpath(startX, startY)==true){ //Error	16	error C2064: term does not evaluate to a function taking 2 arguments
		//	37	IntelliSense: expression preceding parentheses of apparent call must have (pointer-to-) function type
if(findpath(startX , startY)==1){ //Error	17	error C2064: term does not evaluate to a function taking 2 arguments
			maze[startX][startY]='X';
			cout << maze[startX][startY];
			startY-=1;
		}//	38	IntelliSense: expression preceding parentheses of apparent call must have (pointer-to-) function type
		else if(findpath(startX , startY)==2){ //Error	18	error C2064: term does not evaluate to a function taking 2 arguments
			maze[startX][startY]='X';
			cout << maze[startX][startY];
			startX-=1;
		}//	39	IntelliSense: expression preceding parentheses of apparent call must have (pointer-to-) function type
		else if(findpath(startX , startY)==3){ //Error	19	error C2064: term does not evaluate to a function taking 2 arguments
			maze[startX][startY]='X';
			cout << maze[startX][startY];
			startX+=1;
		}//	40	IntelliSense: expression preceding parentheses of apparent call must have (pointer-to-) function type
		else if(findpath(startX , startY)==4){ //Error	20	error C2064: term does not evaluate to a function taking 2 arguments
			maze[startX][startY]='X';
			cout << maze[startX][startY];
			startY+=1;
		}
	}

	}

	
	system("Pause");
}
Mats wrote:
If you pass a 2D array to a function, you must specify all sizes after the first.

This is what I meant to say. I accidentally confused passing arrays with calling functions with predefined parameters. Functions, you only need to specify the undefined parameters, whereas with arrays, you can omit the first size of the array. Not really relevant in this situation, so I'm sorry for the confusion.

From what my compiler is telling me, and what I experimented with, is that you can not use the function names checkpath and findpath because you have a variable with the same exact name. Change one or the other and you'll have no issues (that seems like the only errors I'm getting anyways).

It runs fine when the variables and functions use different names. However, the program doesn't work properly.
it still didin't work, and have exactly the same errors as before. could you please show me the code? thank you
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include <iostream>
using namespace std;

const int SIZE = 12;
char maze[SIZE][SIZE] = {
   {'#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#'},
   {'#', '.', '.', '.', '#', '.', '.', '.', '.', '.', '.', '#'},
   {'.', '.', '#', '.', '#', '.', '#', '#', '#', '#', '.', '#'},
   {'#', '#', '#', '.', '#', '.', '.', '.', '.', '#', '.', '#'},
   {'#', '.', '.', '.', '.', '#', '#', '#', '.', '#', '.', '.'},
   {'#', '#', '#', '#', '.', '#', '.', '#', '.', '#', '.', '#'},
   {'#', '.', '.', '#', '.', '#', '.', '#', '.', '#', '.', '#'},
   {'#', '#', '.', '#', '.', '#', '.', '#', '.', '#', '.', '#'},
   {'#', '.', '.', '.', '.', '.', '.', '.', '.', '#', '.', '#'},
   {'#', '#', '#', '#', '#', '#', '.', '#', '#', '#', '.', '#'},
   {'#', '.', '.', '.', '.', '.', '.', '#', '.', '.', '.', '#'},
   {'#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#'},
};

bool CheckPath(int X, int Y) {
   //turn west
   if(maze[X][Y - 1] == '.') {
      return true;
   }

   //turn north
   if(maze[X][Y - 1] != '.' && maze[X + 1][Y] != '.' && maze[X][Y + 1] != '.' && maze[X - 1][Y] == '.') {
      return true;
   }

   //turn south
   if(maze[X][Y - 1] != '.' && maze[X + 1][Y] == '.') {
      return true;
   }

   //turn east
   if(maze[X][Y - 1] != '.' && maze[X + 1][Y] != '.' && maze[X][Y + 1] == '.') {
      return true;
   }
}

int FindPath(int x, int y) {
   //turn west
   if(maze[x][y - 1] == '.') {
      return 1;
   }

   //turn north
   if(maze[x][y - 1] != '.' && maze[x + 1][y] != '.' && maze[x][y + 1] != '.' && maze[x - 1][y] == '.') {
      return 2;
   }

   //turn south
   if(maze[x][y - 1] != '.' && maze[x + 1][y] == '.') {
      return 3;
   }

   //turn east
   if(maze[x][y - 1] != '.' && maze[x + 1][y] != '.' && maze[x][y + 1] == '.') {
      return 4;
   }
}

int main() {
   int startX = 2;
   int startY = 0;
   int endX = 4;
   int endY = 11;
   int i, j;
   bool checkpath;
   int findpath;

   while(startX <= endX && startY <= endY) {
      if(startX > 0 && CheckPath(startX, startY) == true) {
         if(FindPath(startX , startY) == 1) {
            maze[startX][startY] = 'X';
            cout << maze[startX][startY];
            startY -= 1;
         }
         else if(FindPath(startX , startY) == 2) {
            maze[startX][startY] = 'X';
            cout << maze[startX][startY];
            startX -= 1;
         }
         else if(FindPath(startX , startY) == 3) {
            maze[startX][startY] = 'X';
            cout << maze[startX][startY];
            startX += 1;
         }
         else if(FindPath(startX , startY) == 4) {
            maze[startX][startY] = 'X';
            cout << maze[startX][startY];
            startY += 1;
         }
      }

      if(startX < 13 && CheckPath(startX, startY) == true) {
         if(FindPath(startX , startY) == 1) {
            maze[startX][startY] = 'X';
            cout << maze[startX][startY];
            startY -= 1;
         }
         else if(FindPath(startX , startY) == 2) {
            maze[startX][startY] = 'X';
            cout << maze[startX][startY];
            startX -= 1;
         }
         else if(FindPath(startX , startY) == 3) {
            maze[startX][startY] = 'X';
            cout << maze[startX][startY];
            startX += 1;
         }
         else if(FindPath(startX , startY) == 4) {
            maze[startX][startY] = 'X';
            cout << maze[startX][startY];
            startY += 1;
         }
      }

      if(startY > 0 && CheckPath(startX, startY) == true) {
         if(FindPath(startX , startY) == 1) {
            maze[startX][startY] = 'X';
            cout << maze[startX][startY];
            startY -= 1;
         }
         else if(FindPath(startX , startY) == 2) {
            maze[startX][startY] = 'X';
            cout << maze[startX][startY];
            startX -= 1;
         }
         else if(FindPath(startX , startY) == 3) {
            maze[startX][startY] = 'X';
            cout << maze[startX][startY];
            startX += 1;
         }
         else if(FindPath(startX , startY) == 4) {
            maze[startX][startY] = 'X';
            cout << maze[startX][startY];
            startY += 1;
         }
      }

      if(startY < 13 && CheckPath(startX, startY) == true) {
         if(FindPath(startX , startY) == 1) {
            maze[startX][startY] = 'X';
            cout << maze[startX][startY];
            startY -= 1;
         }
         else if(FindPath(startX , startY) == 2) {
            maze[startX][startY] = 'X';
            cout << maze[startX][startY];
            startX -= 1;
         }
         else if(FindPath(startX , startY) == 3) {
            maze[startX][startY] = 'X';
            cout << maze[startX][startY];
            startX += 1;
         }
         else if(FindPath(startX , startY) == 4) {
            maze[startX][startY] = 'X';
            cout << maze[startX][startY];
            startY += 1;
         }
      }
   }

   system("Pause");
}
Thanks alot, it seems like i didn't understand about declaring functions name can't be the same as declared variable, or there is no need to. But now i understand, still i have to fix the code. May someone give me the hint, i think that i removed the recursive to display the maze. But somehow i am not so sure what to do
finally, my program is working, even though it print out the maze each time. I tried to write the program to print out the maze once, and then print out the 'X', but the problem is : there are errors which can't convert the char to char, i tried using int, and the error changed to can't convert the int to char, or int to int. But now the problem is, my maze isn't running if there's already 'X' placed. What should i do? i tried google but i don't find the answer. If anyone could give me a hint thanks :)

NB: my assigntment already expired, i sent my 90% finished program. But i still want to finish this work.


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
142
143
144
145
146
147
148
#include <iostream>
using namespace std;

const int SIZE = 12;
	char maze[SIZE][SIZE]={
		{'#','#','#','#','#','#','#','#','#','#','#','#'},
		{'#','.','.','.','#','.','.','.','.','.','.','#'},
		{'.','.','#','.','#','.','#','#','#','#','.','#'},
		{'#','#','#','.','#','.','.','.','.','#','.','#'},
		{'#','.','.','.','.','#','#','#','.','#','.','.'},
		{'#','#','#','#','.','#','.','#','.','#','.','#'},
		{'#','.','.','#','.','#','.','#','.','#','.','#'},
		{'#','#','.','#','.','#','.','#','.','#','.','#'},
		{'#','.','.','.','.','.','.','.','.','#','.','#'},
		{'#','#','#','#','#','#','.','#','#','#','.','#'},
		{'#','.','.','.','.','.','.','#','.','.','.','#'},
		{'#','#','#','#','#','#','#','#','#','#','#','#'},
	};


	

bool checkpath(int X, int Y){
		
		//turn west
		if(maze[X][Y-1]=='.'){
			return true;}

		//turn north
		if(maze[X][Y-1]!='.' && maze[X+1][Y]!='.' && maze[X][Y+1]!='.' && maze[X-1][Y]=='.'){
			return true;}

		//turn east
		if(maze[X][Y-1]!='.' && maze[X+1][Y]!='.' && maze[X][Y+1]=='.'){
			return true;}

		//turn south
		if(maze[X][Y-1]!='.' && maze[X+1][Y]=='.'){
			return true;}

		if(maze[X][Y-1]=='X' || maze[X+1][Y]=='X' || maze[X][Y+1]=='X' || maze[X-1][Y]=='X'){
			return true;
			}
	

}


int findpath(int x, int y){


		//turn west
		if(maze[x][y-1]=='.'){
			return 1;}

		//turn north
		if(maze[x][y-1]!='.' && maze[x+1][y]!='.' && maze[x][y+1]!='.' && maze[x-1][y]=='.'){
			return 2;}

		//turn east
		if(maze[x][y-1]!='.' && maze[x+1][y]!='.' && maze[x][y+1]=='.'){
			return 3;}

		//turn south
		if(maze[x][y-1]!='.' && maze[x+1][y]=='.'){
			return 4;}

	
}


int main(){

	
	int startX = 2;
	int startY = 0;
	int endX = 4;
	int endY = 11;
	int i,j;
	

	


	while(startX<=endX && startY<=endY){
		for(i=0 ; i<SIZE ; i++){
			for(j=0 ; j<SIZE ; j++){
			cout << maze[i][j];}
			cout << endl;
		}
			cout << endl;

		if(startX>0 || startX<12 && checkpath(startX,startY)==true){
			if(findpath(startX,startY)==1){
				maze[startX][startY]='X';
				cout << maze[startX][startY];
				startY--;
			}
			else if(findpath(startX,startY)==2){
				maze[startX][startY]='X';
				cout << maze[startX][startY];
				startX--;
			}
			else if(findpath(startX,startY)==3){
				maze[startX][startY]='X';
				cout << maze[startX][startY];
				startY++;
			}
			else if(findpath(startX,startY)==4){
				maze[startX][startY]='X';
				cout << maze[startX][startY];
				startX++;
			}
		}
		else if(startY>0 || startY<12 && checkpath(startX,startY)==true){
			if(findpath(startX,startY)==1){
				maze[startX][startY]='X';
				cout << maze[startX][startY];
				startY--;
			}
			else if(findpath(startX,startY)==2){
				maze[startX][startY]='X';
				cout << maze[startX][startY];
				startX--;
			}
			else if(findpath(startX,startY)==3){
				maze[startX][startY]='X';
				cout << maze[startX][startY];
				startY++;
			}
			else if(findpath(startX,startY)==4){
				maze[startX][startY]='X';
				cout << maze[startX][startY];
				startX++;
			}
		}


		else{
			cout << "*******************" << endl;
			cout << "Your Maze is Solved" << endl;
			cout << "Congratulations!!!!" << endl;
			cout << "*******************" << endl;
		}
	}

	system("pause");
}
In main, you print out the maze every time with your "for" loop.

Also, you print out a line every time that findpath() returns true.
Topic archived. No new replies allowed.
Pages: 12