-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
888 lines (816 loc) · 24.2 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
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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
var allrows = 25;
var allcolumns = 40;
var inProgress = false;
//var initialMessage = "Click or drag cells to build walls! Press start when you finish and have selected an algorithm!";
var cellsToAnimate = [];
var createWalls = false;
var algorithm = null;
var justFinished = false;
var animationState = null;
var startCell = [11, 15];
var endCell = [11, 25];
var movingStart = false;
var movingEnd = false;
function generateGrid( rows, cols ) {
var grid = "<table>";
for ( row = 1; row <= rows; row++ ) {
grid += "<tr>";
for ( col = 1; col <= cols; col++ ) {
grid += "<td></td>";
}
grid += "</tr>";
}
grid += "</table>"
return grid;
}
var myGrid = generateGrid( allrows, allcolumns);
$( "#tableContainer" ).append( myGrid );
/* --------------------------- */
/* --- OBJECT DECLARATIONS --- */
/* --------------------------- */
function Queue() {
this.stack = new Array();
this.dequeue = function(){
return this.stack.pop();
}
this.enqueue = function(item){
this.stack.unshift(item);
return;
}
this.empty = function(){
return ( this.stack.length == 0 );
}
this.clear = function(){
this.stack = new Array();
return;
}
}
function minHeap() {
this.heap = [];
this.isEmpty = function(){
return (this.heap.length == 0);
}
this.clear = function(){
this.heap = [];
return;
}
this.getMin = function(){
if (this.isEmpty()){
return null;
}
var min = this.heap[0];
this.heap[0] = this.heap[this.heap.length - 1];
this.heap[this.heap.length - 1] = min;
this.heap.pop();
if (!this.isEmpty()){
this.siftDown(0);
}
return min;
}
this.push = function(item){
this.heap.push(item);
this.siftUp(this.heap.length - 1);
return;
}
this.parent = function(index){
if (index == 0){
return null;
}
return Math.floor((index - 1) / 2);
}
this.children = function(index){
return [(index * 2) + 1, (index * 2) + 2];
}
this.siftDown = function(index){
var children = this.children(index);
var leftChildValid = (children[0] <= (this.heap.length - 1));
var rightChildValid = (children[1] <= (this.heap.length - 1));
var newIndex = index;
if (leftChildValid && this.heap[newIndex][0] > this.heap[children[0]][0]){
newIndex = children[0];
}
if (rightChildValid && this.heap[newIndex][0] > this.heap[children[1]][0]){
newIndex = children[1];
}
// No sifting down needed
if (newIndex === index){ return; }
var val = this.heap[index];
this.heap[index] = this.heap[newIndex];
this.heap[newIndex] = val;
this.siftDown(newIndex);
return;
}
this.siftUp = function(index){
var parent = this.parent(index);
if (parent !== null && this.heap[index][0] < this.heap[parent][0]){
var val = this.heap[index];
this.heap[index] = this.heap[parent];
this.heap[parent] = val;
this.siftUp(parent);
}
return;
}
}
/* ------------------------- */
/* ---- MOUSE FUNCTIONS ---- */
/* ------------------------- */
$( "td" ).mousedown(function(){
var index = $( "td" ).index( this );
var startCellIndex = (startCell[0] * (allcolumns)) + startCell[1];
var endCellIndex = (endCell[0] * (allcolumns)) + endCell[1];
if ( !inProgress ){
// Clear board if just finished
if ( justFinished && !inProgress ){
clearBoard( keepWalls = true );
justFinished = false;
}
if (index == startCellIndex){
movingStart = true;
//console.log("Now moving start!");
} else if (index == endCellIndex){
movingEnd = true;
//console.log("Now moving end!");
} else {
createWalls = true;
}
}
});
$( "td" ).mouseup(function(){
createWalls = false;
movingStart = false;
movingEnd = false;
});
$( "td" ).mouseenter(function() {
if (!createWalls && !movingStart && !movingEnd){ return; }
var index = $( "td" ).index( this );
var startCellIndex = (startCell[0] * (allcolumns)) + startCell[1];
var endCellIndex = (endCell[0] * (allcolumns)) + endCell[1];
if (!inProgress){
if (justFinished){
clearBoard( keepWalls = true );
justFinished = false;
}
//console.log("Cell index = " + index);
if (movingStart && index != endCellIndex) {
moveStartOrEnd(startCellIndex, index, "start");
} else if (movingEnd && index != startCellIndex) {
moveStartOrEnd(endCellIndex, index, "end");
} else if (index != startCellIndex && index != endCellIndex) {
$(this).toggleClass("wall");
}
}
});
$( "td" ).click(function() {
var index = $( "td" ).index( this );
var startCellIndex = (startCell[0] * (allcolumns)) + startCell[1];
var endCellIndex = (endCell[0] * (allcolumns)) + endCell[1];
if ((inProgress == false) && !(index == startCellIndex) && !(index == endCellIndex)){
if ( justFinished ){
clearBoard( keepWalls = true );
justFinished = false;
}
$(this).toggleClass("wall");
}
});
$( "body" ).mouseup(function(){
createWalls = false;
movingStart = false;
movingEnd = false;
});
/* ----------------- */
/* ---- BUTTONS ---- */
/* ----------------- */
$( "#startBtn" ).click(function(){
if ( algorithm == null ){ return;}
if ( inProgress ){ update("wait"); return; }
traverseGraph(algorithm);
});
$( "#clearBtn" ).click(function(){
if ( inProgress ){ update("wait"); return; }
clearBoard(keepWalls = false);
});
/* --------------------- */
/* --- NAV BAR MENUS --- */
/* --------------------- */
$( "#algorithms .dropdown-item").click(function(){
if ( inProgress ){ update("wait"); return; }
algorithm = $(this).text();
updateStartBtnText();
console.log("Algorithm has been changd to: " + algorithm);
});
$( "#mazes .dropdown-item").click(function(){
if ( inProgress ){ update("wait"); return; }
maze = $(this).text();
if (maze == "Random"){
randomMaze();
} else if (maze == "Recursive Division"){
recursiveDivMaze(null);
}
console.log("Maze has been changd to: " + maze);
});
/* ----------------- */
/* --- FUNCTIONS --- */
/* ----------------- */
function moveStartOrEnd(prevIndex, newIndex, startOrEnd){
var newCellY = newIndex % allcolumns;
var newCellX = Math.floor((newIndex - newCellY) / allcolumns);
if (startOrEnd == "start"){
startCell = [newCellX, newCellY];
console.log("Moving start to [" + newCellX + ", " + newCellY + "]")
} else {
endCell = [newCellX, newCellY];
console.log("Moving end to [" + newCellX + ", " + newCellY + "]")
}
clearBoard(keepWalls = true);
return;
}
function moveEnd(prevIndex, newIndex){
$($("td").find(prevIndex)).removeClass();
var newEnd = $("td").find(newIndex);
$(newEnd).removeClass();
$(newEnd).addClass("end");
var newEndX = Math.floor(newIndex / allrows);
var newEndY = Math.floor(newIndex / allcolumns);
startCell = [newStartX, newStartY];
return;
}
function updateStartBtnText(){
if (algorithm == "Depth-First Search (DFS)"){
$("#startBtn").html("Start DFS");
} else if (algorithm == "Breadth-First Search (BFS)"){
$("#startBtn").html("Start BFS");
} else if (algorithm == "Dijkstra"){
$("#startBtn").html("Start Dijkstra");
} else if (algorithm == "A*"){
$("#startBtn").html("Start A*");
} else if (algorithm == "Greedy Best-First Search"){
$("#startBtn").html("Start Greedy BFS");
} else if (algorithm == "Jump Point Search"){
$("#startBtn").html("Start JPS");
}
return;
}
function update(message){
$("#resultsIcon").removeClass();
$("#resultsIcon").addClass("fas fa-exclamation");
$('#results').css("background-color", "#ffc107");
$("#length").text("");
if (message == "wait"){
$("#duration").text("Please wait for the algorithm to finish.");
}
}
function updateResults(duration, pathFound, length){
var firstAnimation = "swashOut";
var secondAnimation = "swashIn";
$("#results").removeClass();
$("#results").addClass("magictime " + firstAnimation);
setTimeout(function(){
$("#resultsIcon").removeClass();
if (pathFound){
$('#results').css("background-color", "#77dd77");
$("#resultsIcon").addClass("fas fa-check");
} else {
$('#results').css("background-color", "#ff6961");
$("#resultsIcon").addClass("fas fa-times");
}
$("#duration").text("Duration: " + duration + " ms");
$("#length").text("Length: " + length);
$('#results').removeClass(firstAnimation);
$('#results').addClass(secondAnimation);
}, 1100);
}
// Counts length of success
function countLength(){
var cells = $("td");
var l = 0;
for (var i = 0; i < cells.length; i++){
if ($(cells[i]).hasClass("success")){
l++;
}
}
return l;
}
async function traverseGraph(algorithm){
inProgress = true;
clearBoard( keepWalls = true );
var startTime = Date.now();
var pathFound = executeAlgo();
var endTime = Date.now();
await animateCells();
if ( pathFound ){
updateResults((endTime - startTime), true, countLength());
} else {
updateResults((endTime - startTime), false, countLength());
}
inProgress = false;
justFinished = true;
}
function executeAlgo(){
if (algorithm == "Depth-First Search (DFS)"){
var visited = createVisited();
var pathFound = DFS(startCell[0], startCell[1], visited);
} else if (algorithm == "Breadth-First Search (BFS)"){
var pathFound = BFS();
} else if (algorithm == "Dijkstra"){
var pathFound = dijkstra();
} else if (algorithm == "A*"){
var pathFound = AStar();
} else if (algorithm == "Greedy Best-First Search"){
var pathFound = greedyBestFirstSearch();
} else if (algorithm == "Jump Point Search"){
var pathFound = jumpPointSearch();
}
return pathFound;
}
function makeWall(cell){
if (!createWalls){return;}
var index = $( "td" ).index( cell );
var row = Math.floor( ( index ) / allrows) + 1;
var col = ( index % allcolumns ) + 1;
console.log([row, col]);
if ((inProgress == false) && !(row == 1 && col == 1) && !(row == allrows && col == allcolumns)){
$(cell).toggleClass("wall");
}
}
function createVisited(){
var visited = [];
var cells = $("#tableContainer").find("td");
for (var i = 0; i < allrows; i++){
var row = [];
for (var j = 0; j < allcolumns; j++){
if (cellIsAWall(i, j, cells)){
row.push(true);
} else {
row.push(false);
}
}
visited.push(row);
}
return visited;
}
function cellIsAWall(i, j, cells){
var cellNum = (i * (allcolumns)) + j;
return $(cells[cellNum]).hasClass("wall");
}
function BFS(){
var pathFound = false;
var myQueue = new Queue();
var prev = createPrev();
var visited = createVisited();
myQueue.enqueue( startCell );
cellsToAnimate.push(startCell, "searching");
visited[ startCell[0] ][ startCell[1] ] = true;
while ( !myQueue.empty() ){
var cell = myQueue.dequeue();
var r = cell[0];
var c = cell[1];
cellsToAnimate.push( [cell, "visited"] );
if (r == endCell[0] && c == endCell[1]){
pathFound = true;
break;
}
// Put neighboring cells in queue
var neighbors = getNeighbors(r, c);
for (var k = 0; k < neighbors.length; k++){
var m = neighbors[k][0];
var n = neighbors[k][1];
if ( visited[m][n] ) { continue ;}
visited[m][n] = true;
prev[m][n] = [r, c];
cellsToAnimate.push( [neighbors[k], "searching"] );
myQueue.enqueue(neighbors[k]);
}
}
// Make any nodes still in the queue "visited"
while ( !myQueue.empty() ){
var cell = myQueue.dequeue();
var r = cell[0];
var c = cell[1];
cellsToAnimate.push( [cell, "visited"] );
}
// If a path was found, illuminate it
if (pathFound) {
var r = endCell[0];
var c = endCell[1];
cellsToAnimate.push( [[r, c], "success"] );
while (prev[r][c] != null){
var prevCell = prev[r][c];
r = prevCell[0];
c = prevCell[1];
cellsToAnimate.push( [[r, c], "success"] );
}
}
return pathFound;
}
function dijkstra() {
var pathFound = false;
var myHeap = new minHeap();
var prev = createPrev();
var distances = createDistances();
var visited = createVisited();
distances[ startCell[0] ][ startCell[1] ] = 0;
myHeap.push([0, [startCell[0], startCell[1]]]);
cellsToAnimate.push([[startCell[0], startCell[1]], "searching"]);
while (!myHeap.isEmpty()){
var cell = myHeap.getMin();
//console.log("Min was just popped from the heap! Heap is now: " + JSON.stringify(myHeap.heap));
var i = cell[1][0];
var j = cell[1][1];
if (visited[i][j]){ continue; }
visited[i][j] = true;
cellsToAnimate.push([[i, j], "visited"]);
if (i == endCell[0] && j == endCell[1]){
pathFound = true;
break;
}
var neighbors = getNeighbors(i, j);
for (var k = 0; k < neighbors.length; k++){
var m = neighbors[k][0];
var n = neighbors[k][1];
if (visited[m][n]){ continue; }
var newDistance = distances[i][j] + 1;
if (newDistance < distances[m][n]){
distances[m][n] = newDistance;
prev[m][n] = [i, j];
myHeap.push([newDistance, [m, n]]);
//console.log("New cell was added to the heap! It has distance = " + newDistance + ". Heap = " + JSON.stringify(myHeap.heap));
cellsToAnimate.push( [[m, n], "searching"] );
}
}
//console.log("Cell [" + i + ", " + j + "] was just evaluated! myHeap is now: " + JSON.stringify(myHeap.heap));
}
//console.log(JSON.stringify(myHeap.heap));
// Make any nodes still in the heap "visited"
while ( !myHeap.isEmpty() ){
var cell = myHeap.getMin();
var i = cell[1][0];
var j = cell[1][1];
if (visited[i][j]){ continue; }
visited[i][j] = true;
cellsToAnimate.push( [[i, j], "visited"] );
}
// If a path was found, illuminate it
if (pathFound) {
var i = endCell[0];
var j = endCell[1];
cellsToAnimate.push( [endCell, "success"] );
while (prev[i][j] != null){
var prevCell = prev[i][j];
i = prevCell[0];
j = prevCell[1];
cellsToAnimate.push( [[i, j], "success"] );
}
}
return pathFound;
}
function AStar() {
var pathFound = false;
var myHeap = new minHeap();
var prev = createPrev();
var distances = createDistances();
var costs = createDistances();
var visited = createVisited();
distances[ startCell[0] ][ startCell[1] ] = 0;
costs[ startCell[0] ][ startCell[1] ] = 0;
myHeap.push([0, [startCell[0], startCell[1]]]);
cellsToAnimate.push([[startCell[0], startCell[1]], "searching"]);
while (!myHeap.isEmpty()){
var cell = myHeap.getMin();
var i = cell[1][0];
var j = cell[1][1];
if (visited[i][j]){ continue; }
visited[i][j] = true;
cellsToAnimate.push([[i, j], "visited"]);
if (i == endCell[0] && j == endCell[1]){
pathFound = true;
break;
}
var neighbors = getNeighbors(i, j);
for (var k = 0; k < neighbors.length; k++){
var m = neighbors[k][0];
var n = neighbors[k][1];
if (visited[m][n]){ continue; }
var newDistance = distances[i][j] + 1;
if (newDistance < distances[m][n]){
distances[m][n] = newDistance;
prev[m][n] = [i, j];
cellsToAnimate.push( [[m, n], "searching"] );
}
var newCost = distances[i][j] + Math.abs(endCell[0] - m) + Math.abs(endCell[1] - n);
if (newCost < costs[m][n]){
costs[m][n] = newCost;
myHeap.push([newCost, [m, n]]);
}
}
}
// Make any nodes still in the heap "visited"
while ( !myHeap.isEmpty() ){
var cell = myHeap.getMin();
var i = cell[1][0];
var j = cell[1][1];
if (visited[i][j]){ continue; }
visited[i][j] = true;
cellsToAnimate.push( [[i, j], "visited"] );
}
// If a path was found, illuminate it
if (pathFound) {
var i = endCell[0];
var j = endCell[1];
cellsToAnimate.push( [endCell, "success"] );
while (prev[i][j] != null){
var prevCell = prev[i][j];
i = prevCell[0];
j = prevCell[1];
cellsToAnimate.push( [[i, j], "success"] );
}
}
return pathFound;
}
function greedyBestFirstSearch() {
var pathFound = false;
var myHeap = new minHeap();
var prev = createPrev();
var costs = createDistances();
var visited = createVisited();
costs[ startCell[0] ][ startCell[1] ] = 0;
myHeap.push([0, [startCell[0], startCell[1]]]);
cellsToAnimate.push([[startCell[0], startCell[1]], "searching"]);
while (!myHeap.isEmpty()){
var cell = myHeap.getMin();
var i = cell[1][0];
var j = cell[1][1];
if (visited[i][j]){ continue; }
visited[i][j] = true;
cellsToAnimate.push([[i, j], "visited"]);
if (i == endCell[0] && j == endCell[1]){
pathFound = true;
break;
}
var neighbors = getNeighbors(i, j);
for (var k = 0; k < neighbors.length; k++){
var m = neighbors[k][0];
var n = neighbors[k][1];
if (visited[m][n]){ continue; }
var newCost = Math.abs(endCell[0] - m) + Math.abs(endCell[1] - n);
if (newCost < costs[m][n]){
prev[m][n] = [i, j];
costs[m][n] = newCost;
myHeap.push([newCost, [m, n]]);
cellsToAnimate.push([[m, n], "searching"]);
}
}
}
while ( !myHeap.isEmpty() ){
var cell = myHeap.getMin();
var i = cell[1][0];
var j = cell[1][1];
if (visited[i][j]){ continue; }
visited[i][j] = true;
cellsToAnimate.push( [[i, j], "visited"] );
}
// If a path was found, illuminate it
if (pathFound) {
var i = endCell[0];
var j = endCell[1];
cellsToAnimate.push( [endCell, "success"] );
while (prev[i][j] != null){
var prevCell = prev[i][j];
i = prevCell[0];
j = prevCell[1];
cellsToAnimate.push( [[i, j], "success"] );
}
}
return pathFound;
}
async function randomMaze(){
inProgress = true;
clearBoard(keepWalls = false);
var visited = createVisited();
var walls = makeWalls();
var cells = [ startCell, endCell ];
walls [ startCell[0] ][ startCell[1] ] = false;
walls [ endCell[0] ][ endCell[1] ] = false;
visited[ startCell[0] ][ startCell[1] ] = true;
visited[ endCell[0] ][ endCell[1] ] = true;
while ( cells.length > 0 ){
var random = Math.floor(Math.random() * cells.length);
var randomCell = cells[random];
cells[random] = cells[cells.length - 1];
cells.pop();
var neighbors = getNeighbors(randomCell[0], randomCell[1]);
if (neighborsThatAreWalls(neighbors, walls) < 2){ continue; }
walls[ randomCell[0] ][ randomCell[1] ] = false;
for (var k = 0; k < neighbors.length; k++){
var i = neighbors[k][0];
var j = neighbors[k][1];
if (visited[i][j]){ continue; }
visited[i][j] = true;
cells.push([i, j]);
}
}
var cells = $("#tableContainer").find("td");
for (var i = 0; i < allrows; i++){
for (var j = 0; j < allcolumns; j++){
if (i == 0 || i == (allrows - 1) || j == 0 || j == (allcolumns - 1) || walls[i][j]){
cellsToAnimate.push([ [i, j], "wall"]);
}
}
}
await animateCells();
inProgress = false;
return;
}
function inBounds(cell){
return (cell[0] >= 0 && cell[1] >= 0 && cell[0] < allrows && cell[1] < allcolumns);
}
async function recursiveDivMaze(bias){
inProgress = true;
clearBoard(keepWalls = false);
//Animate edge walls
for (var i = 0; i < allrows; i++){
for (var j = 0; j < allcolumns; j++){
if (i == 0 || j == 0 || i == (allrows - 1) || j == (allcolumns - 1)){
cellsToAnimate.push([ [i, j], "wall"]);
}
}
}
var walls = createVisited();
var passages = createVisited();
recursiveDivMazeHelper(1, (allrows - 2), 1, (allcolumns - 2), 2, (allrows - 3), 2, (allcolumns - 3), walls, passages, bias);
await animateCells();
inProgress = false;
return;
}
function recursiveDivMazeHelper(iStart, iEnd, jStart, jEnd, horzStart, horzEnd, vertStart, vertEnd, walls, passages, bias){
var height = iEnd - iStart + 1;
var width = jEnd - jStart + 1;
var canMakeVertWall = (vertEnd - vertStart) >= 0;
var canMakeHorzWall = (horzEnd - horzStart) >= 0;
if (height < 3 || width < 3 || !canMakeVertWall | !canMakeHorzWall) {
return;
}
// Choose line orientation
var x = Math.floor(Math.random() * 10);
if (bias == "VERTICAL"){
var lineOrientation = x < 8 ? "VERTICAL" : "HORIZONTAL"; // weighting: 90/10 (V/H)
} else if (bias == "HORIZONTAL"){
var lineOrientation = x < 1 ? "VERTICAL" : "HORIZONTAL"; // weighting: 10/90 (V/H)
} else {
var lineOrientation = x < 5 ? "VERTICAL" : "HORIZONTAL"; // weighting: 50/50 (V/H)
}
// Draw line and create random passage
if (lineOrientation == "VERTICAL"){
var vertWidth = vertEnd - vertStart + 1;
var randCol = Math.floor(Math.random() * vertWidth) + vertStart;
if (passages[iStart][randCol]){
var randRow = iStart;
} else if (passages[iEnd][randCol]){
var randRow = iEnd;
} else {
var randRow = (Math.floor(Math.random() * 2) == 0) ? iStart: iEnd; // random end assignment
//var randRow = Math.floor(Math.random() * height) + iStart; // random parition
}
for (var i = iStart; i <= iEnd; i++){
if ( passages[i][randCol] ){ continue; }
if (i == randRow){
// Make passages
for (var j = randCol - 1; j <= randCol + 1; j++){
passages[i][j] = true;
}
} else {
walls[i][randCol] = true;
cellsToAnimate.push([ [i, randCol], "wall"]);
}
}
recursiveDivMazeHelper(iStart, iEnd, jStart, (randCol - 1), horzStart, horzEnd, vertStart, (randCol - 2), walls, passages); //left
recursiveDivMazeHelper(iStart, iEnd, (randCol + 1), jEnd, horzStart, horzEnd, (randCol + 2), vertEnd, walls, passages); //right
} else {
var horzHeight = horzEnd - horzStart + 1;
var randRow = Math.floor(Math.random() * horzHeight) + horzStart;
if (passages[randRow][jStart]){
var randCol = jStart;
} else if (passages[randRow][jEnd]){
var randCol = jEnd;
} else {
var randCol = (Math.floor(Math.random() * 2) == 0) ? jStart: jEnd; // random end assignment
//var randCol = Math.floor(Math.random() * width) + jStart; // random parition
}
for (var j = jStart; j <= jEnd; j++){
if ( passages[randRow][j] ){ continue; }
if (j == randCol){
// Make passages
for (var i = randRow - 1; i <= randRow + 1; i++){
passages[i][j] = true;
}
} else {
walls[randRow][j] = true;
cellsToAnimate.push([ [randRow, j], "wall"]);
}
}
recursiveDivMazeHelper(iStart, (randRow - 1), jStart, jEnd, horzStart, (randRow - 2), vertStart, vertEnd, walls, passages); //up
recursiveDivMazeHelper((randRow + 1), iEnd, jStart, jEnd, (randRow + 2), horzEnd, vertStart, vertEnd, walls, passages); //down
}
return;
}
function makeWalls(){
var walls = [];
for (var i = 0; i < allrows; i++){
var row = [];
for (var j = 0; j < allcolumns; j++){
row.push(true);
}
walls.push(row);
}
return walls;
}
function neighborsThatAreWalls( neighbors, walls ){
var neighboringWalls = 0;
for (var k = 0; k < neighbors.length; k++){
var i = neighbors[k][0];
var j = neighbors[k][1];
if (walls[i][j]) { neighboringWalls++; }
}
return neighboringWalls;
}
function createDistances(){
var distances = [];
for (var i = 0; i < allrows; i++){
var row = [];
for (var j = 0; j < allcolumns; j++){
row.push(Number.POSITIVE_INFINITY);
}
distances.push(row);
}
return distances;
}
function createPrev(){
var prev = [];
for (var i = 0; i < allrows; i++){
var row = [];
for (var j = 0; j < allcolumns; j++){
row.push(null);
}
prev.push(row);
}
return prev;
}
function getNeighbors(i, j){
var neighbors = [];
if ( i > 0 ){ neighbors.push( [i - 1, j] );}
if ( j > 0 ){ neighbors.push( [i, j - 1] );}
if ( i < (allrows - 1) ){ neighbors.push( [i + 1, j] );}
if ( j < (allcolumns - 1) ){ neighbors.push( [i, j + 1] );}
return neighbors;
}
async function animateCells(){
animationState = null;
var cells = $("#tableContainer").find("td");
var startCellIndex = (startCell[0] * (allcolumns)) + startCell[1];
var endCellIndex = (endCell[0] * (allcolumns)) + endCell[1];
var delay = getDelay();
for (var i = 0; i < cellsToAnimate.length; i++){
var cellCoordinates = cellsToAnimate[i][0];
var x = cellCoordinates[0];
var y = cellCoordinates[1];
var num = (x * (allcolumns)) + y;
if (num == startCellIndex || num == endCellIndex){ continue; }
var cell = cells[num];
var colorClass = cellsToAnimate[i][1];
// Wait until its time to animate
await new Promise(resolve => setTimeout(resolve, delay));
$(cell).removeClass();
$(cell).addClass(colorClass);
}
cellsToAnimate = [];
//console.log("End of animation has been reached!");
return new Promise(resolve => resolve(true));
}
function getDelay(){
var delay;
delay = 5;
console.log("Delay= " + delay);
return delay;
}
function clearBoard( keepWalls ){
var cells = $("#tableContainer").find("td");
var startCellIndex = (startCell[0] * (allcolumns)) + startCell[1];
var endCellIndex = (endCell[0] * (allcolumns)) + endCell[1];
for (var i = 0; i < cells.length; i++){
isWall = $( cells[i] ).hasClass("wall");
$( cells[i] ).removeClass();
if (i == startCellIndex){
$(cells[i]).addClass("start");
} else if (i == endCellIndex){
$(cells[i]).addClass("end");
} else if ( keepWalls && isWall ){
$(cells[i]).addClass("wall");
}
}
}
clearBoard();
$('#myModal').on('shown.bs.modal', function () {
$('#myInput').trigger('focus');
})
$(window).on('load',function(){
$('#exampleModalLong').modal('show');
});