Note that there are some explanatory texts on larger screens.

plurals
  1. POIssue with my recursive maze traversal algorithm
    text
    copied!<p>The purpose of my code is to create a program that will read in a maze and store it into a 2D array and solve it. I got the program to read the maze and put it into the array but my recursive algorithm is not working and this is the third different time I have changed it trying to get it to work. So could you help me get this to work?</p> <p>Edit:I found out the problem with my original algorithm in the sense I got it to solve the maze but I have ran into another problem. The program is not printing out the correct things. Also this is just for my curiosity. I have already found out how to get it to work another way.</p> <p>Maze Code:</p> <pre><code> public static boolean goNorth(){ boolean success; if (maze[currCol][currRow - 1] == maze[finishCol][finishRow]) { success = true; } if(maze[currCol][currRow - 1] == CLEAR){ maze[currCol][currRow - 1] = PATH; currRow = currRow - 1; success = goNorth(); if(!success){ success = goWest(); if(!success){ success = goEast(); if(!success){ maze[currCol][currRow] = VISITED; currRow = currRow + 1; } } } return success; } else { return false; } } public static boolean goWest(){ boolean success; if (maze[currCol - 1][currRow] == maze[finishCol][finishRow]) { success = true; } if(maze[currCol - 1][currRow] == CLEAR){ maze[currCol - 1][currRow] = PATH; currCol = currCol - 1; success = goWest(); if(!success){ success = goSouth(); if(!success){ success = goNorth(); if(!success){ maze[currCol][currRow] = VISITED; currCol = currCol + 1; } } } return success; } else { return false; } } public static boolean goEast(){ boolean success; if (maze[currCol + 1][currRow] == maze[finishCol][finishRow]) { success = true; } if(maze[currCol + 1][currRow] == CLEAR){ maze[currCol + 1][currRow] = PATH; currCol = currCol + 1; success = goEast(); if(!success){ success = goNorth(); if(!success){ success = goSouth(); if(!success){ maze[currCol][currRow] = VISITED; currCol = currCol - 1; } } } return success; } else { return false; } } public static boolean goSouth(){ boolean success; if (maze[currCol][currRow + 1] == maze[finishCol][finishRow]){ success = true; } if(maze[currCol][currRow + 1] == CLEAR){ maze[currCol][currRow + 1] = PATH; currRow = currRow + 1; success = goSouth(); if(!success){ success = goEast(); if(!success){ success = goWest(); if(!success){ maze[currCol][currRow] = VISITED; currRow = currRow - 1; } } } return success; } else { return false; } } } </code></pre> <p>Maze Solver Code:</p> <pre><code>public class SolveMaze1 { public static void main (String[] args) { Maze1 maze = new Maze1(); maze.readMaze(); maze.printMaze(); maze.goNorth(); maze.printMaze(); } } </code></pre> <p>Desired outcome:</p> <pre><code>xxxxxxxxxxxxxxxxxxFx x x xxxx x x xxxxx xxxxx xx x x xxxxx xxxxxxx xx x x xx xx x x xxxxxxxxxx xx x xxxxxxxxxxxxSxxxxxxx xxxxxxxxxxxxxxxxxxFx xVVVVVxPPPPPPPxxxxPx xVxxxxxPxxxxxPPPxxPx xVxxxxxPxxxxxxxPxxPx xVVVVVVPPPPPPxxPxxPx xVxxxxxxxxxxPxxPPPPx xxxxxxxxxxxxSxxxxxxx </code></pre> <p>My Outcome:</p> <pre><code> xxxxxxxxxxxxxxxxxxFx xVVVVVxVVVVVVVxxxxVx xVxxxxxVxxxxxVVVxxVx xVxxxxxVxxxxxxxVxxVx xVVVVVVVVVVVVxxVxxVx xVxxxxxxxxxxVxxVVVVx xxxxxxxxxxxxSxxxxxxx </code></pre>
 

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