-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfun.js
More file actions
484 lines (418 loc) · 16.8 KB
/
fun.js
File metadata and controls
484 lines (418 loc) · 16.8 KB
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
function Mine(canvas, ctx, gridWidth, gridHight, speed, cellSize) {
let dirEnum = { left: "left", right: "right", up: "up", down: "down", diagupleft: "diagupleft", diagdownleft: "diagdownleft", diagupright: "diagupright", diagdownright: "diagdownright" };
let PathArray = [];
let markedblocks = new Set();
let working = false;
let showCoordinates = false;
let Notdragging = true;
let Pickedtarget = null;
let PickedstartNode = null;
let Pickingstatus = { target: false, startNode: false };
let alreadymazed = false;
window.sizerange.onchange = (event) => {
cellSize = event.target.value;
grid.grid = [];
window.sizerangelabel.innerText = `Cell Size: ${cellSize} x ${cellSize}`;
grid = new Graph(gridWidth, gridHight, cellSize);
reset();
};
window.speedrange.onchange = (event) => {
speed = event.target.value;
window.speedrangelabel.innerText = `Drawing Delay: ${speed}ms`;
};
window.ShowCordinates.onchange = (event) => {
showCoordinates = event.target.checked;
reset();
};
window.onresize = () => {
canvas.width = innerWidth - innerWidth * 0.2;
canvas.height = innerHeight - innerHeight * 0.29;
gridWidth = canvas.width;
gridHight = canvas.height;
canvas.style.backgroundColor = "black";
grid.grid = [];
grid = new Graph(gridWidth, gridHight, cellSize);
reset();
};
window.performanceSpan.innerText = "Click to select a start node";
class Graph {
static graph = [];
constructor(w, h, cellSize) {
this.grid = [];
this.width = Math.floor((w - cellSize) / cellSize);
this.height = Math.floor((h - cellSize) / cellSize);
this.graph = {};
this.node = null;
this.makeGraph();
}
checkAndPush(X, Y, offsetX, offsetY, walldir) {
let newX = X + offsetX;
let newY = Y + offsetY;
let adjname = `${newX},${newY}`;
if (newX >= 0 && newX <= this.width && newY >= 0 && newY <= this.height) {
this.node.adj.push(adjname);
}
if (walldir) {
this.node.walls.push([walldir, adjname]);
}
}
makeGraph() {
for (let Y = 0; Y <= this.height; Y++) {
for (let X = 0; X <= this.width; X++) {
this.node = new Node(X, Y, cellSize);
this.grid.push(this.node);
this.checkAndPush(X, Y, 1, 0, dirEnum.right); // right
this.checkAndPush(X, Y, -1, 0, dirEnum.left); // left
this.checkAndPush(X, Y, 0, -1, dirEnum.up); // up
this.checkAndPush(X, Y, 0, 1, dirEnum.down); // down
// this.checkAndPush(X, Y, -1, -1, null); // diagupleft
// this.checkAndPush(X, Y, -1, 1, null); // diagdownleft
// this.checkAndPush(X, Y, 1, -1, null); // diagupright
// this.checkAndPush(X, Y, 1, 1, null); // diagdownright
}
}
}
}
class Node {
constructor(X, Y, cellSize) {
this.name = `${X},${Y}`;
this.X = X;
this.Y = Y;
this.offsetX = X * cellSize;
this.offsetY = Y * cellSize - cellSize * 0.29;
this.adj = [];
this.cellSize = cellSize;
this.walls = [];
this.distance = 0;
this.highlight = false;
this.targeted = false;
this.spacebetween = 1;
}
draw() {
const walls = this.walls.map((arr) => arr[0]);
if (showCoordinates) {
ctx.beginPath();
ctx.font = "10px Arial";
ctx.fillStyle = "red";
ctx.fillText(`${this.X},${this.Y}`, this.offsetX + this.cellSize * 0.3, this.offsetY + this.cellSize * 0.8);
}
if (this.highlight || this.targeted) {
ctx.save();
ctx.beginPath();
ctx.globalAlpha = this.targeted ? 0.2 : 0.5;
ctx.fillStyle = this.targeted ? "red" : "green";
ctx.fillRect(this.offsetX + this.cellSize * 0.05, this.offsetY + this.cellSize * 0.3, this.cellSize, this.cellSize);
ctx.restore();
}
///// this is for testing
if (markedblocks.has(this.name)) {
ctx.save();
ctx.beginPath();
ctx.globalAlpha = this.targeted ? 0.2 : 0.5;
ctx.fillStyle = "gray";
ctx.fillRect(this.offsetX + this.cellSize * 0.05, this.offsetY + this.cellSize * 0.3, this.cellSize, this.cellSize);
ctx.restore();
}
ctx.save();
ctx.strokeStyle = "green";
ctx.lineWidth = 0.5;
ctx.translate(this.offsetX + this.cellSize * 0.05, this.offsetY + this.cellSize * 0.3);
//top
if (walls.includes(dirEnum.up)) {
ctx.beginPath();
ctx.moveTo(0, 0);
ctx.lineTo(this.cellSize - this.spacebetween, 0);
ctx.stroke();
}
//right
if (walls.includes(dirEnum.right)) {
ctx.beginPath();
ctx.moveTo(this.cellSize - this.spacebetween, 0);
ctx.lineTo(this.cellSize - this.spacebetween, this.cellSize - this.spacebetween);
ctx.stroke();
}
//left
if (walls.includes(dirEnum.left)) {
ctx.beginPath();
ctx.moveTo(0, this.cellSize - this.spacebetween);
ctx.lineTo(0, 0);
ctx.stroke();
}
// bottom
if (walls.includes(dirEnum.down)) {
ctx.beginPath();
ctx.moveTo(0, this.cellSize - this.spacebetween);
ctx.lineTo(this.cellSize, this.cellSize - this.spacebetween);
ctx.stroke();
}
ctx.restore();
}
}
let grid = new Graph(gridWidth, gridHight, cellSize);
window.opensettingbtn.onclick = () => {
window.settings.style.display = "flex";
};
window.closesettingbtn.onclick = () => {
window.settings.style.display = "none";
};
window.mazebtn.onclick = (event) => {
if (working) return;
if (alreadymazed) HardReset();
window.settings.style.display = "none";
let rect = canvas.getBoundingClientRect();
let X = Math.floor((event.clientX - rect.left) / cellSize);
let Y = Math.floor((event.clientY - rect.top) / cellSize);
depthFirstMaze(grid.grid);
FindIt(PathArray);
};
canvas.onpointermove = (event) => {
if (event.buttons == 1) {
Notdragging = false;
let markblocks = window.markblocks.checked;
if (markblocks) {
let rect = canvas.getBoundingClientRect();
let X = Math.floor((event.clientX - rect.left) / cellSize);
let Y = Math.floor((event.clientY - rect.top) / cellSize);
let cell = grid.grid.find((cell) => cell.name == `${X},${Y}`);
markedblocks.add(`${X},${Y}`);
if (cell) cell.draw();
}
}
};
canvas.onpointerup = (e) => {
setTimeout(() => {
Notdragging = true;
}, 20);
};
canvas.onpointerdown = (event) => {
if (working) return;
window.settings.style.display = "none";
PathArray = [];
const bfsbtn = window.bfs.checked;
const dfsbtn = window.dfs.checked;
let markblocks = window.markblocks.checked;
let rect = canvas.getBoundingClientRect();
let X = Math.floor((event.clientX - rect.left) / cellSize);
let Y = Math.floor((event.clientY - rect.top) / cellSize);
if (!markblocks) {
if (Pickingstatus.startNode == true && PickedstartNode.name == `${X},${Y}`) {
reset();
window.performanceSpan.innerText = "Click to select a start node";
console.log("start node deleted");
} else if (Pickingstatus.startNode == false) {
PickedstartNode = grid.grid.find((cell) => cell.name == `${X},${Y}`);
Pickingstatus.startNode = true;
PickedstartNode.highlight = true;
PickedstartNode.draw();
window.performanceSpan.innerText = "Click to select a target node";
} else if (Pickingstatus.startNode && Pickingstatus.target == false) {
Pickedtarget = grid.grid.find((cell) => cell.name == `${X},${Y}`);
Pickedtarget.targeted = true;
Pickedtarget.draw();
}
}
if (Pickedtarget && PickedstartNode && !markblocks) {
if (bfsbtn) {
performance.mark("start");
breadthFirst(grid.grid, PickedstartNode.name, { X, Y });
performance.mark("end");
window.performanceSpan.innerText = `Performance: ${Math.floor(performance.measure("bfs", "start", "end").duration)}ms`;
} else if (dfsbtn) {
performance.mark("start");
depthFirst(grid.grid, PickedstartNode.name, { X, Y });
performance.mark("end");
window.performanceSpan.innerText = `Performance: ${Math.floor(performance.measure("dfs", "start", "end").duration)}ms`;
} else {
console.log("error");
}
FindIt(PathArray);
}
if (markblocks && Notdragging) {
if (markedblocks.has(`${X},${Y}`)) {
markedblocks.delete(`${X},${Y}`);
let cell = grid.grid.find((cell) => cell.name == `${X},${Y}`);
if (cell) cell.draw();
reset();
}
}
};
function reset() {
working = false;
ctx.clearRect(0, 0, canvas.width, canvas.height);
grid.grid.forEach((cell) => {
cell.highlight = false;
cell.targeted = false;
cell.draw();
});
PathArray = [];
Pickedtarget = null;
startNode = null;
Pickingstatus = { target: false, startNode: false };
window.performanceSpan.innerText = "Click to select a start node";
}
function HardReset() {
grid = new Graph(gridWidth, gridHight, cellSize);
grid.grid.forEach((cell) => {
cell.draw();
});
alreadymazed = false;
}
reset();
function depthFirstMaze(graph) {
if (alreadymazed) return;
let getRandomNeighbor = (node) => {
let neighbors = node.adj.filter((adjNodeName) => !visited[adjNodeName]);
if (neighbors.length === 0) {
return null;
}
let randomIndex = Math.floor(Math.random() * neighbors.length);
return graph.find((n) => n.name === neighbors[randomIndex]);
};
let startNode = graph[0];
let stack = [startNode];
let visited = { [startNode.name]: true };
PathArray.push(startNode);
while (stack.length > 0) {
let currentNode = stack[stack.length - 1];
PathArray.push(currentNode);
let randomNeighbor = getRandomNeighbor(currentNode);
if (!randomNeighbor) {
stack.pop();
continue;
}
let currnetsharedWall = currentNode.walls.find((wall) => wall.includes(randomNeighbor.name));
let neighborsharedWall = randomNeighbor.walls.find((wall) => wall.includes(currentNode.name));
if (currnetsharedWall && neighborsharedWall) {
currentNode.walls = currentNode.walls.filter((wall) => wall !== currnetsharedWall);
randomNeighbor.walls = randomNeighbor.walls.filter((wall) => wall !== neighborsharedWall);
visited[randomNeighbor.name] = true;
stack.push(randomNeighbor);
}
}
alreadymazed = true;
}
function sleep(milliseconds) {
return new Promise((resolve) => setTimeout(resolve, milliseconds));
}
async function FindIt(arr) {
working = true;
window.sizerange.disabled = true;
window.mazebtn.disabled = true;
for (const cell of arr) {
cell.highlight = true;
cell.draw();
await sleep(speed);
}
await sleep(1500);
window.sizerange.disabled = false;
window.mazebtn.disabled = false;
reset();
}
function breadthFirst(graph, source, target) {
let startNode = graph.find((node) => node.name == source);
if (!startNode) {
console.log("couldnt find startNode");
return;
}
let queue = [startNode];
let visited = { [startNode.name]: true };
let { X: targetX, Y: targetY } = target;
let distances = { [source]: 0 };
let previous = { [source]: null };
let path = [];
while (queue.length > 0) {
let current = queue.shift();
if (markedblocks.has(current.name)) {
continue;
}
if (current.X === targetX && current.Y === targetY) {
console.log("found it");
while (current !== null) {
path.unshift(current);
current = previous[current.name];
}
PathArray = path;
return;
}
PathArray.push(current);
for (const neighbor of current.adj) {
if (alreadymazed) {
let currnetsharedWall = current.walls.find((wall) => wall.includes(neighbor));
if (currnetsharedWall) {
continue;
}
}
if (!visited[neighbor]) {
visited[neighbor] = true;
let neighborNode = graph.find((node) => node.name === neighbor);
queue.push(neighborNode);
let distance = distances[current.name] + 1;
if (!distances[neighborNode.name] || distance < distances[neighborNode.name]) {
distances[neighborNode.name] = distance;
previous[neighborNode.name] = current;
}
}
}
}
console.log("didnt find it");
return null;
}
function depthFirst(graph, source, target) {
let startNode = graph.find((node) => node.name == source);
if (!startNode) {
console.log("couldnt find startNode");
return;
}
let stack = [startNode];
let visited = { [startNode.name]: true };
let { X, Y } = target;
let distances = { [source]: 0 };
let previous = { [source]: null };
let path = [];
while (stack.length > 0) {
let current = stack.pop();
if (markedblocks.has(current.name)) {
continue;
}
if (current.X == X && current.Y == Y) {
console.log("found it");
while (current !== null) {
path.unshift(current);
current = previous[current.name];
}
PathArray = path;
return;
}
PathArray.push(current);
for (const neighbor of current.adj) {
if (alreadymazed) {
let currnetsharedWall = current.walls.find((wall) => wall.includes(neighbor));
if (currnetsharedWall) {
continue;
}
}
if (!visited[neighbor]) {
visited[neighbor] = true;
stack.push(graph.find((arr) => arr.name == neighbor));
let distanceToNeighbor = distances[current.name] + 1;
if (!distances[neighbor] || distanceToNeighbor < distances[neighbor]) {
distances[neighbor] = distanceToNeighbor;
previous[neighbor] = current;
}
}
}
// console.log(stack.map((e) => e.name));
}
console.log("didnt find it");
}
}
let canvas = document.getElementById("Mycanvas");
let ctx = canvas.getContext("2d");
canvas.width = innerWidth - innerWidth * 0.2;
canvas.height = innerHeight - innerHeight * 0.29;
canvas.style.backgroundColor = "black";
let cellSize = window.sizerange.value;
let gridWidth = canvas.width;
let gridHight = canvas.height;
let speed = window.speedrange.value;
Mine(canvas, ctx, gridWidth, gridHight, speed, cellSize, true);