Hello,
I am writing a program that navigates a maze. I am trying to use a recursive function to do so. My problem is the function stops working partway through. This is the maze I am using:
ooooooooooo
o ```````` o
o`ooooooo `o
o+o```````o
ooo`ooooooo
o$o``````$o
o`o`ooooooo
o````````-o
ooooooooooo
The function gets as far as this and quits
ooooooooooo
o ########o
o #ooooooo `o
o+o```````o
ooo`ooooooo
o$o``````$o
o`o`ooooooo
o````````-o
ooooooooooo
Code:
void findDollar ( maz_t *t){ // function to find checkpoint
int i, j;
for (i=0;i<row;i++){ // loop to find a checkpoint
for(j=0;j<column;j++){
if (t->map[i][j] == '$'){
t->dollarX = i;
t->dollarY = j;
}
}
}
if(t->map[t->startx-1][t->starty] != 'o' && t->map[t->startx-1][t->starty] != '#'){ // moves up
t->map[t->startx-1][t->starty] = '#';
t->startx = t->startx-1;
}
else if(t->map[t->startx+1][t->starty] != 'o' && t->map[t->startx+1][t->starty] != '#'){ // moves down
t->map[t->startx+1][t->starty] = '#';
t->startx = t->startx+1;
}
else if(t->map[t->startx][t->starty-1] != 'o' && t->map[t->startx][t->starty-1] != '#'){ // moves to the left
t->map[t->startx][t->starty-1] = '#';
t->starty = t->starty-1;
}
else if(t->map[t->startx][t->starty+1] != 'o' && t->map[t->startx][t->starty+1] != '#'){ // moves to the right
t->map[t->startx][t->starty+1] = '#';
t->starty = t->starty+1;
}
if (t->startx != t->dollarX && t->starty != t->dollarY){ // If the checkpoint has not been reached, call function again.
findDollar(t);
}
}// end function
I'm assuming that it's something with the way I am directing it to move down, but I can't see the problem. Any help would be greatly appreciated.