# Thread: Recursive maze, so close, but something is wrong

1. ## Recursive maze, so close, but something is wrong

I'm very new to C and I've been working so hard on this and I have no idea what is wrong with it, I think it is in the Move method, but I wasn't sure so I included the whole program, if anyone can figure out why whats wrong please let me know! Thanks!
My assignment is to get through a maze that can be up to 50 by 50, the sample maze I've been using looks like this: The 1s are walls and the 0s are where you can step.

1 1 1 1 1 1 1 1 1 1
1 0 1 1 0 0 0 0 0 1
1 1 0 0 0 1 0 1 0 1
1 0 0 1 1 1 0 1 0 1
1 0 1 1 0 0 1 1 1 1
1 1 1 0 1 1 0 0 1 1
1 1 0 1 1 0 1 0 0 0 S
1 0 0 0 0 0 0 1 0 1
1 0 1 0 0 0 0 0 0 1
1 0 0 1 0 0 1 1 1 1
1 0 1 0 0 1 0 0 1 1
1 1 1 1 0 1 1 1 1 1
E
It prints X's on the places it has visited and it will go left, but it won't go up, down, or right. it starts at S and ends at E, but it will only go over the first 3 0s, replace them with X's, remove the last two X's and terminate. Please help!

Code:
#include <stdio.h>
#define M 50
#define N 50
int rows, columns, exitRow, exitCol, enterRow, enterCol;
void printArray(char a[M][N], int rows, int columns);
int moveLeft(int currCol, int currRow, char a[M][N]);
int moveRight(int currCol, int currRow, char a[M][N]);
int moveUp(int currRow, int currCol, char a[M][N]);
int moveDown(int currRow, int currCol, char a[M][N]);
int Move(int currCol, int currRow, char array[M][N], int first);
void removeX(int currRow, int currCol, char array[M][N]);
int fail=0;

main()
{
char comma;
char foundEnter='n';
char foundExit='n';
int first=1;

//takes in the size of the maze
printf("Please type in the size of the field as rows, columns\n");
scanf("%d", &rows);
scanf("%c", &comma);
scanf("%d", &columns);

/*Asks user for input file and adds the field from the file to an array*/
char array[M][N]={9};
FILE *fptr;
char c;
char file_name[20];
int i,j;
printf("Type in the name of the file containing the Field\n");
scanf("%s",file_name);
fptr=fopen(file_name,"r");
for (i=0; i<rows; i++)
for (j=0; j<columns; j++){
c=fgetc(fptr);
while ( !((c == '1')||(c =='0')) ) c=fgetc(fptr);
array[i][j]=c;
}
fclose(fptr);

//Copies maze into the failed array
char failArray[M][N]={9};
for (i=0; i<rows; i++)
for (j=0; j<columns; j++){
failArray[i][j]=array[i][j];
}

//finds the entrance and exit of the maze
for(j=0; j<columns; j++){
if(array[0][j] != '1' && array[0][j]=='0'){
if(foundEnter=='n'){
enterCol= j;
enterRow=0;
foundEnter='y';
}
else{ exitCol= j;
exitRow=0;
foundExit='y';
}
}
}

for(i=0; i<rows; i++){
if(array[i][columns-1] != '1' && array[i][columns-1]=='0'){
if(foundEnter=='n'){
enterCol= columns-1;
enterRow=i;
foundEnter='y';
}
else { exitCol= columns-1;
exitRow=i;
foundExit='y';
}
}
}

for(j=columns-1; j>=0; j--){
if(array[rows-1][j] != '1' && array[rows-1][j]=='0'){
if(foundEnter=='n'){
enterCol= j;
enterRow=rows-1;
foundEnter='y';
}
else { exitCol= j;
exitRow=rows-1;
foundExit='y';
}
}
}
for(i=rows-1; i>=0; i--){
if(array[i][0] !='1' && array[i][0]=='0'){
if(foundEnter=='n'){
enterCol= 0;
enterRow=i;
foundEnter='y';
}
else { exitCol= 0;
exitRow=i;
foundExit='y';
}
}
}

//prints the array
printArray(array, rows, columns);

int currRow=enterRow; //sets the current Row to the enter Row
int currCol=enterCol; //sets the current Column to the enter Column
int end;

end=Move(currCol, currRow, array, first);

//if theres no path found it prints the fail array
if(end){
printArray(failArray, rows, columns);
}
else{
//if the path is found it prints the path to the exit
printArray(array, rows, columns);
printf("Path found.\n");
}
return 0;
}

//prints the array
void printArray(char a[M][N], int rows, int columns){
int i, j;
for (i=0; i<rows; i++)
for (j=0; j<columns; j++) {
if (j == 0) printf("\n");
printf("%c ",a[i][j]);
}
printf("\n");
}

//checks if move Left is possible
int moveLeft(int currCol, int currRow, char a[M][N]){
if(currCol-1>=-1)
if(a[currRow][currCol-1]=='0')
return 1;
return 0;
}

//checks if move Right is possible
int moveRight(int currCol, int currRow, char a[M][N]){
if(currCol+1<currCol)
if(a[currRow][currCol+1]=='0')
return 1;
return 0;
}

//checks if move Up is possible
int moveUp(int currRow, int currCol, char a[M][N]){
if(currRow-1>-1)
if(a[currRow-1][currCol]=='0')
return 1;
return 0;
}

//checks if move Down is possible
int moveDown(int currRow, int currCol, char a[M][N]){
if(currRow+1<currRow)
if(a[currRow+1][currCol]=='0')
return 1;
return 0;
}

//moves through the maze
int Move(int currCol, int currRow, char array[M][N], int first){
int fail;
int c;
array[currRow][currCol]='X';
failArray[currRow][currCol]='X';

//checks the exit condition as long as its not the first time through
if(!first){
if(currRow==exitRow && currCol==exitCol)
return 0;
if(currRow==enterRow && currCol==enterCol)
return 1;
}

//this is to track the array (temporary until code fixed)
first=0;
printArray(array, rows, columns);
printf("At Point row %d and col %d \n",currRow, currCol);
printf("fail=%d \n", fail);
getchar();

//goes left until it can't
if(moveLeft(currCol, currRow, array)){
c=--currCol;
printf("Moved left \n");
printf("Moving to col %d \n",c);
fail = !Move(c, currRow, array, first);
}
//if left fails goes up until it can't
if(moveUp(currCol, currRow, array) && fail){
c=--currRow;
printf("Moved up \n");
fail = !Move(currCol, c, array, first);
}
//if up fails goes down until it can't
if(moveDown(currCol, currRow, array) && fail){
c=++currRow;
printf("Moved down \n");
fail = !Move(currCol, c, array, first);
}
//if down fails goes right until it can't
if(moveRight(currCol, currRow, array) && fail){
c=++currCol;
printf("Moved right \n");
fail = !Move(c, currRow, array, first);
}
//removes the X so we can backtrack
removeX(currRow, currCol, array);
return 1;

//absolute failure
printf("Failed all \n");
return 0;
}

//removes the X and replaces it with a 0.
void removeX(int currRow, int currCol, char array[M][N]){
array[currRow][currCol]='0';
}

2. What does your compiler tell you? Do you have your warnings turned on? I would guess that you do, because you know that there is indeed a problem with your Move function. Let me ask you, do you think that the failArray in the Move function knows anything about the failArray declared in main?

3. No one wants to read a hundred lines of code with no indentation.

Quzah.

4. Originally Posted by quzah
No one wants to read a hundred lines of code with no indentation.

Quzah.
238, actually. I was going to say something along the same lines as this, but you always have a much better way of saying it.

5. ## Sorry, warnings and the area I think is the problem

It's indented on my program, just not when I pasted it.
I just moved the failArray from being global and did not recompile, so now its back to being global so the only warnings I get are:

path2.c:20: warning: missing curly brackets around initialiser
path2.c:20: warning: (near initialisation for ‘failArray[0]’)
path2.c:23: warning: return type defaults to ‘int’
path2.c: In function ‘main’:
path2.c:36: warning: missing curly brackets around initialiser
path2.c:36: warning: (near initialisation for ‘array[0]’)

And here is the code I think there is a problem with,I wasn't sure so I posted the whole thing, I hope its more clear this way, I'm new at this:
Code:
//moves through the maze
int Move(int currCol, int currRow, char array[M][N], int first){
int fail;
int c;
array[currRow][currCol]='X';
failArray[currRow][currCol]='X';

//checks the exit condition as long as its not the first time through
if(!first){
if(currRow==exitRow && currCol==exitCol)
return 0;
if(currRow==enterRow && currCol==enterCol)
return 1;
}

//this is to track the array (temporary until code fixed)
first=0;
printArray(array, rows, columns);
printf("At Point row %d and col %d \n",currRow, currCol);
printf("fail=%d \n", fail);
getchar();

//goes left until it can't
if(moveLeft(currCol, currRow, array)){
c=--currCol;
printf("Moved left \n");
printf("Moving to col %d \n",c);
fail = !Move(c, currRow, array, first);
}
//if left fails goes up until it can't
if(moveUp(currCol, currRow, array) && fail){
c=--currRow;
printf("Moved up \n");
fail = !Move(currCol, c, array, first);
}
//if up fails goes down until it can't
if(moveDown(currCol, currRow, array) && fail){
c=++currRow;
printf("Moved down \n");
fail = !Move(currCol, c, array, first);
}
//if down fails goes right until it can't
if(moveRight(currCol, currRow, array) && fail){
c=++currCol;
printf("Moved right \n");
fail = !Move(c, currRow, array, first);
}
//removes the X so we can backtrack
removeX(currRow, currCol, array);
return 1;

//absolute failure
printf("Failed all \n");
return 0;
}

6. You probably think the variable failArray exists, but the compiler does not agree with you. It also doesn't quite agree with you on where functions begin and end, so I think you have a curly brace problem.

7. The program will still run with warnings, I really don't think the warnings are the problem, but if someone knows from experience they are, I'd like advice on how to fix them. Thanks.
This is the output if I print failArray before the program exits, so I believe it has to exist to be printed:

failArray printed:

1 1 1 1 1 1 1 1 1 1
1 0 1 1 0 0 0 0 0 1
1 1 0 0 0 1 0 1 0 1
1 0 0 1 1 1 0 1 0 1
1 0 1 1 0 0 1 1 1 1
1 1 1 0 1 1 0 0 1 1
1 1 0 1 1 0 1 X X X
1 0 0 0 0 0 0 1 0 1
1 0 1 0 0 0 0 0 0 1
1 0 0 1 0 0 1 1 1 1
1 0 1 0 0 1 0 0 1 1
1 1 1 1 0 1 1 1 1 1

8. Well your compiler must be a lot better than mine, because I cannot get an executable from that code. You have been given some ideas of what you need to fix.

9. //removes the X so we can backtrack
removeX(currRow, currCol, array);
return 1;

//absolute failure
printf("Failed all \n");
return 0;
}
Well, I'm betting it's some form of braces problem too, considering "absolute failure" will never be reached. Shouldn't your compiler be complaining about that as well?

Quzah.

10. Originally Posted by forensicgeek
The program will still run with warnings, I really don't think the warnings are the problem, but if someone knows from experience they are, I'd like advice on how to fix them. Thanks.
Actually, ignoring the warnings is the problem...

It somehow knows it cant be referring to an existing variable called failArray for some reason. Thus it's telling you that it's forced to assume you're trying to actually declare failArray on line 20, but it cant quite understand the syntax you're using, as it doesn't match with any proper way of declaring something. So it suspects you've probably messed up somewhere, and it is indeed correct.

Looking through your code, there is indeed no globally accessable variable with that name. There's a variable local to main with that name, but it knows that can only be referred to from inside main. Perhaps you meant to either make that global, or pass it in as another parameter to Move?