-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmazeScript.js
More file actions
335 lines (275 loc) · 8.65 KB
/
mazeScript.js
File metadata and controls
335 lines (275 loc) · 8.65 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
const DEFAULT_COLOR = '#87ceeb'
const VISITED_COLOR = '#ff0000'
const HEIGHT = 5;
const WIDTH = 5;
const nodeCoords = [];
const edgeList = [];
/* --------------------------------------- */
// set edgelist
for (let i = 0; i <= HEIGHT * WIDTH; i++) {
nodeCoords[i] = {};
edgeList[i] = [];
}
// order: clockwise
edgeList[1] = [2, 6];
edgeList[2] = [3, 7, 1];
edgeList[3] = [4, 8, 2];
edgeList[4] = [5, 9, 3];
edgeList[5] = [10, 4];
edgeList[6] = [1, 11];
edgeList[7] = [2];
edgeList[8] = [3, 13];
edgeList[9] = [4, 14];
edgeList[10] = [5, 15];
edgeList[11] = [6, 12, 16];
edgeList[12] = [13, 17, 11];
edgeList[13] = [8, 18, 12];
edgeList[14] = [9, 15];
edgeList[15] = [10, 20, 14];
edgeList[16] = [11, 21];
edgeList[17] = [12, 22];
edgeList[18] = [13];
edgeList[19] = [20, 24];
edgeList[20] = [15, 19];
edgeList[21] = [16, 22];
edgeList[22] = [17, 23, 21];
edgeList[23] = [22];
edgeList[24] = [19, 25];
edgeList[25] = [24];
/* --------------------------------------- */
// draw functions
function drawInitialMaze() {
let canvas = document.getElementById('canvasId');
let ctx = canvas.getContext('2d');
ctx.clearRect(0, 0, canvas.width, canvas.height);
let nodeIndex = 1;
for (let i = 1; i <= HEIGHT; i++) {
for (let j = 1; j <= WIDTH; j++) {
// set node coordinates
let x = canvas.width * (j / (WIDTH+1));
let y = canvas.height * (i/ (HEIGHT+1));
nodeCoords[nodeIndex].x = x;
nodeCoords[nodeIndex].y = y;
// draw node circle
ctx.fillStyle = DEFAULT_COLOR;
ctx.beginPath();
ctx.arc(nodeCoords[nodeIndex].x, nodeCoords[nodeIndex].y, canvas.width / 22, 0, Math.PI * 2, true);
ctx.fill();
// draw circle outline
ctx.strokeStyle = "#222222";
ctx.lineWidth = 3;
ctx.beginPath();
ctx.arc(nodeCoords[nodeIndex].x, nodeCoords[nodeIndex].y, canvas.width / 21, 0, Math.PI * 2, true);
ctx.stroke();
// write node number
ctx.fillStyle = "#000000";
ctx.font = "30px serif";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText(nodeIndex, nodeCoords[nodeIndex].x, nodeCoords[nodeIndex].y);
nodeIndex++;
}
}
// draw lines between nodes. skip this process when i == 0 (root node)
ctx.globalCompositeOperation = "destination-over"; // draw lines behind the nodes
for (let i = 1; i <= HEIGHT * WIDTH; i++) {
ctx.strokeStyle = "#000000";
ctx.lineWidth = 5;
// check adjacent nodes
for (let j of edgeList[i]) {
ctx.beginPath();
ctx.moveTo(nodeCoords[i].x, nodeCoords[i].y);
ctx.lineTo(nodeCoords[j].x, nodeCoords[j].y);
ctx.stroke();
}
}
ctx.globalCompositeOperation = "source-over";
}
function drawPath(reachable) {
canvas = document.getElementById('canvasId');
let ctx = canvas.getContext('2d');
for (let i = 0; i < reachable.length; i++) {
let current = reachable[i];
// draw node circle
ctx.fillStyle = VISITED_COLOR;
ctx.beginPath();
ctx.arc(nodeCoords[current].x, nodeCoords[current].y, canvas.width / 22, 0, Math.PI * 2, true);
ctx.fill();
// write node number
ctx.fillStyle = "#000000";
ctx.font = "30px serif";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText(current, nodeCoords[current].x, nodeCoords[current].y);
}
}
// function DFS(start, end) {
// let reachable = [];
// let stack = [];
// stack.push(start);
// while (stack.length != 0) {
// let currentNode = stack[stack.length - 1];
// stack.pop();
// // if already visited, skip
// if (reachable.indexOf(currentNode) >= 0)
// continue;
// reachable.push(currentNode);
// // if reaches goal
// if (currentNode === end)
// break;
// // check adjacent nodes and add them to the container
// for (let adjNode of edgeList[currentNode]) {
// stack.push(adjNode);
// }
// }
// console.log("DFS: " + reachable);
// }
// DFS(1, 25);
// function BFS(start, end) {
// let reachable = [];
// let queue = [];
// queue.push(start);
// while (queue.length != 0) {
// let currentNode = queue[0];
// queue.shift();
// // if already visited, skip
// if (reachable.indexOf(currentNode) >= 0)
// continue;
// reachable.push(currentNode);
// // if reaches goal
// if (currentNode === end)
// break;
// // check adjacent nodes and add them to the container
// for (let adjNode of edgeList[currentNode]) {
// queue.push(adjNode);
// }
// }
// console.log("BFS: " + reachable);
// }
// BFS(1, 25);
function stepDFS() {
let log = document.getElementById('log');
if (reachable.length === 25) {
log.textContent = `Visited all nodes`;
return;
}
if (stack.length === 0) {
log.textContent = `Stack is empty`;
return;
}
currentNode = stack.pop();
log.textContent = `Pop ${currentNode}. `;
// reachable contains currentNode, skip
if (reachable.indexOf(currentNode) >= 0) {
log.textContent += `Already visited`;
return;
}
reachable.push(currentNode);
// if reaches goal
if (currentNode === goal)
log.textContent += `Reached goal`;
numOfPush = 0;
// check adjacent nodes and add them to the container
for (let adjNode of edgeList[currentNode]) {
if (reachable.indexOf(adjNode) >= 0)
continue;
stack.push(adjNode);
numOfPush++;
}
console.log("Reachable: " + reachable);
drawPath(reachable);
}
function stepBFS() {
let log = document.getElementById('log');
if (reachable.length === 25) {
log.textContent = `Visited all nodes`;
return;
}
if (queue.length === 0) {
log.textContent = `queue is empty`;
return;
}
currentNode = queue.shift();
log.textContent = `Pop ${currentNode}. `;
// reachable contains currentNode, skip
if (reachable.indexOf(currentNode) >= 0) {
log.textContent += `Already visited`;
return;
}
reachable.push(currentNode);
// if reaches goal
if (currentNode === goal)
log.textContent += `Reached goal`;
numOfPush = 0;
// check adjacent nodes and add them to the container
for (let adjNode of edgeList[currentNode]) {
if (reachable.indexOf(adjNode) >= 0)
continue;
queue.push(adjNode);
numOfPush++;
}
console.log("Reachable: " + reachable);
drawPath(reachable);
}
// global variables
let start;
let goal;
let reachable;
let stack;
let queue;
let currentNode;
let algorithm;
let numOfPush = 0;
function reset() {
start = 1;
goal = 25;
reachable = [];
stack = [start];
queue = [start];
algorithm = 'dfs';
drawInitialMaze();
document.getElementById('log').textContent = "";
document.getElementById('reachable').textContent = `{${reachable}}`;
document.getElementById('stack').textContent = `{${stack}}`;
document.getElementById('queue').textContent = `{${queue}}`;
}
document.getElementById('reset').addEventListener('click', function() {
reset();
document.getElementById('algorithmSelect').algorithm[0].checked = "ckecked";
});
document.getElementById('step').addEventListener('click', function() {
if (algorithm === 'dfs') {
stepDFS();
let stackLog = [];
for (let i in stack) {
if (i >= stack.length - numOfPush) {
stackLog[i] = `<b>${stack[i]}</b>`;
}
else {
stackLog[i] = `${stack[i]}`;
}
}
document.getElementById('stack').innerHTML = `{${stackLog}}`;
}
else if (algorithm === 'bfs') {
stepBFS();
let queueLog = [];
for (let i in queue) {
if (i >= queue.length - numOfPush) {
queueLog[i] = `<b>${queue[i]}</b>`;
}
else {
queueLog[i] = `${queue[i]}`;
}
}
document.getElementById('queue').innerHTML = `{${queueLog}}`;
}
document.getElementById('reachable').innerHTML = `{${reachable}}`;
});
document.getElementById('algorithmSelect').addEventListener('change', function() {
reset();
algorithm = this.algorithm.value;
document.getElementById('log').textContent = `Mode: ${algorithm}`;
})
// starts from here
reset();