-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
80 lines (59 loc) · 1.73 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import Cell from './cell.js';
const GRID_WIDTH = 48;
const GRID_HEIGHT = 32;
// Create maze grid
const maze = [];
for (let y = 0; y < GRID_HEIGHT; y++) {
maze[y] = [];
for (let x = 0; x < GRID_WIDTH; x++) {
maze[y][x] = new Cell(maze, x, y);
if (x === 0 && y === 0) {
maze[y][x].walls.top = false;
} else if (x === GRID_WIDTH - 1 && y === GRID_HEIGHT - 1) {
maze[y][x].walls.bottom = false;
}
}
}
// Create maze DOM table
const $table = document.createElement('table');
for (let y = 0; y < GRID_HEIGHT; y++) {
const $tr = document.createElement('tr');
for (let x = 0; x < GRID_WIDTH; x++) {
const $td = document.createElement('td');
maze[y][x].$td = $td;
maze[y][x].updateBorders();
$tr.appendChild($td);
}
$table.appendChild($tr);
}
document.body.appendChild($table);
// Generate maze
const stack = [];
maze[0][0].visited = true;
stack.push(maze[0][0]);
(async () => {
while (stack.length) {
const current = stack.pop();
current.$td.classList.add('black');
if (current.unvisitedNeighbors.length) {
const visitIndex = Math.random() * current.unvisitedNeighbors.length | 0;
const { pos, cell: neighbor } = current.unvisitedNeighbors[visitIndex];
current.visit(pos);
neighbor.visited = true;
if (current.unvisitedNeighbors.length) {
stack.push(current);
} else {
current.$td.classList.add('ready');
}
if (neighbor.unvisitedNeighbors.length) {
stack.push(neighbor);
} else {
neighbor.$td.classList.add('ready');
}
} else {
current.$td.classList.add('ready');
}
await new Promise(resolve => requestAnimationFrame(resolve));
current.$td.classList.remove('black');
}
})();