-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
1174 lines (996 loc) · 36.3 KB
/
index.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
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// variables for A* Pathfinding Algorithm
let OPEN = [];
let CLOSED = [];
// animation speed
let speed = 0.1;
// start point, goal point and obstacle button functions
let start_point_count = 1;
let goal_point_count = 1;
let start_click_count = 0;
let goal_click_count = 0;
let obstacle_click_count = 0;
// grids
function Grids(rows, cols) {
let table = document.querySelector('#grids table');
for (let i = 0; i < rows; i++) {
// for each row
let row = document.createElement('tr');
for (let j = 0; j < cols; j++) {
// for each column
let grid = document.createElement('td');
if (j < 10 && i < 10) {
grid.setAttribute('id', `0${i}0${j}`);
}
else if (i < 10) {
grid.setAttribute('id', `0${i}${j}`);
}
else if (j < 10) {
grid.setAttribute('id', `${i}0${j}`);
}
else {
grid.setAttribute('id', `${i}${j}`);
}
row.append(grid);
}
table.append(row);
}
// add style to td
document.querySelectorAll('td').forEach((td) => {
td.setAttribute('class', 'grid');
})
}
const GRID_WIDTH = 20;
const GRID_HEIGHT = 20;
Grids(GRID_WIDTH, GRID_HEIGHT);
// random Grid
function RandomGrid() {
return String(("0" + Math.floor(Math.random() * 20)).slice(-2)) + String(("0" + Math.floor(Math.random() * 20)).slice(-2));
}
// clear board
function ClearBoard() {
document.querySelectorAll('td').forEach((td) => {
td.style.backgroundColor = 'white';
})
// clear OPEN and CLOSED lists
OPEN = [];
CLOSED = [];
// clear start and end point counts
start_point_count = 0;
end_point_count = 0;
}
// random board
function RandomBoard() {
// clear board
ClearBoard();
// set start and goal points count
start_point_count = 1;
end_point_count = 1;
// 250 obstacles
// this may make several nodes of the same grid
// note to remove all copies while removing obstacle
for (let i = 0; i < 250; i++) {
let obstacle = new Node(Math.floor(Math.random() * 20), Math.floor(Math.random() * 20));
obstacle.color('black');
CLOSED.push(obstacle);
}
// start point and goal point
start = RandomGrid();
goal = RandomGrid();
while (goal == start) {
goal = RandomGrid();
}
// convert start and goal to nodes
[start_x, start_y] = convert(start);
[goal_x, goal_y] = convert(goal);
start = new Node(start_x, start_y);
start.color('blue');
goal = new Node(goal_x, goal_y);
goal.color('orange');
// put start into OPEN
OPEN.push(start);
// if goal is in CLOSED, remove it
let closed_len = CLOSED.length;
for (let i = 0; i < closed_len; i++) {
if (CLOSED[i].x == goal.x && CLOSED[i].y == goal.y) {
CLOSED.splice(i, 1);
closed_len--;
i--;
}
}
}
// reset board back to original
function ResetBoard() {
document.querySelectorAll('td').forEach((td) => {
td.style.backgroundColor = 'white';
})
// color obstacles
let closed_len = CLOSED.length;
for (let i = 0; i < closed_len; i++) {
CLOSED[i].color('black');
}
// set start and goal points count
start_point_count = 1;
end_point_count = 1;
// color start and goal
if (!start || !goal) {
Alert2();
return false;
}
start.color('blue');
goal.color('orange');
return true;
}
// sample board
function SampleBoard() {
// clear board
ClearBoard();
// set start and goal points count
start_point_count = 1;
end_point_count = 1;
// sample obstacles
for (let i = 1; i < 15; i++) {
let obstacle = new Node(11, i);
obstacle.color('black');
CLOSED.push(obstacle);
}
for (let i = 1; i < 12; i++) {
let obstacle = new Node(i, 14);
obstacle.color('black');
CLOSED.push(obstacle);
}
// start point and goal point
start = '0307';
goal = '1517';
// convert start and goal to nodes
[start_x, start_y] = convert(start);
[goal_x, goal_y] = convert(goal);
start = new Node(start_x, start_y);
start.color('blue');
goal = new Node(goal_x, goal_y);
goal.color('orange');
// put start into OPEN
OPEN.push(start);
}
RandomBoard();
// A* Pathfinding Algorithm
// f(n) = g(n) + h(n)
// (h) represents vertices far from the goal
// (g) represents the exact cost of the path from the starting point to any vertex n
async function Astar(open, closed) {
console.log('A* Pathfinding Algorithm');
DisableButtons();
// check if there is a starting point and a goal point
if (!goal || !start) {
Alert2();
return EnableButtons();
}
while (goal.x != start.x || goal.y != start.y) {
// if no path available
if (open.length == 0) {
Alert()
return EnableButtons();
}
let current_node = LowestFValue(open);
closed.push(current_node);
// change colors current node that is not start
if (current_node.x != start.x || current_node.y != start.y) {
current_node.color('red');
await wait();
}
// caculate for neighbours
for (let i = -1; i < 2; i++) {
neighbour_loop:
for (let j = -1; j < 2; j++) {
// x and y co-ordinates of neighbour
let neighbour_x = current_node.x + i;
let neighbour_y = current_node.y + j;
// if neighbour not on the grid
if (neighbour_x < 0 || neighbour_x >= GRID_WIDTH || neighbour_y < 0 || neighbour_y >= GRID_HEIGHT) {
continue neighbour_loop;
}
// if neighbour node is the goal
if (neighbour_x == goal.x && neighbour_y == goal.y) {
Path(current_node);
return EnableButtons();
}
// make sure the neighbour is not the current node
if (neighbour_x == current_node.x && neighbour_y == current_node.y) {
continue neighbour_loop;
}
// create neighbour node
let neighbour_node = new Node(neighbour_x, neighbour_y);
// add neighbour parent
neighbour_node.parent = current_node;
// diagonal steps
let diagonal = false;
let temp = [i, j];
if ((temp[0] == -1 && temp[1] == -1) || (temp[0] == -1 && temp[1] == 1) || (temp[0] == 1 && temp[1] == -1) || (temp[0] == 1 && temp[1] == 1)) {
diagonal = true;
}
neighbour_node.h = heuristic(neighbour_node, goal, diagonal);
neighbour_node.g = gCost(neighbour_node, neighbour_node.parent, diagonal);
neighbour_node.f = neighbour_node.g + neighbour_node.h;
// make sure neighbour not in CLOSED list
let closed_len = closed.length;
for (let k = 0; k < closed_len; k++) {
if (neighbour_node.x == closed[k].x && neighbour_node.y == closed[k].y) {
continue neighbour_loop;
}
}
// if neighbour is in OPEN
// check if new f and g are lower
let open_len = open.length;
for (let w = 0; w < open_len; w++) {
if (neighbour_node.x == open[w].x && neighbour_node.y == open[w].y) {
if (neighbour_node.f <= open[w].f) {
if (neighbour_node.g < open[w].g) {
// remove old lower f value neighbour from OPEN
open[w].color('white');
open.splice(w, 1);
await wait();
break;
}
else {
continue neighbour_loop;
}
}
else {
// if new f is higher or same, ignore this neighbour
continue neighbour_loop;
}
}
}
neighbour_node.color('lightblue');
open.push(neighbour_node);
await wait();
}
}
}
}
// Greedy Best First Search
// consider h(n) only
// how far is the grid from goal
async function Greedy(open, closed) {
console.log('Greedy Best First Search');
DisableButtons();
// check if there is a starting point and a goal point
if (!goal || !start) {
Alert2();
return EnableButtons();
}
while (goal.x != start.x || goal.y != start.y) {
// if no path available
if (open.length == 0) {
Alert();
return EnableButtons();
}
let current_node = LowestHValue(open);
closed.push(current_node);
// change colors current node that is not start
if (current_node.x != start.x || current_node.y != start.y) {
current_node.color('red');
await wait();
}
// caculate for neighbours
for (let i = -1; i < 2; i++) {
neighbour_loop:
for (let j = -1; j < 2; j++) {
// x and y co-ordinates of neighbour
let neighbour_x = current_node.x + i;
let neighbour_y = current_node.y + j;
// if neighbour not on the grid
if (neighbour_x < 0 || neighbour_x >= GRID_WIDTH || neighbour_y < 0 || neighbour_y >= GRID_HEIGHT) {
continue neighbour_loop;
}
// if neighbour node is the goal
if (neighbour_x == goal.x && neighbour_y == goal.y) {
Path(current_node);
return EnableButtons();
}
// make sure the neighbour is not the current node
if (neighbour_x == current_node.x && neighbour_y == current_node.y) {
continue neighbour_loop;
}
// create neighbour node
let neighbour_node = new Node(neighbour_x, neighbour_y);
// add neighbour parent
neighbour_node.parent = current_node;
// diagonal steps
let diagonal = false;
let temp = [i, j];
if ((temp[0] == -1 && temp[1] == -1) || (temp[0] == -1 && temp[1] == 1) || (temp[0] == 1 && temp[1] == -1) || (temp[0] == 1 && temp[1] == 1)) {
diagonal = true;
}
neighbour_node.h = heuristic(neighbour_node, goal, diagonal);
// make sure neighbour not in CLOSED list
let closed_len = closed.length;
for (let k = 0; k < closed_len; k++) {
if (neighbour_node.x == closed[k].x && neighbour_node.y == closed[k].y) {
continue neighbour_loop;
}
}
// make sure neighbour is not in OPEN
let open_len = open.length;
for (let w = 0; w < open_len; w++) {
if (neighbour_node.x == open[w].x && neighbour_node.y == open[w].y) {
continue neighbour_loop;
}
}
neighbour_node.color('lightblue');
open.push(neighbour_node);
await wait();
}
}
}
}
// breadth first search
// search by expanding neighbouring nodes (uninformed search)
async function Breadth(open, closed) {
console.log('Breadth First Search');
DisableButtons();
// check if there is a starting point and a goal point
if (!goal || !start) {
Alert2();
return EnableButtons();
}
while (goal.x != start.x || goal.y != start.y) {
// if no path available
if (open.length == 0) {
Alert();
return EnableButtons();
}
let current_node = open.shift();
closed.push(current_node);
// change colors current node that is not start
if (current_node.x != start.x || current_node.y != start.y) {
current_node.color('red');
await wait();
}
// caculate for neighbours
for (let i = -1; i < 2; i++) {
neighbour_loop:
for (let j = -1; j < 2; j++) {
// x and y co-ordinates of neighbour
let neighbour_x = current_node.x + i;
let neighbour_y = current_node.y + j;
// if neighbour not on the grid
if (neighbour_x < 0 || neighbour_x >= GRID_WIDTH || neighbour_y < 0 || neighbour_y >= GRID_HEIGHT) {
continue neighbour_loop;
}
// if neighbour node is the goal
if (neighbour_x == goal.x && neighbour_y == goal.y) {
Path(current_node);
return EnableButtons();
}
// make sure the neighbour is not the current node
if (neighbour_x == current_node.x && neighbour_y == current_node.y) {
continue neighbour_loop;
}
// create neighbour node
let neighbour_node = new Node(neighbour_x, neighbour_y);
// add neighbour parent
neighbour_node.parent = current_node;
// make sure neighbour not in CLOSED list
let closed_len = closed.length;
for (let k = 0; k < closed_len; k++) {
if (neighbour_node.x == closed[k].x && neighbour_node.y == closed[k].y) {
continue neighbour_loop;
}
}
// make sure neighbour is not in OPEN
let open_len = open.length;
for (let w = 0; w < open_len; w++) {
if (neighbour_node.x == open[w].x && neighbour_node.y == open[w].y) {
continue neighbour_loop;
}
}
neighbour_node.color('lightblue');
open.push(neighbour_node);
await wait();
}
}
}
}
// Dijkstra's algorithm
// find shortest path to each node from source (starting node)
// g(n) cost
async function Dijkstra(open, closed) {
console.log('Dijkstra Algorithm');
DisableButtons();
// check if there is a starting point and a goal point
if (!goal || !start) {
Alert2();
return EnableButtons();
}
while (goal.x != start.x || goal.y != start.y) {
// if no path available
if (open.length == 0) {
Alert();
return EnableButtons();
}
let current_node = LowestGValue(open);
closed.push(current_node);
// check if current node is the goal (having min g cost in open list)
if (current_node.x == goal.x && current_node.y == goal.y) {
Path(current_node);
current_node.color('orange');
return EnableButtons();
}
// change colors current node that is not start
if (current_node.x != start.x || current_node.y != start.y) {
current_node.color('red');
await wait();
}
// caculate for neighbours
for (let i = -1; i < 2; i++) {
neighbour_loop:
for (let j = -1; j < 2; j++) {
// x and y co-ordinates of neighbour
let neighbour_x = current_node.x + i;
let neighbour_y = current_node.y + j;
// if neighbour not on the grid
if (neighbour_x < 0 || neighbour_x >= GRID_WIDTH || neighbour_y < 0 || neighbour_y >= GRID_HEIGHT) {
continue neighbour_loop;
}
// make sure the neighbour is not the current node
if (neighbour_x == current_node.x && neighbour_y == current_node.y) {
continue neighbour_loop;
}
// create neighbour node
let neighbour_node = new Node(neighbour_x, neighbour_y);
// add neighbour parent
neighbour_node.parent = current_node;
// diagonal steps
let diagonal = false;
let temp = [i, j];
if ((temp[0] == -1 && temp[1] == -1) || (temp[0] == -1 && temp[1] == 1) || (temp[0] == 1 && temp[1] == -1) || (temp[0] == 1 && temp[1] == 1)) {
diagonal = true;
}
neighbour_node.g = gCost(neighbour_node, neighbour_node.parent, diagonal);
// make sure neighbour not in CLOSED list
let closed_len = closed.length;
for (let k = 0; k < closed_len; k++) {
if (neighbour_node.x == closed[k].x && neighbour_node.y == closed[k].y) {
continue neighbour_loop;
}
}
// if neighbour in OPEN, check if new g is lower (more efficient path)
let open_len = open.length;
for (let w = 0; w < open_len; w++) {
if (neighbour_node.x == open[w].x && neighbour_node.y == open[w].y) {
if (neighbour_node.g < open[w].g) {
open.splice(w, 1);
neighbour_node.color('white');
await wait();
break;
}
else {
continue neighbour_loop;
}
}
}
// change color if not goal
if (neighbour_node.x != goal.x || neighbour_node.y != goal.y) {
neighbour_node.color('lightblue');
}
open.push(neighbour_node);
await wait();
}
}
}
}
// for each node
function Node(x, y) {
this.x = x;
this.y = y;
this.color = (color) => {
let x_num = x;
let y_num = y;
let str_id = String(("0" + x_num).slice(-2)) + String(("0" + y_num).slice(-2));
document.getElementById(`${str_id}`).style.backgroundColor = color;
}
this.f = 0;
this.g = 0;
this.h = 0;
// pointer to parent
this.parent = [-1, -1];
}
// calculate Heuristic (h) (how far from goal)
// Diagonal distance allowed
function heuristic(node, goal, diagonal) {
dx = Math.abs(node.x - goal.x);
dy = Math.abs(node.y - goal.y);
if (diagonal) {
return (Math.sqrt(dx ** 2 + dy ** 2));
// D * (dx + dy) + (D2 - 2 * D) * Math.min(dx, dy)
}
else {
return (Math.sqrt(dx ** 2 + dy ** 2));
}
}
// calculate (g) (exact cost of the path from the starting point to any vertex n)
function gCost(node, parent, diagonal) {
let gcost = parent.g;
dx = Math.abs(node.x - parent.x);
dy = Math.abs(node.y - parent.y);
if (diagonal) {
gcost += (Math.sqrt(dx ** 2 + dy ** 2));
}
else {
gcost += (Math.sqrt(dx ** 2 + dy ** 2));
}
return gcost;
}
// finding lowest F value in OPEN list, tie break by lower h value and then lowest g value
// returning the node and removing it from OPEN list
function LowestFValue(open_list) {
// smallest = [index, node]
// smallest_value = [f, h, g]
let smallest = [0, open_list[0]];
let smallest_value = [open_list[0].f, open_list[0].h, open_list[0].g]
list_len = open_list.length;
for(let i = 0; i < list_len; i++) {
// compare f value
if (open_list[i].f < smallest_value[0]) {
smallest[0] = i;
smallest[1] = open_list[i];
smallest_value = [open_list[i].f, open_list[i].h, open_list[i].g];
}
// compare g when f are the same
else if (open_list[i].f == smallest_value[0]) {
if (open_list[i].g < smallest_value[2]) {
smallest[0] = i;
smallest[1] = open_list[i];
smallest_value = [open_list[i].f, open_list[i].h, open_list[i].g];
}
// compare h when f and g are the same
else if (open_list[i].g == smallest_value[2]) {
if (open_list[i].h < smallest_value[1]) {
smallest[0] = i;
smallest[1] = open_list[i];
smallest_value = [open_list[i].f, open_list[i].h, open_list[i].g];
}
}
}
}
open_list.splice(smallest[0], 1);
return smallest[1];
}
// find lowest h value in OPEN list
// return node and remove from OPEN list
function LowestHValue(open_list) {
// smallest = [index, node]
// lowest_h = h value
let smallest = [0, open_list[0]];
let lowest_h = open_list[0].h;
let open_len = open_list.length;
for (let i = 0; i < open_len; i++) {
if (open_list[i].h < lowest_h) {
lowest_h = open_list[i].h;
smallest[0] = i;
smallest[1] = open_list[i];
}
}
open_list.splice(smallest[0], 1);
return smallest[1];
}
// find lowest g value in OPEN list
// return node and remove from OPEN list
function LowestGValue(open_list) {
// smallest = [index, node]
// lowest_g = g value
let smallest = [0, open_list[0]];
let lowest_g = open_list[0].g;
let open_len = open_list.length;
for (let i = 0; i < open_len; i++) {
if (open_list[i].g < lowest_g) {
lowest_g = open_list[i].g;
smallest[0] = i;
smallest[1] = open_list[i];
}
}
open_list.splice(smallest[0], 1);
return smallest[1];
}
// convert id from td to [x, y]
function convert(id) {
int_id = parseInt(id);
let y = int_id % 100;
let x = Math.floor(int_id / 100) % 100;
return [x, y];
}
// show path animation
async function Path(current_node) {
// base case
if (current_node.x == start.x && current_node.y == start.y) {
return;
}
current_node.color('green');
await wait();
Path(current_node.parent);
}
// slow animation
function wait() {
return new Promise(resolve => {
setTimeout(() => {
resolve('resolved');
}, speed * 20);
});
}
function Buttons() {
function SpeedButton() {
let button = document.querySelector('#speed');
button.addEventListener('change', () => {
speed = button.value;
})
}
function ClearBoardButton() {
document.querySelector('#clear-board').onclick = () => {
ClearBoard();
}
}
function RandomBoardButton() {
document.querySelector('#random-board').onclick = () => {
RandomBoard();
}
}
function ResetBoardButton() {
document.querySelector('#reset-board').onclick = () => {
ResetBoard();
}
}
function SampleBoardButton() {
document.querySelector('#sample-board').onclick = () => {
SampleBoard();
}
}
function StartPointButton() {
let start_point = document.querySelector('#start-point');
start_point.addEventListener('mouseover', () => {
start_point.style.backgroundColor = 'blue';
});
start_point.addEventListener('mouseout', () => {
if (start_click_count == 0) {
start_point.style.backgroundColor = 'white';
}
});
start_point.addEventListener('click', () => {
// first click
if (start_click_count == 0) {
// if end point button is enabled, disable it
if (goal_click_count != 0) {
document.querySelector('#goal-point').click();
}
// if obstacle button is enabled, disable it
if (obstacle_click_count != 0) {
document.querySelector('#modify-obstacle').click();
}
start_click_count++;
start_point.style.backgroundColor = 'blue';
start_point.style.color = 'white';
document.querySelectorAll('td').forEach((td) => {
td.addEventListener('click', start_point_function);
td.addEventListener('mouseover', hoverColor);
});
}
// second click
else if (start_click_count == 1) {
start_click_count--;
start_point.setAttribute('style', '');
document.querySelectorAll('td').forEach((td) => {
td.removeEventListener('click', start_point_function);
td.removeEventListener('mouseover', hoverColor);
});
}
});
function hoverColor() {
grid_color = this.style.backgroundColor;
this.style.backgroundColor = 'blue';
this.addEventListener('mouseout', hoverOff);
function hoverOff() {
this.style.backgroundColor = grid_color;
this.removeEventListener('mouseout', hoverOff);
};
}
function start_point_function() {
if (start_point_count > 0) {
document.querySelectorAll('td').forEach((cell) => {
// if starting point already chosen
if (cell.style.backgroundColor == 'blue') {
cell.style.backgroundColor = 'white';
start_point_count = 0;
}
})
}
start_point_count++;
start = this.id;
[start_x, start_y] = convert(start);
start = new Node(start_x, start_y);
start.color('blue');
// remove old start point
OPEN = [];
// put start into OPEN
OPEN.push(start);
// if the block replaced is in CLOSED, remove it from CLOSED
let closed_len = CLOSED.length;
if (grid_color == 'black') {
for (let i = 0; i < closed_len; i++) {
if (CLOSED[i].x == start_x && CLOSED[i].y == start_y) {
CLOSED.splice(i, 1);
closed_len--;
i--;
}
}
}
// if goal is replaced
else if (grid_color == 'orange') {
goal = null;
}
grid_color = 'blue';
}
}
function GoalPointButton() {
let goal_point = document.querySelector('#goal-point');
goal_point.addEventListener('mouseover', () => {
goal_point.style.backgroundColor = 'orange';
});
goal_point.addEventListener('mouseout', () => {
if (goal_click_count == 0) {
goal_point.style.backgroundColor = 'white';
}
});
goal_point.addEventListener('click', () => {
// first click
if (goal_click_count == 0) {
// if start point button is enabled, disable it
if (start_click_count != 0) {
document.querySelector('#start-point').click();
}
// if obstacle button is enabled, disable it
if (obstacle_click_count != 0) {
document.querySelector('#modify-obstacle').click();
}
goal_click_count++;
goal_point.style.backgroundColor = 'orange';
goal_point.style.color = 'white';
document.querySelectorAll('td').forEach((td) => {
td.addEventListener('click', goal_point_function);
td.addEventListener('mouseover', hoverColor);
});
}
// second click
else if (goal_click_count == 1) {
goal_click_count--;
goal_point.setAttribute('style', '');
document.querySelectorAll('td').forEach((td) => {
td.removeEventListener('click', goal_point_function);
td.removeEventListener('mouseover', hoverColor);
});
}
});
function hoverColor() {
grid_color = this.style.backgroundColor;
this.style.backgroundColor = 'orange';
this.addEventListener('mouseout', hoverOff);
function hoverOff() {
this.style.backgroundColor = grid_color;
this.removeEventListener('mouseout', hoverOff);
};
}
function goal_point_function() {
if (goal_point_count > 0) {
document.querySelectorAll('td').forEach((cell) => {
// if starting point already chosen
if (cell.style.backgroundColor == 'orange') {
cell.style.backgroundColor = 'white';
goal_point_count = 0;
}
})
}
goal_point_count++;
goal = this.id;
[goal_x, goal_y] = convert(goal);
goal = new Node(goal_x, goal_y);
goal.color('orange');
// if the block replaced is in CLOSED, remove it from CLOSED
let closed_len = CLOSED.length;
if (grid_color == 'black') {
for (let i = 0; i < closed_len; i++) {
if (CLOSED[i].x == goal_x && CLOSED[i].y == goal_y) {
CLOSED.splice(i, 1);
closed_len--;
i--;
}
}
}
// if start is replaced
else if (grid_color == 'blue') {
start = null;
}
grid_color = 'orange';
}
}
function NewObstacleButton() {
let obstacle_button = document.querySelector('#modify-obstacle');
obstacle_button.onclick = () => {
// first click
if (obstacle_click_count == 0) {
// if start point button is enabled, disable it
if (start_click_count != 0) {
document.querySelector('#start-point').click();
}
// if end point button is enabled, disable it
if (goal_click_count != 0) {
document.querySelector('#goal-point').click();
}
obstacle_click_count++;
obstacle_button.style.backgroundColor = 'black';
obstacle_button.style.color = 'white';
document.querySelectorAll('td').forEach((td) => {
td.addEventListener('click', obstacle_point_function);
td.addEventListener('mouseover', hoverColor);
});
}
// second click
else if (obstacle_click_count == 1) {
obstacle_click_count--;
obstacle_button.setAttribute('style', '');
document.querySelectorAll('td').forEach((td) => {
td.addEventListener('click', obstacle_point_function);
td.removeEventListener('mouseover', hoverColor);
});
}