I am trying to convert the following code (from Wikipedia ) from Java to JavaScript:
import java.awt.*;
import java.applet.*;
import java.util.Random;
class Constants {
public static final int WALL_ABOVE = 1;
public static final int WALL_BELOW = 2;
public static final int WALL_LEFT = 4;
public static final int WALL_RIGHT = 8;
public static final int QUEUED = 16;
public static final int IN_MAZE = 32;
}
public class Maze extends java.applet.Applet {
private int width;
private int height;
private int maze[][];
private static final Random rnd = new Random();
private int cell_width;
public Maze() {
this(20,20,10);
}
public Maze(int width, int height, int cell_width) {
this.width = width;
this.height = height;
this.cell_width = cell_width;
}
public static void main(String[] args) {
Maze m = new Maze();
m.createMaze();
m.printSVG();
}
public void init() {
createMaze();
}
private void createMaze(){
int x, y, n, d;
int dx[] = { 0, 0, -1, 1 };
int dy[] = { -1, 1, 0, 0 };
int todo[] = new int[height * width], todonum = 0;
maze = new int[width][height];
for (x = 0; x < width; ++x) {
for (y = 0; y < height; ++y) {
if (x == 0 || x == width - 1 || y == 0 || y == height - 1) {
maze[x][y] = Constants.IN_MAZE;
} else {
maze[x][y] = 63;
}
}
}
x = 1 + rnd.nextInt (width - 2);
y = 1 + rnd.nextInt (height - 2);
maze[x][y] &= ~48;
for (d = 0; d < 4; ++d) {
if ((maze[][d][][d] & Constants.QUEUED) != 0) {
todo[todonum++] = ((x + dx[d]) << Constants.QUEUED) | (y + dy[d]);
maze[][d][][d] &= ~Constants.QUEUED;
}
}
while (todonum > 0) {
n = rnd.nextInt (todonum);
x = todo[n] >> 16;
y = todo[n] & 65535;
todo[n] = todo[--todonum];
do {
d = rnd.nextInt (4);
}
while ((maze[][d][][d] & Constants.IN_MAZE) != 0);
maze[x][y] &= ~((1 << d) | Constants.IN_MAZE);
maze[][d][][d] &= ~(1 << (d ^ 1));
for (d = 0; d < 4; ++d) {
if ((maze[][d][][d] & Constants.QUEUED) != 0) {
todo[todonum++] = ((x + dx[d]) << Constants.QUEUED) | (y + dy[d]);
maze[][d][][d] &= ~Constants.QUEUED;
}
}
}
maze[1][1] &= ~Constants.WALL_ABOVE;
maze[width - 2][height - 2] &= ~Constants.WALL_BELOW;
}
public void paint(Graphics g) {
drawMaze(g);
}
public void printSVG() {
System.out.format("<svg width=\"%d\" height=\"%d\" version=\"1.1\""
+ " xmlns=\"http://www.w3.org/2000/svg\">\n",
width*cell_width, height*cell_width);
System.out.println(" <g stroke=\"black\" stroke-width=\"1\""
+ " stroke-linecap=\"round\">");
drawMaze(null);
System.out.println(" </g>\n</svg>");
}
public void drawMaze(Graphics g) {
int x, y;
for (x = 1; x < width - 1; ++x) {
for (y = 1; y < height - 1; ++y) {
if ((maze[x][y] & Constants.WALL_ABOVE) != 0)
drawLine( x * cell_width, y * cell_width,
(x + 1) * cell_width, y * cell_width, g);
if ((maze[x][y] & Constants.WALL_BELOW) != 0)
drawLine( x * cell_width, (y + 1) * cell_width,
(x + 1) * cell_width, (y + 1) * cell_width, g);
if ((maze[x][y] & Constants.WALL_LEFT) != 0)
drawLine( x * cell_width, y * cell_width,
x * cell_width, (y + 1) * cell_width, g);
if ((maze[x][y] & Constants.WALL_RIGHT) != 0)
drawLine((x + 1) * cell_width, y * cell_width,
(x + 1) * cell_width, (y + 1) * cell_width, g);
}
}
}
public void drawLine(int x1, int y1, int x2, int y2, Graphics g) {
if ( g != null ) g.drawLine(x1, y1, x2, y2);
else System.out.format(" <line x1=\"%d\" y1=\"%d\""
+ " x2=\"%d\" y2=\"%d\" />\n", x1, y1, x2, y2);
}
}
In any case, I got over pretty quickly when I worked a little, which I just don’t understand:
for (var d = 0; d < 4; ++d) {
if ((maze[][d][][d] & Constants.QUEUED) != 0) {
todo[todonum++] = ((x + dx[d]) << Constants.QUEUED) | (y + dy[d]);
maze[][d][][d] &= ~Constants.QUEUED;
}
}
What I am not getting is that the four bracket parameters follow the “labyrinth” parameter instead of the two, since the “labyrinth” is a two-dimensional array, not a four-dimensional array.
I am sure there is a good reason for this. The problem is that I just do not understand.
Thank!