-
Notifications
You must be signed in to change notification settings - Fork 0
/
class.js
373 lines (352 loc) · 11.3 KB
/
class.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
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
class Node
{
constructor(value, parent)
{
this.value = value;
this.parent= parent;
}
}
class Path
{
constructor()
{
this.tree = [];
}
// finds index of node by value
find(node)
{
for(let x in this.tree)
{
if(this.tree[x].value == node)
{
return x;
}
}
console.error("No match found in Path.find()");
return undefined;
}
addNode(value, parent)
{
let j = new Node(value, parent);
this.tree.push(j);
}
// creates pathway, in reverse order.
link(node, path)
{
path.push(node.value);
let index = this.find(node.parent); // finds index of parent node in tree
if(index == 0) // if index == 0 then it is the root, exit recursion
{
path.push(this.tree[index].value);
}
else {
this.link(this.tree[index], path);
}
}
findPath(endNode)
{
let pathway = [];
this.link(endNode, pathway);
return pathway.reverse();
}
}
class Queue
{
constructor()
{
this.priorityQ = [];
this.minIndex = 0;
}
// returns index of minimum value from priority queue
min()
{
let minPriority = this.priorityQ[0][0];
let minimum = this.priorityQ[0];
this.minIndex = 0;
for (let x in this.priorityQ)
{
if(this.priorityQ[x][0] < minPriority)
{
minimum = this.priorityQ[x];
this.minIndex = x;
}
}
return minimum;
}
push(nodeArray)
{
this.priorityQ.push(...nodeArray);
}
pop()
{
this.priorityQ.splice(this.minIndex, 1);
}
}
class Astar
{
constructor(startButton, currentBoard)
{
this.currentBoard = currentBoard;
startButton.addEventListener("click", ()=>{this.start()});
}
start()
{
// make sure there is a start and end
if(document.querySelector(".start") == null || document.querySelector('.end') == null)
{
window.alert("Please add a start and end block first");
return -1;
}
// declare path and queue and add start cell to both
let startCell = parseInt(document.querySelector(".start").id);
let endCell = parseInt(document.querySelector('.end').id);
let path = new Path();
let queue = new Queue();
let distanceOfStart = this.currentBoard.distanceFromCell(startCell, endCell);
queue.push([[distanceOfStart,startCell]]);
// find shortest path
while(queue.min()[1] != endCell)
{
let q = queue.min();
path.addNode(q[1], q[2]);
this.currentBoard.visit(q[1]);
let neighbors = this.currentBoard.neighborCells(q[1]);
// the step distance is not directly kept tracked of,
// Instead the distance of the parent cell to the end cell
// is subtracted from its priority to find the parent's step distance
let distanceOfParent = this.currentBoard.distanceFromCell(q[1], endCell);
let stepDistance = q[0] - distanceOfParent + 1;
let neighborNodes = neighbors.map(cell => {
let priority = stepDistance + this.currentBoard.distanceFromCell(cell, endCell);
return [priority, cell, q[1]];
});
queue.push(neighborNodes);
queue.pop();
if(queue.priorityQ.length == 0)
{
window.alert("No path possible");
}
}
// show shortest path
path.addNode(endCell, queue.min()[2]);
let shortestPath = path.findPath(path.tree[path.tree.length - 1]);
this.currentBoard.highlight(shortestPath);
this.currentBoard.add("replay", document.getElementById("replay"), [path, shortestPath]);
}
}
class Board
{
constructor(board, rowWidth = 20, numRows = 10)
{
this.board = board;
this.rowWidth = rowWidth;
this.numRows = numRows;
this.numCells = rowWidth * numRows;
this.leftMouseDown = false;
this.cells = [];
this.mode = "wall";
}
// adds board and proper event handling to add walls and start/end blocks
load()
{
// sets event handling to detect when left mouse button is down
this.board.addEventListener('mousedown', function()
{
this.leftMouseDown = true;
});
this.board.addEventListener('mouseup', function()
{
this.leftMouseDown = false;
});
this.board.addEventListener('mouseleave', function()
{
this.leftMouseDown = false;
});
// adds each cell to boards
for (let i = 0; i<this.numCells; i++)
{
this.cells[i] = document.createElement('div');
this.cells[i].setAttribute('class', 'cell');
this.cells[i].setAttribute('id', i);
/*
this.cells[i].addEventListener('mousedown', function(){
if(this.mode=='wall') {
this.classList.toggle('wall');
} else if(this.mode=='start'){
if(!this.classList.contains('end')) {
if(document.querySelector('.start') != null)
{ document.querySelector('.start').classList.remove('start'); }
this.classList.add('start');
this.mode = 'end';
}
} else {
if(!this.classList.contains('start'))
{
if(document.querySelector('.end') != null)
{ document.querySelector('.end').classList.remove('end');}
this.classList.add('end');
this.mode = 'start';
}
}
});
*/
this.board.appendChild(this.cells[i]);
// adds event handlers to each cell
this.cells[i].addEventListener("click", ()=> {
if(this.mode == "wall")
{
this.cells[i].classList.toggle("wall");
}
else
{
this.cells[i].classList.remove("wall"); // make sure start/end point is not a wall
let s = document.querySelector(".start");
let e = document.querySelector(".end")
if(s == null)
{
this.cells[i].classList.add("start");
}
else if(e == null && !this.cells[i].classList.contains("start"))
{
this.cells[i].classList.add("end")
}
else if(s != null && e != null)
{
s.classList.remove("start");
e.classList.remove("end");
this.cells[i].classList.add("start");
}
}
});
this.cells[i].addEventListener("mouseenter", ()=> {
console.log(this.mode + " " + this.leftMouseDown);
if(this.mode == "wall" && this.leftMouseDown)
{
this.cells[i].classList.add("wall");
}
});
}
}
// calculates distance from one cell to another cell (exclusive, inclusive)
distanceFromCell(cell, endCell)
{
return (Math.abs((endCell % this.rowWidth) - (cell % this.rowWidth))
+ Math.abs(Math.floor(endCell/this.rowWidth) - Math.floor(cell/this.rowWidth)));
}
// returns neighboring cells that are not walls, optionally exclude visited cells.
neighborCells(cell, exlcudeVistied=true)
{
let stepValues = [1, -1, this.rowWidth, -1 * this.rowWidth];
let neighbors = [];
for (let x in stepValues)
{
// skips iteration if 'neighboring cell' is across the board
if(stepValues[x] == -1 && cell != 0 && cell % 20 == 0)
{
continue;
}
if(stepValues[x] == 1 && (cell + 1) % 20 == 0)
{
continue;
}
// if defined i.e. a valid cell id and if the cell is not a wall
if(typeof this.cells[cell+stepValues[x]] !== "undefined" && !this.cells[cell+stepValues[x]].classList.contains('wall')
// if excludeVisited is false or cell is not visited
&& !(exlcudeVistied && this.cells[cell+stepValues[x]].classList.contains('visited')))
{
neighbors.push(cell+stepValues[x]);
}
}
return neighbors;
}
visit(cell)
{
document.getElementById(cell.toString()).classList.add("visited");
}
highlight(cells)
{
if(typeof cells === "number")
{
document.getElementById(parseInt(cells)).classList.add("path");
} else {
this.cells.forEach((cell)=>{
cell.classList.remove("path");
});
cells.forEach((cell)=>
{
document.getElementById(parseInt(cell)).classList.add("path");
});
}
}
*replay(pathObject)
{
let path = pathObject.tree;
this.clear();
while(path.length != 0)
{
let nextCell = path.shift().value;
yield document.getElementById(nextCell.toString()).classList.add("visited");
}
}
playback(i) {
let pathObject = i[0];
let shortestPath = i[1];
let replay = this.replay(pathObject);
let s = setInterval(()=>{
if(replay.next().done)
{
this.highlight(shortestPath);
window.alert("path finished");
clearInterval(s);
}
}, 100);
}
clear(entireBoard = false)
{
if(entireBoard)
{
this.cells.forEach((cell)=> {
cell.classList.remove("visited", "path", "start", "end", "wall");
});
}
else
{
this.cells.forEach((cell)=> {
cell.classList.remove("visited", "path");
});
}
}
add(type, element, other=false)
{
switch(type)
{
case "startEnd":
element.addEventListener("click", ()=>
{
this.mode = "startEnd";
});
break;
case "wall":
element.addEventListener("click", ()=>
{
this.mode = "wall";
});
break;
case "clear":
element.addEventListener("click", ()=>
{
this.clear(true);
});
break;
case "replay":
element.classList.remove("inactive");
element.addEventListener("click", ()=>
{
this.playback(other);
});
break;
default:
console.error("Unknown type '" + type + "' at Board.add()");
}
}
}