-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaze.js
283 lines (241 loc) · 7.78 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
class Maze {
constructor(x1, y1, numRows, numCols, cellSizeX, cellSizeY, window, seed = null) {
this.x1 = x1;
this.y1 = y1;
this.numRows = numRows;
this.numCols = numCols;
this.cellSizeX = cellSizeX;
this.cellSizeY = cellSizeY;
this.window = window;
this.seed = seed;
this.process = true;
if (this.seed) {
Math.seedrandom(this.seed); // Requires seedrandom library
}
this.cells = [];
this.start = null;
this.end = null;
}
async createCells() {
for (let row = 0; row < this.numRows; row++) {
let cellsRow = [];
for (let col = 0; col < this.numCols; col++) {
let topLeft = new Point(this.x1 + col * this.cellSizeX, this.y1 + row * this.cellSizeY);
let bottomRight = new Point(topLeft.x + this.cellSizeX, topLeft.y + this.cellSizeY);
let cell = new Cell(topLeft, bottomRight, this.window.getCanvas());
cellsRow.push(cell);
}
this.cells.push(cellsRow);
}
await this.drawAllCells();
await this.breakEntranceAndExit();
await this.breakWallsRecursive(this.start[0], this.start[1]);
this.resetCellsVisited();
//save the canvas to restore later before solving
this.savedCanvas = this.window.getCanvas().toDataURL();
}
restoreCanvas() {
const img = new Image();
img.onload = () => {
const ctx = this.window.getCanvas().getContext('2d');
ctx.clearRect(0, 0, this.window.getCanvas().width, this.window.getCanvas().height);
ctx.drawImage(img, 0, 0);
};
img.src = this.savedCanvas;
}
async drawAllCells() {
for (let row = 0; row < this.numRows; row++) {
for (let col = 0; col < this.numCols; col++) {
if (this.process) {
await this.drawCell(row, col, 30);
}
}
}
}
async drawCell(row, col, duration = 0) {
if (this.process) {
this.cells[row][col].draw();
await this.animate(duration);
}
}
stop() {
this.process = false;
}
async animate(duration = 500) {
if (this.process) {
await new Promise(resolve => setTimeout(resolve, duration));
}
}
async breakEntranceAndExit() {
// Select a random cell on any of the 4 sides to be the entrance
let iStart = Math.floor(Math.random() * this.numRows);
let jStart;
if (iStart === 0 || iStart === this.numRows - 1) {
jStart = Math.floor(Math.random() * this.numCols);
} else {
jStart = Math.random() < 0.5 ? 0 : this.numCols - 1;
}
let iEnd = iStart;
let jEnd = jStart;
let tries = 0;
while ((Math.abs(iStart - iEnd) + Math.abs(jStart - jEnd) < (this.numRows + this.numCols) / 2) && this.process && tries++ < 100) {
iEnd = Math.floor(Math.random() * this.numRows);
if (iEnd === 0 || iEnd === this.numRows - 1) {
jEnd = Math.floor(Math.random() * this.numCols);
} else {
jEnd = Math.random() < 0.5 ? 0 : this.numCols - 1;
}
}
this.start = [iStart, jStart];
this.end = [iEnd, jEnd];
this.updateWall(iStart, jStart, iEnd, jEnd);
if (this.process) {
this.drawCellAndEmoji(iStart, jStart, "🚀");
await this.animate(100);
this.drawCellAndEmoji(iEnd, jEnd, "🌔");
}
}
updateWall(iStart, jStart, iEnd, jEnd) {
// Update the walls for the entrance
if (iStart === 0) {
this.cells[iStart][jStart].hasTopWall = false;
} else if (iStart === this.numRows - 1) {
this.cells[iStart][jStart].hasBottomWall = false;
} else if (jStart === 0) {
this.cells[iStart][jStart].hasLeftWall = false;
} else if (jStart === this.numCols - 1) {
this.cells[iStart][jStart].hasRightWall = false;
}
// Update the walls for the exit
if (iEnd === 0) {
this.cells[iEnd][jEnd].hasTopWall = false;
} else if (iEnd === this.numRows - 1) {
this.cells[iEnd][jEnd].hasBottomWall = false;
} else if (jEnd === 0) {
this.cells[iEnd][jEnd].hasLeftWall = false;
} else if (jEnd === this.numCols - 1) {
this.cells[iEnd][jEnd].hasRightWall = false;
}
}
drawCellAndEmoji(i, j, emoji) {
if (this.process) {
this.cells[i][j].draw(); // Assuming Cell class has a draw method
this.cells[i][j].drawEmoji(emoji); // Assuming Cell class has a drawEmoji method
}
}
async breakWalls(i, j, iNext, jNext) {
if (iNext > i) {
this.cells[i][j].hasBottomWall = false;
this.cells[iNext][jNext].hasTopWall = false;
} else if (iNext < i) {
this.cells[i][j].hasTopWall = false;
this.cells[iNext][jNext].hasBottomWall = false;
} else if (jNext > j) {
this.cells[i][j].hasRightWall = false;
this.cells[iNext][jNext].hasLeftWall = false;
} else if (jNext < j) {
this.cells[i][j].hasLeftWall = false;
this.cells[iNext][jNext].hasRightWall = false;
}
this.drawCell(i, j);
await this.animate(30);
this.drawCell(iNext, jNext);
await this.animate(30);
}
async breakWallsRecursive(i, j) {
this.cells[i][j].visited = true;
while (this.process) {
let neighbours = [];
if (i > 0 && !this.cells[i - 1][j].visited) {
neighbours.push([i - 1, j]);
}
if (i < this.numRows - 1 && !this.cells[i + 1][j].visited) {
neighbours.push([i + 1, j]);
}
if (j > 0 && !this.cells[i][j - 1].visited) {
neighbours.push([i, j - 1]);
}
if (j < this.numCols - 1 && !this.cells[i][j + 1].visited) {
neighbours.push([i, j + 1]);
}
if (neighbours.length === 0) {
break;
}
const [iNext, jNext] = neighbours[Math.floor(Math.random() * neighbours.length)];
await this.breakWalls(i, j, iNext, jNext);
await this.breakWallsRecursive(iNext, jNext);
}
}
resetCellsVisited() {
for (let row = 0; row < this.numRows; row++) {
for (let col = 0; col < this.numCols; col++) {
if (!this.process) {
return;
}
this.cells[row][col].visited = false;
}
}
}
solve() {
this.resetCellsVisited();
this.restoreCanvas();
return this.solveRecursive(this.start[0], this.start[1]);
}
async solveRecursive(i, j) {
this.cells[i][j].visited = true;
await this.animate(100);
if ((i === this.end[0] && j === this.end[1]) || !this.process) {
return true;
}
let deadEnd = 0;
// Down
if (i < this.numRows - 1 && !this.cells[i + 1][j].visited && !this.cells[i][j].hasBottomWall) {
this.cells[i][j].drawMove(this.cells[i + 1][j]);
if (await this.solveRecursive(i + 1, j)) {
return true;
} else {
this.cells[i][j].drawMove(this.cells[i + 1][j], true);
}
} else {
deadEnd++;
}
// Right
if (j < this.numCols - 1 && !this.cells[i][j + 1].visited && !this.cells[i][j].hasRightWall) {
this.cells[i][j].drawMove(this.cells[i][j + 1]);
if (await this.solveRecursive(i, j + 1)) {
return true;
} else {
this.cells[i][j].drawMove(this.cells[i][j + 1], true);
}
} else {
deadEnd++;
}
// Left
if (j > 0 && !this.cells[i][j - 1].visited && !this.cells[i][j - 1].hasRightWall) {
this.cells[i][j].drawMove(this.cells[i][j - 1]);
if (await this.solveRecursive(i, j - 1)) {
return true;
} else {
this.cells[i][j].drawMove(this.cells[i][j - 1], true);
}
} else {
deadEnd++;
}
// Up
if (i > 0 && !this.cells[i - 1][j].visited && !this.cells[i - 1][j].hasBottomWall) {
this.cells[i][j].drawMove(this.cells[i - 1][j]);
if (await this.solveRecursive(i - 1, j)) {
return true;
} else {
this.cells[i][j].drawMove(this.cells[i - 1][j], true);
}
} else {
deadEnd++;
}
if (deadEnd === 4 && this.process) {
this.cells[i][j].drawEmoji("💥");
}
// Backtrack as no path found
return false;
}
}