So this code is supposed to run through a map to find the destination and from a starting point like so:
##########
S ##
## #######
## ### #
## ###
###### # #
## # #
## ##### #
## D
##########
Basically I have recursive calls if there are 2 or more available paths and that's can be travelled. (I'm only showing the code for 2 directions)
public static String maze (String[][] map, int row, int col){
// mark current spot as done
map[row][col] = "-1";
boolean up = false;
boolean down = false;
boolean right = false;
boolean left = false;
int total = 0;
// check which adjacent boxes are free
if (map[row - 1][col].equals(" ")) up = true;
if (map[row + 1][col].equals(" ")) down = true;
if (map[row][col + 1].equals(" ")) right = true;
if (map[row][col - 1].equals(" ")) left = true;
// base case
if (map[row][col].equals("D")){
return "complete";
// 2 directions
if (up == true && right == true){
return maze(map, row - 1, col) + maze(map, row, col + 1);
} else if (up == true && down == true){
return maze(map, row - 1, col) + maze(map, row + 1, col); // only runs the first portion (i.e. maze(map, row - 1, col))
} else if (up == true && left == true){
return maze(map, row - 1, col) + maze(map, row, col - 1);
} else if (right == true && down == true){
return maze(map, row, col + 1) + maze(map, row + 1, col);
} else if (right == true && left == true){
return maze(map, row, col + 1) + maze(map, row, col - 1);
} else if (down == true && left == true){
return maze(map, row + 1, col) + maze(map, row, col - 1);
} else {
return "false";
}
}
Right now it's only printing "false" when it gets only runs one of the calls indicated above. Also, the code terminates when it returns false.
So what am I supposed to do in order to get to "complete" since it is possible to reach the end in this diagram. Or how do I declare my recursion statement to keep the program going even after it returns "false" so that it eventually reaches "complete".