-1

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".

Beau Grantham
  • 3,435
  • 5
  • 33
  • 43
cuzynot
  • 5
  • 3
  • 2
    ` if (up == true && right == true)` nothing seems strange to you in this expression? If `up` is true, the LHS assignment is true. Just use `up && right`. – Turtle Oct 30 '17 at 19:14
  • Welcome to Stack Overflow! [What does your step debugger tell you?](http://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) –  Oct 30 '17 at 19:19
  • If i am not wrong then you should try save all states aswell which you have visited or later you will keep moving left and right :D – Luai Ghunim Oct 30 '17 at 19:20
  • `if (up == true && right == true)` NEVER write code like this. Always write it like `if (up && right)` if you are going making boolean decisions. –  Oct 30 '17 at 19:21

1 Answers1

1

I have not run your code, but reading it I suspect that one critical issue is the mutually exclusive conditions you have.

Your code reads:

if(condition 1) { 
   //recursion path A
} else if(condition 2) {
   //recursion path B
}

Which begs the questions: How does this code behave when more than one of those conditions are true? It will always just execute the first condition, and skip the rest.

Amir Afghani
  • 37,814
  • 16
  • 84
  • 124