Note that there are some explanatory texts on larger screens.

plurals
  1. PORecursive Maze Solver Issue
    primarykey
    data
    text
    <p>I'm making a recursive Java maze program and I'm stuck on the part when it comes to calling to my subroutines <code>goNorth()</code>, <code>goWest()</code>, <code>goEast()</code> and <code>goSouth()</code>. Basically my problem involves the fact that it calls to one subroutine, but within that subroutine it doesn't pick up on my other else if and else statements therefore not making it pick up on the other possibilities. Please assist, I appreciate your upcoming answer.</p> <pre><code>import java.util.*; import java.io.*; public class RecursiveMazeSolverv2 { static Scanner in; static int row = 0; static int col = 0; static char maze[][]; static int rowSize = 0; static int colSize = 0; static boolean success = true; public static void main(String[] args) { while (true) { try { in = new Scanner(new File("Maze1.txt")); break; } catch (FileNotFoundException e) { System.out.println("Wrong File"); } } rowSize = Integer.parseInt(in.nextLine()); colSize = Integer.parseInt(in.nextLine()); maze = new char[rowSize][colSize]; String lineOfInput = ""; for (int i = 0; i &lt; maze.length; i++) { lineOfInput = in.nextLine(); for (int j = 0; j &lt; maze.length; j++) { maze[i][j] = lineOfInput.charAt(j); } } displayGrid(); for (int i = 0; i &lt; maze.length; i++) { for (int j = 0; j &lt; maze.length; j++) { if (maze[i][j] == 'S') { maze[i][j]='+'; System.out.println("Starting coordinates: " + i + ", " + j); row = i; col = j; } } } if (goNorth(row, col)) displayGrid(); else System.out.println("Impossible!"); } static Boolean goNorth(int row, int col) { if (maze[row][col] == '.') { maze[row][col] = '+'; return goNorth(row -1, col); } else if (maze[row][col] == 'G') { return true; } else { success = goNorth(row, col); if (success == false) { success = goWest(row, col -1); } if (success == false) { success = goEast(row, col +1); } if (success == false) { success = goSouth(row +1, col); } if (success == false) { maze[row][col] = '.'; success = false; } return false; } } static Boolean goWest(int row, int col) { if (maze[row][col] == '.') { maze[row][col] = '+'; return goWest(row, col -1); } else if (maze[row][col] == 'G') { return true; } else { success = goWest(row, col); if (success == false) { success = goNorth(row -1, col); } if (success == false) { success = goSouth(row +1, col); } if (success == false) { success = goEast(row, col -1); } if (success == false) { maze[row][col] = '.'; success = false; } return false; } } static Boolean goEast(int row, int col) { if (maze[row][col] == '.') { maze[row][col] = '+'; return goEast(row, col +1); } else if (maze[row][col] == 'G') { return true; } else { success = goEast(row, col); if (success == false) { success = goNorth(row -1, col); } if (success == false) { success = goSouth(row +1, col); } if (success == false) { success = goWest(row, col -1); } if (success == false) { maze[row][col] = '.'; success = false; } return false; } } static Boolean goSouth(int row, int col) { if (maze[row][col] == '.') { maze[row][col] = '+'; return goSouth(row +1, col); } else if (maze[row][col] == 'G') { return true; } else { success = goSouth(row, col); if (success == false) { success = goNorth(row -1, col); } if (success == false) { success = goWest(row, col -1); } if (success == false) { success = goEast(row, col +1); } if (success == false) { maze[row][col] = '.'; success = false; } return false; } } public static void displayGrid() { for (int j = 0; j &lt; maze.length; j++) { for (int k = 0; k &lt; maze.length; k++) { System.out.print(maze[j][k] + " "); } System.out.println(); } } } </code></pre> <p>Sorry, I can't post the actual maze in here, it wont display right.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload