Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Problems I see:</p> <ol> <li>As others eluded to, you shouldn't make everything <code>static</code>. Without going into a super long and boring discussion, making everything <code>static</code> means that the values you set in your recursive calls will modify the value for all calls. You are effectively over-writing each recursive call with values you are setting in subsequent recursive calls. You will want to make most of those method-scoped variables so that the value will only be valid in the scope of that method call.</li> <li>Your recursive calls are not in the same order. You need to do the same calls in the same step each time: Try North, then South, then East, then West. Whatever order you pick, the calls need to be in the same order. In fact, I am not really sure why you decided to have separate methods for each direction...why not have one method called "move" that tries to go North, then recurses, then tries to go South, then recurses, then tries to go East and recurses, then West and recurses. The way you have things coded, there is no telling where the code will go in the maze, and most likely you will end up going around in circles.</li> <li>This isn't directly linked to why it's not working, but you REALLY need to work on your code formatting, in particular your tabbing. Your code looks like it's all over the place. I can only imagine that that is making it WAY harder to troubleshoot that it needs to be.</li> </ol> <p><strong>Edit - Example</strong></p> <p>I will try to do this in a way to guide you without giving you a copy/paste answer, so this will be pseudo-code'ish.</p> <pre><code>/** * My move recursion */ public boolean move(int currRow, int currCol) { // See if we solved it... if (solved) { return true; } // Try to go north first... if (maze[currRow-1][currCol] == '.') { if (move(int currRow-1, currCol)) { // Mark this with the "good" path and return true } } // Try to go east next... if (maze[currRow][currCol+1] == '.') { if (move(int currRow, currCol+1)) { // Mark this with the "good" path and return true } } // Try to go south next... if (maze[currRow+1][currCol] == '.') { if (move(int currRow+1, currCol)) { // Mark this with the "good" path and return true } } // Try to go west... if (maze[currRow][currCol-1] == '.') { if (move(int currRow, currCol-1)) { // Mark this with the "good" path and return true } } return false; } </code></pre> <p>So, basically we check if we are "solved". If not, see if we can go north. If we can, see if the next call is solved. Repeat for east, south, and west. Eventually one of the recursive calls will get to the solved condition, which will trigger each of the recursive calls to pass the inner if, which marks the maze and returns true, thus creating a chain reaction that ends up popping back up the call stack until you are done with the recursion.</p> <p>Things to note with recursion:</p> <ol> <li>It typically needs to be a process that can be broken into one or more repeatable, autonomous steps. This doesn't mean it has to be one method, but if it's multiple methods you HAVE to perform things within the method in the same order. Otherwise, your logic get's out of sync.</li> <li>It typically works best when you can break the "steps" into their smallest, most menial parts. Overly complex recursive methods makes for hard debugging and lots and lots of crazy branching and looping.</li> <li>You have to have a VERY clear "end", else you will recurse until you blow the stack up.</li> <li>There should be a very clear distinction between variables/data/information that are "method level" and things that are "global". In your case, the maze is global, the success and current position are not.</li> </ol>
 

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