-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
300 lines (250 loc) · 7.87 KB
/
script.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
const canvas = document.getElementById("canvas-grid");
const gridContainer = document.querySelector(".grid-container");
const form = document.querySelector(".form");
const placeStartBtn = document.getElementById("place-start");
const placeEndBtn = document.getElementById("place-end");
const placeWallBtn = document.getElementById("place-walls");
const runAlgorithmBtn = document.getElementById("run-algorithm");
const resetBtn = document.getElementById("reset");
/* CONSTANTS & Global variables*/
const cellSize = 30;
const rows = 40;
const cols = 40;
let grid;
let isPlacingStart = false;
let isPlacingEnd = false;
let isPlacingWalls = false;
// Functions
function setCanvasDimensions() {
canvas.height = gridContainer.clientHeight;
canvas.width = gridContainer.clientWidth;
}
function initializeGrid() {
const grid = [];
for (let row = 0; row < rows; row++) {
const currentRow = [];
for (let col = 0; col < cols; col++) {
currentRow.push({
row,
col,
isStart: false,
isEnd: false,
isWall: false,
isVisited: false,
isPath: false,
previousNode: null,
distance: Infinity,
});
}
grid.push(currentRow);
}
return grid;
}
grid = initializeGrid();
function drawGrid() {
const ctx = canvas.getContext("2d");
if (!ctx) {
console.error("Canvas context is not available.");
return;
}
ctx.clearRect(0, 0, canvas.width, canvas.height);
const cellWidth = canvas.width / cols;
const cellHeight = canvas.height / rows;
for (let row = 0; row < rows; row++) {
for (let col = 0; col < cols; col++) {
const cell = grid[row][col];
const x = col * cellWidth;
const y = row * cellHeight;
// Draw cell background
if (cell.isStart) ctx.fillStyle = "green";
else if (cell.isEnd) ctx.fillStyle = "red";
else if (cell.isPath) ctx.fillStyle = "yellow";
else if (cell.isVisited) ctx.fillStyle = "lightblue";
else if (cell.isWall) ctx.fillStyle = "black";
else ctx.fillStyle = "white";
ctx.fillRect(x, y, cellWidth, cellHeight);
// Draw cell borders
ctx.strokeStyle = "grey";
ctx.strokeRect(x, y, cellWidth, cellHeight);
}
}
}
// Function to handle node placement
function handleNodePlacement(e) {
const rect = canvas.getBoundingClientRect();
const mouseX = e.clientX - rect.left;
const mouseY = e.clientY - rect.top;
const row = Math.floor(mouseY / (canvas.height / rows));
const col = Math.floor(mouseX / (canvas.width / cols));
if (row >= 0 && row < rows && col >= 0 && col < cols) {
// cell or node
const cell = grid[row][col];
if (isPlacingStart && !cell.isWall && !cell.isEnd) {
grid.forEach((row) => {
row.forEach((cell) => {
cell.isStart = false;
});
});
cell.isStart = true;
} else if (isPlacingEnd && !cell.isWall && !cell.isStart) {
grid.forEach((row) => {
row.forEach((cell) => {
cell.isEnd = false;
});
});
cell.isEnd = true;
} else if (isPlacingWalls && !cell.isStart && !cell.isEnd) {
cell.isWall = !cell.isWall; // Toggle wall
}
setCanvasDimensions();
drawGrid();
}
}
/* DIJKSTRA ALGORITHM */
function getAllNodes(grid) {
const nodes = [];
for (const row of grid) {
for (const node of row) nodes.push(node);
}
return nodes;
}
function getNeighbors(node, grid) {
const neighbors = [];
const { row, col } = node;
if (row > 0) neighbors.push(grid[row - 1][col]); //up
if (row < grid.length - 1) neighbors.push(grid[row + 1][col]); //down
if (col > 0) neighbors.push(grid[row][col - 1]); //left
if (col < grid[0].length - 1) neighbors.push(grid[row][col + 1]); //right
return neighbors.filter(
(neighbor) => !neighbor.isWall && !neighbor.isVisited
);
}
// backtracing the shortest-path from endNode to startNode
function findShortestPath(endNode) {
const shortestPath = [];
let currentNode = endNode;
while (currentNode != null) {
shortestPath.unshift(currentNode); // add current node at the front
currentNode = currentNode.previousNode;
}
return shortestPath;
}
function dijkstra(startNode, endNode, grid) {
const visitedNodes = [];
const unvisitedNodes = getAllNodes(grid);
// startNode && endNode coming from when clicking on run-algo btn which calls visualizationAlgorithm func which calls for example dijkstra func
startNode.distance = 0;
while (unvisitedNodes.length > 0) {
unvisitedNodes.sort((a, b) => a.distance - b.distance); // simulating priority queue
const closestNode = unvisitedNodes.shift(); // extract first node of unvisitedNodes array
if (closestNode.isWall) continue; // disregard walls
if (closestNode.distance === Infinity) {
console.log("the end node is unreachable");
return { visitedNodes, shortestPath: [] };
}
closestNode.isVisited = true;
visitedNodes.push(closestNode);
if (closestNode === endNode) {
return { visitedNodes, shortestPath: findShortestPath(endNode) };
}
const neighbors = getNeighbors(closestNode, grid);
for (const neighbor of neighbors) {
const newDistance = closestNode.distance + 1;
if (newDistance < neighbor.distance) {
neighbor.distance = newDistance;
neighbor.previousNode = closestNode;
}
}
}
return { visitedNodes, shortestPath: [] };
}
/* Function to visualize the selected algorithem*/
async function visualizeAlgorithm(algorithm, grid, startNode, endNode) {
if (!algorithm) {
console.error("Algorithm not found or not implemented yet.");
return;
}
const { visitedNodes, shortestPath } = algorithm(startNode, endNode, grid);
grid.forEach((row) =>
row.forEach((cell) => {
cell.isVisited = false;
cell.isPath = false;
cell.distance = Infinity;
cell.previousNode = null;
})
);
// Highlight nodes
for (const node of visitedNodes) {
node.isVisited = true;
drawGrid();
await sleep(10); // Add a delay for visualization
}
if (shortestPath.length === 0 && visitedNodes.length > 0) {
console.log(`You shall not pass! ⚠`);
return;
}
for (const node of shortestPath) {
node.isPath = true;
drawGrid();
await sleep(30);
}
}
//function to add a delay
function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
function resetAll() {
grid = initializeGrid();
drawGrid();
}
// Map algorithm names to functions
const algorithms = {
dijkstra: dijkstra,
};
/* Buttons function handlers */
placeStartBtn.addEventListener("click", () => {
isPlacingStart = true;
isPlacingEnd = false;
isPlacingWalls = false;
});
placeEndBtn.addEventListener("click", () => {
isPlacingEnd = true;
isPlacingStart = false;
isPlacingWalls = false;
});
placeWallBtn.addEventListener("click", () => {
isPlacingWalls = true;
isPlacingStart = false;
isPlacingEnd = false;
});
runAlgorithmBtn.addEventListener("click", () => {
const startNode = grid.flat().find((cell) => cell.isStart);
const endNode = grid.flat().find((cell) => cell.isEnd);
if (startNode && endNode) {
// Get the selected algorithm
const selectedAlgorithm = document.getElementById("algorithm-select").value;
const algorithmFunction = algorithms[selectedAlgorithm]; // e.g dijkstra;
if (algorithmFunction) {
visualizeAlgorithm(algorithmFunction, grid, startNode, endNode);
} else {
console.error("Algorithm not found or not implemented yet.");
alert("The selected algorithm is not implemented yet.");
return;
}
} else {
alert("Please place both start and end nodes.");
}
});
resetBtn.addEventListener("click", resetAll);
canvas.addEventListener("mousedown", handleNodePlacement);
canvas.addEventListener("mousemove", (e) => {
if (isPlacingWalls && e.buttons === 1) {
handleNodePlacement(e);
}
});
function initializeApp() {
setCanvasDimensions();
drawGrid();
}
// Start the app
initializeApp();