Maze generation: Difference between revisions

Content added Content deleted
(→‎{{header|Java}}: randomize() was not a correct shuffle (http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle#Implementation_errors), plus shuffle is already in library)
Line 1,035: Line 1,035:
*/
*/
public class MazeGenerator {
public class MazeGenerator {
private int x;
private final int x;
private int y;
private final int y;
private int[][] maze;
private final int[][] maze;
private Random rand = new Random(System.currentTimeMillis());
private static final Random rand = new Random();


public MazeGenerator(int x, int y) {
public MazeGenerator(int x, int y) {
Line 1,051: Line 1,051:
// draw the north edge
// draw the north edge
for (int j = 0; j < x; j++) {
for (int j = 0; j < x; j++) {
System.out.print(((maze[j][i] & 1) == 0) ? "+---" : "+ ");
System.out.print((maze[j][i] & 1) == 0 ? "+---" : "+ ");
}
}
System.out.println("+");
System.out.println("+");
// draw the west edge
// draw the west edge
for (int j = 0; j < x; j++) {
for (int j = 0; j < x; j++) {
System.out.print(String.format("%s ",
System.out.print((maze[j][i] & 8) == 0 ? "| " : " ");
((maze[j][i] & 8) == 0 ? "|" : " "), maze[j][i]));
}
}
System.out.println("|");
System.out.println("|");
Line 1,074: Line 1,073:
int nx = cx + dir.dx;
int nx = cx + dir.dx;
int ny = cy + dir.dy;
int ny = cy + dir.dy;
if (between(nx, maze.length) && between(ny, maze[0].length)
if (between(nx, x) && between(ny, y)
&& (maze[nx][ny] == 0)) {
&& (maze[nx][ny] == 0)) {
maze[cx][cy] |= dir.bit;
maze[cx][cy] |= dir.bit;
Line 1,089: Line 1,088:
private enum DIR {
private enum DIR {
N(1, 0, -1), S(2, 0, 1), E(4, 1, 0), W(8, -1, 0);
N(1, 0, -1), S(2, 0, 1), E(4, 1, 0), W(8, -1, 0);
private int bit;
private final int bit;
private int dx;
private final int dx;
private int dy;
private final int dy;
private DIR opposite;
private DIR opposite;