-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
436 lines (363 loc) · 13.3 KB
/
script.js
File metadata and controls
436 lines (363 loc) · 13.3 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
const grid = document.getElementById('grid');
const rows = 18; // Ensure rows match the grid styling
const cols = 18; // Ensure columns match the grid styling
let startNode = null;
let endNode = null;
const runButton = document.getElementById('run-btn');
const algorithmSelect = document.getElementById('algorithm-select');
const spinner = document.getElementById('spinner');
const resetButton = document.getElementById('reset-btn');
const clearPathButton = document.getElementById('clear-path-btn');
const themeToggleButton = document.getElementById('theme-toggle-btn');
let isMouseDown = false;
// Create grid
function createGrid() {
grid.innerHTML = '';
for (let row = 0; row < rows; row++) {
for (let col = 0; col < cols; col++) {
const cell = document.createElement('div');
cell.classList.add('grid-cell');
cell.dataset.row = row;
cell.dataset.col = col;
cell.addEventListener('mousedown', handleCellMouseDown);
cell.addEventListener('mouseover', handleCellMouseOver);
cell.addEventListener('mouseup', handleCellMouseUp);
grid.appendChild(cell);
}
}
}
// Handle mouse down
function handleCellMouseDown(event) {
const cell = event.target;
isMouseDown = true;
if (!startNode) {
cell.classList.add('start');
startNode = cell;
} else if (!endNode) {
cell.classList.add('end');
endNode = cell;
} else {
cell.classList.toggle('wall');
}
}
// Handle mouse over
function handleCellMouseOver(event) {
if (isMouseDown && startNode && endNode) {
const cell = event.target;
cell.classList.add('wall');
}
}
// Handle mouse up
function handleCellMouseUp() {
isMouseDown = false;
}
// Add event listener to the document to handle mouse up globally
document.addEventListener('mouseup', () => {
isMouseDown = false;
});
// Helper function to get neighbors
function getNeighbors(cell) {
const row = parseInt(cell.dataset.row);
const col = parseInt(cell.dataset.col);
const neighbors = [];
[[0, 1], [1, 0], [0, -1], [-1, 0]].forEach(([dx, dy]) => {
const neighbor = document.querySelector(
`.grid-cell[data-row="${row + dx}"][data-col="${col + dy}"]`
);
if (neighbor && !neighbor.classList.contains('wall')) {
neighbors.push(neighbor);
}
});
return neighbors;
}
// BFS Algorithm
async function bfs(start, end) {
const queue = [start];
const visited = new Set();
const parentMap = new Map();
visited.add(start);
while (queue.length > 0) {
const current = queue.shift();
if (current === end) break;
current.classList.add('visited');
await new Promise(resolve => setTimeout(resolve, 50)); // Delay for visualization
for (const neighbor of getNeighbors(current)) {
if (!visited.has(neighbor)) {
visited.add(neighbor);
parentMap.set(neighbor, current);
queue.push(neighbor);
}
}
}
// Trace back the path
let current = end;
while (current && current !== start) {
current.classList.add('path');
current = parentMap.get(current);
await new Promise(resolve => setTimeout(resolve, 50)); // Delay for visualization
}
}
// Helper function to show a celebration
function showCelebration() {
alert('Pathfinding Complete! 🎉');
}
// Depth-First Search (DFS)
async function dfs(start, end) {
const stack = [start];
const visited = new Set();
const parentMap = new Map();
visited.add(start);
while (stack.length > 0) {
const current = stack.pop();
if (current === end) break;
current.classList.add('visited');
await new Promise(resolve => setTimeout(resolve, 50)); // Delay for visualization
for (const neighbor of getNeighbors(current)) {
if (!visited.has(neighbor)) {
visited.add(neighbor);
parentMap.set(neighbor, current);
stack.push(neighbor);
}
}
}
// Trace back the path
let current = end;
while (current && current !== start) {
current.classList.add('path');
current = parentMap.get(current);
await new Promise(resolve => setTimeout(resolve, 50)); // Delay for visualization
}
}
// Dijkstra's Algorithm
async function dijkstra(start, end) {
const distances = new Map();
const parentMap = new Map();
const visited = new Set();
const priorityQueue = [{ node: start, distance: 0 }];
distances.set(start, 0);
while (priorityQueue.length > 0) {
priorityQueue.sort((a, b) => a.distance - b.distance);
const { node: current } = priorityQueue.shift();
if (visited.has(current)) continue;
visited.add(current);
current.classList.add('visited');
await new Promise(resolve => setTimeout(resolve, 50)); // Delay for visualization
if (current === end) break;
for (const neighbor of getNeighbors(current)) {
const newDist = (distances.get(current) || 0) + 1;
if (newDist < (distances.get(neighbor) || Infinity)) {
distances.set(neighbor, newDist);
parentMap.set(neighbor, current);
priorityQueue.push({ node: neighbor, distance: newDist });
}
}
}
// Trace back the path
let current = end;
while (current && current !== start) {
current.classList.add('path');
current = parentMap.get(current);
await new Promise(resolve => setTimeout(resolve, 50)); // Delay for visualization
}
}
// A* Search
async function astar(start, end) {
const distances = new Map();
const parentMap = new Map();
const visited = new Set();
const priorityQueue = [{ node: start, fScore: 0 }];
distances.set(start, 0);
function heuristic(node) {
const [row1, col1] = [parseInt(node.dataset.row), parseInt(node.dataset.col)];
const [row2, col2] = [parseInt(end.dataset.row), parseInt(end.dataset.col)];
return Math.abs(row1 - row2) + Math.abs(col1 - col2);
}
while (priorityQueue.length > 0) {
priorityQueue.sort((a, b) => a.fScore - b.fScore);
const { node: current } = priorityQueue.shift();
if (visited.has(current)) continue;
visited.add(current);
current.classList.add('visited');
await new Promise(resolve => setTimeout(resolve, 50)); // Delay for visualization
if (current === end) break;
for (const neighbor of getNeighbors(current)) {
const newDist = (distances.get(current) || 0) + 1;
if (newDist < (distances.get(neighbor) || Infinity)) {
distances.set(neighbor, newDist);
parentMap.set(neighbor, current);
const fScore = newDist + heuristic(neighbor);
priorityQueue.push({ node: neighbor, fScore });
}
}
}
// Trace back the path
let current = end;
while (current && current !== start) {
current.classList.add('path');
current = parentMap.get(current);
await new Promise(resolve => setTimeout(resolve, 50)); // Delay for visualization
}
}
// Greedy Best-First Search
async function greedyBestFirstSearch(start, end) {
const visited = new Set();
const parentMap = new Map();
const priorityQueue = [{ node: start, heuristic: 0 }];
function heuristic(node) {
const [row1, col1] = [parseInt(node.dataset.row), parseInt(node.dataset.col)];
const [row2, col2] = [parseInt(end.dataset.row), parseInt(end.dataset.col)];
return Math.abs(row1 - row2) + Math.abs(col1 - col2);
}
while (priorityQueue.length > 0) {
priorityQueue.sort((a, b) => a.heuristic - b.heuristic);
const { node: current } = priorityQueue.shift();
if (visited.has(current)) continue;
visited.add(current);
current.classList.add('visited');
await new Promise(resolve => setTimeout(resolve, 50)); // Delay for visualization
if (current === end) break;
for (const neighbor of getNeighbors(current)) {
if (!visited.has(neighbor)) {
parentMap.set(neighbor, current);
priorityQueue.push({ node: neighbor, heuristic: heuristic(neighbor) });
}
}
}
// Trace back the path
let current = end;
while (current && current !== start) {
current.classList.add('path');
current = parentMap.get(current);
await new Promise(resolve => setTimeout(resolve, 50)); // Delay for visualization
}
}
// Bidirectional Search
async function bidirectionalSearch(start, end) {
const startQueue = [start];
const endQueue = [end];
const startVisited = new Set();
const endVisited = new Set();
const startParentMap = new Map();
const endParentMap = new Map();
startVisited.add(start);
endVisited.add(end);
while (startQueue.length > 0 && endQueue.length > 0) {
// Expand from the start side
const currentStart = startQueue.shift();
currentStart.classList.add('visited');
await new Promise(resolve => setTimeout(resolve, 50)); // Delay for visualization
for (const neighbor of getNeighbors(currentStart)) {
if (!startVisited.has(neighbor)) {
startVisited.add(neighbor);
startParentMap.set(neighbor, currentStart);
startQueue.push(neighbor);
if (endVisited.has(neighbor)) {
return traceBidirectionalPath(neighbor, startParentMap, endParentMap, start, end);
}
}
}
// Expand from the end side
const currentEnd = endQueue.shift();
currentEnd.classList.add('visited');
await new Promise(resolve => setTimeout(resolve, 50)); // Delay for visualization
for (const neighbor of getNeighbors(currentEnd)) {
if (!endVisited.has(neighbor)) {
endVisited.add(neighbor);
endParentMap.set(neighbor, currentEnd);
endQueue.push(neighbor);
if (startVisited.has(neighbor)) {
return traceBidirectionalPath(neighbor, startParentMap, endParentMap, start, end);
}
}
}
}
}
// Helper function to trace the path for bidirectional search
async function traceBidirectionalPath(meetingNode, startParentMap, endParentMap, start, end) {
let current = meetingNode;
// Trace back to the start
while (current && current !== start) {
current.classList.add('path');
current = startParentMap.get(current);
await new Promise(resolve => setTimeout(resolve, 50)); // Delay for visualization
}
current = meetingNode;
// Trace back to the end
while (current && current !== end) {
current.classList.add('path');
current = endParentMap.get(current);
await new Promise(resolve => setTimeout(resolve, 50)); // Delay for visualization
}
}
// Run Algorithm with Spinner and Celebration
async function runAlgorithm() {
if (!startNode || !endNode) {
alert('Please place both a start and an end node.');
return;
}
spinner.style.display = 'inline-block'; // Show spinner
const algorithm = algorithmSelect.value;
switch (algorithm) {
case 'bfs':
await bfs(startNode, endNode);
break;
case 'dfs':
await dfs(startNode, endNode);
break;
case 'dijkstra':
await dijkstra(startNode, endNode);
break;
case 'astar':
await astar(startNode, endNode);
break;
case 'greedy':
await greedyBestFirstSearch(startNode, endNode);
break;
case 'bidirectional':
await bidirectionalSearch(startNode, endNode);
break;
default:
console.error('Unknown algorithm selected.');
}
spinner.style.display = 'none'; // Hide spinner
showCelebration(); // Show celebration
}
// Reset the entire grid
function resetGrid() {
startNode = null;
endNode = null;
grid.querySelectorAll('.grid-cell').forEach(cell => {
cell.className = 'grid-cell';
});
}
// Clear only the path and visited cells
function clearPath() {
grid.querySelectorAll('.visited, .path').forEach(cell => {
cell.classList.remove('visited', 'path');
});
}
// Toggle dark/light mode
function toggleTheme() {
const body = document.body;
body.classList.toggle('light'); // Toggle the 'light' class
localStorage.setItem('theme', body.classList.contains('light') ? 'light' : 'dark'); // Save theme preference
}
// Apply saved theme on load
function applySavedTheme() {
const savedTheme = localStorage.getItem('theme');
if (savedTheme === 'light') {
document.body.classList.add('light');
}
}
// Add event listener for the theme toggle button
themeToggleButton.addEventListener('click', toggleTheme);
// Apply theme on page load
applySavedTheme();
// Add event listeners for the buttons
resetButton.addEventListener('click', resetGrid);
clearPathButton.addEventListener('click', clearPath);
themeToggleButton.addEventListener('click', toggleTheme);
// Add event listener to the "Run" button
runButton.addEventListener('click', runAlgorithm);
// Initialize
createGrid();