-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaze.js
416 lines (378 loc) · 14.8 KB
/
maze.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
const RECURSIVE_BACKTRACKING = "Recursive Backtracking";
const KRUSKAL = "Kruskal's Algorithm";
const ELLER = "Eller's Algorithm";
const MAX_DELAY = 500;
$(document).ready(() => {
var algorithmSelect = document.getElementById("algorithm-select");
var speedRange = document.getElementById("speed-range");
var sizeRange = document.getElementById("size-range");
var runButton = document.getElementById("run-button");
var pauseButton = document.getElementById("pause-button");
var table = document.getElementById("maze-grid");
var size = 12;
var speed = 200;
var isRunning = false;
var isPaused = false;
drawGrid();
sizeRange.oninput = function() {
size = sizeRange.value;
drawGrid();
}
speedRange.oninput = function() {
speed = MAX_DELAY - speedRange.value;
};
runButton.onclick = function() {
runButton.style.display = "none";
pauseButton.style.display = "inline";
sizeRange.disabled = true;
isPaused = false;
if (!isRunning) {
isRunning = true;
drawGrid();
let algorithm = algorithmSelect.value;
size = sizeRange.value;
speed = MAX_DELAY - speedRange.value;
generateMaze(algorithm);
}
};
pauseButton.onclick = function() {
runButton.style.display = "inline";
pauseButton.style.display = "none";
isPaused = true;
}
function drawGrid() {
table.innerHTML = "";
size = sizeRange.value;
for (let y = 0; y < size; y++) {
let row = table.insertRow(0);
for (let x = 0; x < size; x++) {
let cell = row.insertCell(0);
}
}
}
function generateMaze(algorithm) {
switch(algorithm) {
case RECURSIVE_BACKTRACKING:
RecusiveBacktracking();
break;
case KRUSKAL:
Kruskal();
break;
case ELLER:
Eller();
break;
}
}
function RecusiveBacktracking() {
let cells = [];
let stack = [];
let unvisitedCount = size * size;
let currentCell = {x:0, y:0};
// Initialize map
for (let y = 0; y < size; y++) {
let row = [];
for (let x = 0; x < size; x++) {
row.push({visited:false, top:true, right:true, bottom:true, left:true});
}
cells.push(row);
}
// Execute main loop
paintCell(currentCell.x, currentCell.y, "#00c483", cells[currentCell.x][currentCell.y]);
cells[currentCell.x][currentCell.y].visited = true;
unvisitedCount--;
loop();
return;
function loop() {
if (unvisitedCount > 0) {
if (!isPaused) {
let unvisitedNeighbors = getUnivistedNeighbors(currentCell.x, currentCell.y);
// Exploring new cells
if (unvisitedNeighbors.length > 0) {
let nextCell = unvisitedNeighbors[Math.floor(Math.random() * unvisitedNeighbors.length)];
stack.push(currentCell);
removeWallsBetween(currentCell, nextCell);
paintCell(currentCell.x, currentCell.y, "#00c48356", cells[currentCell.x][currentCell.y]);
paintCell(nextCell.x, nextCell.y, "#00c483", cells[currentCell.x][currentCell.y]);
currentCell = nextCell;
cells[currentCell.x][currentCell.y].visited = true;
unvisitedCount--;
}
// Backtracking
else {
paintCell(currentCell.x, currentCell.y, "#00c48356", cells[currentCell.x][currentCell.y]);
currentCell = stack.pop();
paintCell(currentCell.x, currentCell.y, "#00c483", cells[currentCell.x][currentCell.y]);
}
}
setTimeout(loop, speed);
}
// Maze is completed
else {
paintCell(currentCell.x, currentCell.y, "#00c48356", cells[currentCell.x][currentCell.y]);
runButton.style.display = "inline";
pauseButton.style.display = "none";
sizeRange.disabled = false;
isRunning = false;
return;
}
}
function getUnivistedNeighbors(x, y) {
unvisited = [];
// Left neighbor
if (x > 0 && !cells[x-1][y].visited) {
unvisited.push({x:x-1, y:y});
}
// Top neighbor
if (y > 0 && !cells[x][y-1].visited) {
unvisited.push({x:x, y:y-1});
}
// Right neighbor
if (x < size - 1 && !cells[x+1][y].visited) {
unvisited.push({x:x+1, y:y});
}
// Bottom neighbor
if (y < size - 1 && !cells[x][y+1].visited) {
unvisited.push({x:x, y:y+1});
}
return unvisited;
}
function removeWallsBetween(from, to) {
if (to.x == from.x) {
// Moving up
if (to.y < from.y) {
cells[from.x][from.y].top = false;
cells[to.x][to.y].bottom = false;
}
// Moving down
else {
cells[from.x][from.y].bottom = false;
cells[to.x][to.y].top = false;
}
}
else {
// Moving left
if (to.x < from.x) {
cells[from.x][from.y].left = false;
cells[to.x][to.y].right = false;
}
// Moving right
else {
cells[from.x][from.y].right = false;
cells[to.x][to.y].left = false;
}
}
}
}
function Kruskal() {
class Tree {
constructor(parent) {
this.parent = parent;
}
root() {
if (this.parent != null) {
return this.parent.root();
}
else {
return this;
}
}
connectTo(newParent) {
this.parent = newParent;
}
}
const TOP = 1;
const LEFT = 2;
// Initialize map
let cells = [];
for (let x = 0; x < size; x++) {
let col = [];
for (let y = 0; y < size; y++) {
col.push({tree: new Tree(null), top:true, right:true, bottom:true, left:true});
}
cells.push(col);
}
// Creates a list of every edge
let edges = [];
for (let x = 0; x < size; x++) {
for (let y = 0; y < size; y++) {
if (y > 0) {
edges.push({x:x, y:y, direction:TOP});
}
if (x > 0) {
edges.push({x:x, y:y, direction:LEFT});
}
}
}
// Execute main loop
let lastCell1 = null;
let lastCell2 = null;
loop();
function loop() {
if (edges.length > 0) {
if (!isPaused) {
// Selects a random edge
let index = Math.floor(Math.random() * edges.length);
let edge = edges[index];
let deltaX = 0;
let deltaY = 0;
// Calculates the position of the cells on either side of the edge.
if (edge.direction == TOP) {
deltaY = -1;
}
else if (edge.direction == LEFT) {
deltaX = -1;
}
let cell1 = cells[edge.x][edge.y];
let cell2 = cells[edge.x + deltaX][edge.y + deltaY];
// If the cells on either side of the edge are not already in the same tree, connect them.
if (cell1.tree.root() !== cell2.tree.root()) {
if (edge.direction == TOP) {
cell1.top = false;
cell2.bottom = false;
}
else if (edge.direction == LEFT) {
cell1.left = false;
cell2.right = false;
}
cell1.tree.root().connectTo(cell2.tree);
// Painting the cells
paintCell(edge.x, edge.y, "#00c483", cell1);
paintCell(edge.x + deltaX, edge.y + deltaY, "#00c483", cell2);
if (lastCell1 != null && lastCell2 != null) {
paintCell(lastCell1.x, lastCell1.y, "#00c48356", cells[lastCell1.x][lastCell1.y]);
paintCell(lastCell2.x, lastCell2.y, "#00c48356", cells[lastCell2.x][lastCell2.y]);
}
lastCell1 = {x:edge.x, y:edge.y};
lastCell2 = {x:edge.x + deltaX, y:edge.y + deltaY};
}
// Remove the chosen edge from the list of edges.
edges.splice(index, 1);
}
// Repeats the loop after the specified delay.
setTimeout(loop, speed);
}
// Maze is complete
else {
paintCell(lastCell1.x, lastCell1.y, "#00c48356", cells[lastCell1.x][lastCell1.y]);
paintCell(lastCell2.x, lastCell2.y, "#00c48356", cells[lastCell2.x][lastCell2.y]);
runButton.style.display = "inline";
pauseButton.style.display = "none";
sizeRange.disabled = false;
isRunning = false;
return;
}
}
}
function Eller() {
const HORIZONTAL_MERGE_CHANCE = 0.5;
const VERTICAL_MERGE_CHANCE = 0.5;
let cells = [];
let sets = {};
for (let x = 0; x < size; x++) {
let col = [];
for (let y = 0; y < size; y++) {
let cell = {set:0, x:x, y:y, top:true, right:true, bottom:true, left:true};
col.push(cell);
}
cells.push(col);
}
loop(0, 0);
function loop(x, y) {
let currentCell = cells[x][y];
let setsInRow = [];
// If the cell is not a part of any set yet, create a new set for it with the smallest available index.
if (currentCell.set == 0) {
console.log("currentCell.set == 0");
for (let i = 0; currentCell.set == 0; i++) {
console.log(`i: ${i}`);
if (sets[i] == null) {
console.log("sets[i] == null");
currentCell.set = i;
setsInRow.push(i);
}
}
}
// Merging currentCell with its neighbor to the left.
if (x > 0) {
let neighbor = cells[x-1][y];
if (Math.random() <= HORIZONTAL_MERGE_CHANCE) {
if (currentCell.set != neighbor.set) {
currentCell.left = false;
neighbor.right = false;
for (let i in sets[currentCell.set]) {
let cell = sets[currentCell.set][i];
cells[cell.x][cell.y].set = neighbor.set;
sets[neighbor.set].push(cell);
}
delete sets[currentCell.set];
setsInRow.slice(setsInRow.indexOf(currentCell.set), 1);
paintCell(currentCell.x, currentCell.y, "#00c483", currentCell);
paintCell(neighbor.x, neighbor.y, "#00c483", neighbor);
}
}
}
// Once the entire row has been iterated, make vertical connections such that every set in the row has at least one vertical connection.
if (x == size - 1) {
if (y < size - 1) {
let currentRow = cells[x];
let rowSets = {}
for (let i in currentRow) {
let cell = currentRow[i];
if (rowSets[cell.set] == null) {
rowSets[cell.set] = [];
}
rowSets[cell.set].push(cell);
}
for (let i in rowSets) {
let set = rowSets[i];
let verticalConnections = 0;
while (verticalConnections < 1) {
for (let j in set) {
let cell = set[j];
let neighbor = cells[cell.x][cell.y+1];
if (Math.random() < VERTICAL_MERGE_CHANCE) {
cells[cell.x][cell.y].bottom = false;
cells[neighbor.x][neighbor.y].top = false;
neighbor.set = cell.set;
verticalConnections++;
paintCell(cell.x, cell.y, "#00c483", cells[cell.x][cell.y]);
paintCell(neighbor.x, neighbor.y, "#00c483", cells[neighbor.x][neighbor.y]);
}
}
}
}
setTimeout(()=>{loop(0, ++y);}, speed);
}
}
setTimeout(()=>{loop(++x, y);}, speed);
}
}
function paintCell(x, y, color, borders) {
let cellStyle = table.rows[y].cells[x].style;
cellStyle.backgroundColor = color;
if (borders.top) {
cellStyle.borderTop = "1px solid #000000";
}
else {
cellStyle.borderTop = "1px solid #00000000";
}
if (borders.right) {
cellStyle.borderRight = "1px solid #000000";
}
else {
cellStyle.borderRight = "1px solid #00000000";
}
if (borders.bottom) {
cellStyle.borderBottom = "1px solid #000000";
}
else {
cellStyle.borderBottom = "1px solid #00000000";
}
if (borders.left) {
cellStyle.borderLeft = "1px solid #000000";
}
else {
cellStyle.borderLeft = "1px solid #00000000";
}
}
});