Note that there are some explanatory texts on larger screens.

plurals
  1. POMaze generation - ArrayIndexOutOfBoundsException
    text
    copied!<p>I tried generate maze with two entries at the opposite sides.</p> <p>But when I trying to run this test program I catch <code>ArrayIndexOutOfBoundsException</code>:</p> <pre><code>Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1 at maze.variation.MyMaze.generateMaze(MyMaze.java:61) at maze.variation.MyMaze.generateMaze(MyMaze.java:63) at maze.variation.MyMaze.generateMaze(MyMaze.java:51) at maze.variation.MyMaze.generateMaze(MyMaze.java:51) at maze.variation.MyMaze.generateMaze(MyMaze.java:51) at maze.variation.MyMaze.generateMaze(MyMaze.java:51) at maze.variation.MyMaze.generateMaze(MyMaze.java:35) at maze.variation.MyMaze.&lt;init&gt;(MyMaze.java:14) at maze.variation.MyMaze.main(MyMaze.java:137) </code></pre> <p>I can't figure out what's wrong. It's some easy failures, but I can't find weak place.</p> <p><strong>Code:</strong></p> <pre><code>public class MyMaze { private int dimension; // dimension of maze private char[][] grid; private boolean[][] marked; private boolean[][] visited; private boolean done = false; public MyMaze(int aDimension) { dimension = aDimension; grid = new char[dimension][dimension]; init(); generateMaze(); } private void init() { // initialize border cells as already visited visited = new boolean[dimension + 2][dimension + 2]; for (int x = 0; x &lt; dimension + 2; x++) visited[x][0] = visited[x][dimension + 1] = true; for (int y = 0; y &lt; dimension + 2; y++) visited[0][y] = visited[dimension + 1][y] = true; // initialze all walls as present visited = new boolean[dimension + 2][dimension + 2]; marked = new boolean[dimension + 2][dimension + 2]; for (int x = 0; x &lt; dimension + 2; x++) for (int y = 0; y &lt; dimension + 2; y++) marked[x][y] = true; } // generate the maze starting from lower left private void generateMaze() { generateMaze(1, 1); } // generate the maze private void generateMaze(int x, int y) { visited[x][y] = true; // while there is an unvisited neighbor while (!visited[x][y + 1] || !visited[x + 1][y] || !visited[x][y - 1] || !visited[x - 1][y]) { // pick random neighbor while (true) { double r = Math.random(); if (r &lt; 0.25 &amp;&amp; !visited[x][y + 1]) { marked[x][y] = marked[x][y + 1] = false; generateMaze(x, y + 1); // 51 line break; } else if (r &gt;= 0.25 &amp;&amp; r &lt; 0.50 &amp;&amp; !visited[x + 1][y]) { marked[x][y] = marked[x + 1][y] = false; generateMaze(x + 1, y); break; } else if (r &gt;= 0.5 &amp;&amp; r &lt; 0.75 &amp;&amp; !visited[x][y - 1]) { marked[x][y] = marked[x][y - 1] = false; generateMaze(x, y - 1); break; } else if (r &gt;= 0.75 &amp;&amp; r &lt; 1.00 &amp;&amp; !visited[x - 1][y]) { // 61 line marked[x][y] = marked[x - 1][y] = false; generateMaze(x - 1, y); break; } } } } // solve the maze starting from the start state public void solve() { for (int x = 1; x &lt;= dimension; x++) for (int y = 1; y &lt;= dimension; y++) visited[x][y] = false; done = false; solve(1, 1); } // draw the maze public void draw() { for (int x = 1; x &lt;= dimension; x++) { for (int y = 1; y &lt;= dimension; y++) { if (marked[x][y]) { grid[x][y] = '*'; } } } System.out.print(this.grid); } /** * Overridden method to generate a human readable maze state. */ @Override public String toString() { StringBuffer sb = new StringBuffer(1024); for (int x = 0; x &lt; this.dimension; x++) { for (int y = 0; y &lt; this.dimension; y++) { sb.append(this.grid[x][y]); } sb.append("\n"); } return sb.toString(); } public static void main(String[] args) { int userDimension = 40; MyMaze maze = new MyMaze(userDimension); maze.draw(); } } </code></pre> <p>I don't know how to find solution for creating two entries at opposite sides (north and south) and they need to have connection to maze. Any suggestions?</p> <ul> <li><em><strong>How to solve this error trouble?</em></strong></li> </ul> <p><strong>Edit:</strong></p> <p>I added this check into while loop:</p> <pre><code> // pick random neighbor while (true) { if (x == 0 || x == 1 || y == 0 || y == 1) continue; // method's rest </code></pre> <p>It looks ok now.If you running this code you'll find no output. But it should be.<br> <strong><em>Why this doesn't print maze correctly?</em></strong></p>
 

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