Note that there are some explanatory texts on larger screens.

plurals
  1. POError when using backtracking recursive algorithm to generate maze
    primarykey
    data
    text
    <p>I am creating a maze generator using the recursive backtracking algorithm. However, my problem is that every time I run the program it gives the following result:</p> <pre><code>1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 1 1 1 1 1 0 0 0 0 1 1 1 1 1 1 0 0 0 1 1 1 1 1 1 1 0 0 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 </code></pre> <p>A little background on my code: The constructor fills the maze int array with 0s, and puts a 1 at the end position (0, 0), and then calls the backtrackGenerateMaze() method to randomly generate a maze. I think the problem may be that I don't have sort of walls, which may be making weird things happen. Also, for the directions: 0 = up, 1 = right, 2 = down, &amp; 3 = left. Hope that helps.</p> <p>Here's my code (sorry its so long, I've been trying to use comments to debug it):</p> <pre><code>public class Maze { private int width, length; private int[][] maze; public Maze(int rows, int columns) { width = columns; length = rows; maze = new int[rows][columns]; for (int i = 0; i &lt; rows; i++) { for (int j = 0; j &lt; columns; j++) { maze[i][j] = 0; } } maze[0][0] = 1; backtrackGenerateMaze(rows - 1, columns - 1, 1); } /* * THE PROBLEM MUST BE HERE IN THE backtrackGenerateMaze() METHOD */ private void backtrackGenerateMaze(int rows, int columns, int moveLength) { if (rows == 0 &amp;&amp; columns == 0) { System.out.println("rows = " + rows); System.out.println("length = " + length); System.out.println("columns = " + columns); System.out.println("width = " + width); return; } int[] randDirs = generateRandomDirections(); for (int dir : randDirs) { System.out.println("dir == " + dir); System.out.println("rows == " + rows); System.out.println("columns == " + columns + "\n"); if (dir == 0) { System.out.println("rows - moveLength == " + (rows - moveLength)); System.out.println("valid(rows - moveLength, columns) == " + (valid(rows - moveLength, columns))); System.out.println("isInRange(0, length, rows - moveLength, false) == " + (isInRange(0, length, rows - moveLength, false))); if (valid(rows - moveLength, columns) &amp;&amp; isInRange(0, length, rows - moveLength, false) &amp;&amp; maze[rows - moveLength][columns] != 1) { System.out.println("IF 0 is TRUE"); for (int i = 1; i &lt;= moveLength; i++) { maze[rows - moveLength][columns] = 1; } maze[rows][columns] = 1; System.out.println("HERE 0"); backtrackGenerateMaze(rows - moveLength, columns, moveLength); } // System.out.println("RETURN DIR 0"); // return; } else if (dir == 1) { System.out.println("columns + moveLength = " + (columns + moveLength)); System.out.println("valid(rows, columns + moveLength) = " + (valid(rows, columns + moveLength))); System.out.println(); if (valid(rows, columns + moveLength)) { System.out.println("VALID 1 is TRUE"); if (isInRange(0, width, columns + moveLength, false)) { System.out.println("isInRange() 1 is TRUE"); if (maze[rows][columns + moveLength] != 1) { System.out.println("square != 1 is TRUE"); System.out.println("IF 1 is TRUE"); for (int i = 1; i &lt;= moveLength; i++) { maze[rows][columns + moveLength] = 1; } maze[rows][columns] = 1; System.out.println("HERE 1"); backtrackGenerateMaze(rows, columns + moveLength, moveLength); } } } System.out.println("RETURN DIR 1"); return; } else if (dir == 2) { if (valid(rows + moveLength, columns) &amp;&amp; isInRange(0, length, rows + moveLength, false) &amp;&amp; maze[rows + moveLength][columns] != 1) { System.out.println("IF 2 is TRUE"); for (int i = 1; i &lt;= moveLength; i++) { maze[rows + moveLength][columns] = 1; } maze[rows][columns] = 1; System.out.println("HERE 2"); backtrackGenerateMaze(rows + moveLength, columns, moveLength); } System.out.println("RETURN DIR 2"); return; } else if (dir == 3) { if (valid(rows, columns - moveLength) &amp;&amp; isInRange(0, width, columns - moveLength, false) &amp;&amp; maze[rows][columns - moveLength] != 1) { System.out.println("IF 3 is TRUE"); for (int i = 1; i &lt;= moveLength; i++) { maze[rows][columns - moveLength] = 1; } maze[rows][columns] = 1; System.out.println("HERE 3"); backtrackGenerateMaze(rows + moveLength, columns - moveLength, moveLength); } System.out.println("RETURN DIR 3"); return; } } System.out.println("--------------------"); } public int[] generateRandomDirections() { ArrayList&lt;Integer&gt; rands = new ArrayList&lt;&gt;(); for (int i = 0; i &lt; 4; i++) { rands.add(i); } Collections.shuffle(rands); int[] ret = new int[4]; for (int i = 0; i &lt; rands.size(); i++) { ret[i] = rands.get(i); } return ret; } private boolean valid(int row, int column) { return isInRange(0, maze.length - 1, row, true) &amp;&amp; isInRange(0, maze[0].length - 1, column, true); } private boolean isInRange(int start, int end, int toCheck, boolean inclusive) { if (inclusive) { return (toCheck &gt;= start &amp;&amp; toCheck &lt;= end); } return (toCheck &gt; start &amp;&amp; toCheck &lt; end); } @Override public String toString() { String ret = ""; for (int i = 0; i &lt; maze.length; i++) { for (int j = 0; j &lt; maze[0].length; j++) { ret += maze[i][j] + " "; } ret += "\n"; } return ret; } } </code></pre> <p>...and here's the Main method I use to run it:</p> <pre><code>public class MazeGame { private static ArrayList&lt;Maze&gt; mazes = new ArrayList&lt;&gt;(); private static final int MAZE_SIZE = 10, NUM_MAZES = 1; public static void main(String[] args) { Maze temp; for (int i = 0; i &lt; NUM_MAZES; i++) { temp = new Maze(MAZE_SIZE, MAZE_SIZE); System.out.println(temp); mazes.add(temp); } } } </code></pre> <p>Any help is much appreciated.</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