-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMazeDrawTest2.html
More file actions
889 lines (650 loc) · 20.6 KB
/
Copy pathMazeDrawTest2.html
File metadata and controls
889 lines (650 loc) · 20.6 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
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Maze</title>
<style>
* { padding: 0; margin: 0; }
canvas { background: #eee; display: block; margin: 0 auto; }
</style>
</head>
<body>
<canvas id ="myCanvas" width="480" height="320"></canvas>
<input type="text" name="x" id="xValue"><br>
<input type="text" name="y" id="yValue"><br>
<script>
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
//disjoint set algorithm
//make a list of sets
//make a union of a random start set and an adjacent set, removing the wall between them
//increment to the next set that was added, push last onto stack
//check adjacent and remove wall
//increment
class treeRoot{
constructor(){
this.id = -1;
this.x = -1;
this.y = -1;
this.lastTileId = -1;
this.distanceFromRoot = 1;
this.leaves = [];
this.distances = [];
}
init(id, x, y, lastTileId){
this.id = id;
this.x = x;
this.y = y;
this.lastTileId = lastTileId;
this.distanceFromRoot = 1;
this.leaves = [];
this.distances = [];
}
addLeaf(leaf){
this.leaves.push(leaf);
}
addDistance(distance){
this.distances.push(distance);
}
}
function isNode(tile){
//tile = {id:, up:, down:, left:, right:}
passages = tile.up + tile.down + tile.left + tile.right;
if (passages != 2){
return true;
}
else{
return false;
}
}
function findNode(nodeIn, maze){
var nodeOut = nodeIn;
var travelStack = [];
travelStack.push(nodeIn.lastTileId);
var travelStackDir = [];
var xCoord = nodeIn.x;
var yCoord = nodeIn.y;
var tile = maze[xCoord][yCoord];
nodeOut.id = tile.id;
let tileMove;
while(!isNode(tile)){
let newX = xCoord;
let newY = yCoord;
nodeOut.lastTileId = tile.id;
for(dir in tile){
if (dir != "id"){
if (tile[dir] == 0){
if(dir == "up"){
newY -= 1;
}
else if(dir == "down"){
newY += 1;
}
else if(dir == "left"){
newX -= 1;
}
else if(dir == "right"){
newX += 1;
}
tileMove = maze[newX][newY];
if (!travelStack.includes(tileMove.id)){
travelStack.push(tile.id);
travelStackDir.push(dir);
tile = tileMove;
xCoord = newX;
yCoord = newY;
nodeOut.id = tile.id;
nodeOut.x = newX;
nodeOut.y = newY;
nodeOut.distanceFromRoot += 1;
//var topLeft = {x:(0 + newX * 20), y:(0 + newY * 20)};
//ctx.fillStyle = "rgba(0,255,0,.2)";
//ctx.fillRect(topLeft.x, topLeft.y, 20,20);
break;
}
else{
newX = xCoord;
newY = yCoord;
}
}
}
}
if (nodeOut.id == nodeOut.lastTileId){
return 0;
}
//console.log("Looping in find node")
}
return nodeOut;
}
function mazeToTree(maze, x, y){
//maze[x][y][{id:, up:, down:, left:, right:}]
var width = maze.length;
var height = maze[0].length;
var xCoord;
var yCoord;
if (x == "" && y == ""){
xCoord = Math.floor(Math.random()*width);
yCoord = Math.floor(Math.random()*height);
}
else{
xCoord = Number(x);
yCoord = Number(y);
}
console.log(xCoord, yCoord);
var tile = maze[xCoord][yCoord];
var topLeft = {x:(0 + xCoord * 20), y:(0 + yCoord * 20)};
ctx.fillStyle = "rgba(0,0,255,.2)";
ctx.fillRect(topLeft.x, topLeft.y, 20,20);
let tileMove;
var seedNode = new treeRoot();
seedNode.init(-1, xCoord, yCoord, -1);
var baseTree = findNode(seedNode, maze)
baseTree.lastTileId = -1;
var nodeQueue = [];
nodeQueue.push(baseTree);
var topLeft = {x:(0 + baseTree.x * 20), y:(0 + baseTree.y * 20)};
ctx.fillStyle = "rgba(0,0,0,.3)";
ctx.fillRect(topLeft.x, topLeft.y, 20,20);
while(nodeQueue.length != 0){
var node = nodeQueue.shift();
tile = maze[node.x][node.y];
var newTile;
for (dir in tile){
let newX = node.x;
let newY = node.y;
if(dir != "id" && tile[dir] == 0){
if(dir == "up"){
newY -= 1;
}
else if(dir == "down"){
newY += 1;
}
else if(dir == "left"){
newX -= 1;
}
else if(dir == "right"){
newX += 1;
}
newTile = maze[newX][newY];
if(newTile.id == node.lastTileId){
continue;
}
let tileObj = new treeRoot();
tileObj.init(newTile.id, newX, newY, node.id);
var adjacentNode = findNode(tileObj, maze);
if (adjacentNode == 0){
continue;
}
nodeQueue.push(adjacentNode);
node.addLeaf(adjacentNode);
node.addDistance(adjacentNode.distanceFromRoot);
var topLeft = {x:(0 + adjacentNode.x * 20), y:(0 + adjacentNode.y * 20)};
ctx.fillStyle = "rgba(0,0,0,.3)";
ctx.fillRect(topLeft.x, topLeft.y, 20,20);
console.log(node.id, adjacentNode.x, adjacentNode.y, adjacentNode.id, dir)
console.log(nodeQueue)
}
}
//console.log("looping in find branch")
}
return baseTree;
}
//maybe a recursive thing later
function travelLeaves(){
}
function findLongestPath(mazeTree){
var pathStack = [];
var traveledRoots = []; //find a way to make this ids instead of trees, much more efficient memory usage... probably
var paths = [];
var combinedPaths = [];
pathStack.push(mazeTree);
//replace with recursion? I think a lot of this program can be replaced with recursion
while(pathStack.length != 0){
let currentTree = pathStack.pop();
//building stack
if (currentTree.leaves.length != 0){
let nonTraveledLeaves = currentTree.leaves.filter(leaf => -1 === traveledRoots.indexOf(leaf));
if (nonTraveledLeaves.length != 0){
pathStack.push(currentTree);
for (let leaf = 0; leaf < nonTraveledLeaves.length; leaf++){
pathStack.push(nonTraveledLeaves[leaf]);
}
}
//building paths
else if (nonTraveledLeaves.length == 0){
for (let i = 0; i < paths.length; i++){
currentTree.leaves.forEach(function (leaf, index){
//add current tree id to "leaf paths"
if (leaf.id == paths[i].path[0].id){// tree vs id
//if (leaf.id == paths[i].path[0]){
//paths[i].path.unshift(currentTree.id);
paths[i].path.unshift(currentTree); //tree vs id
paths[i].distance += currentTree.distances[index];
}
});
}
//attempt to combine two longest paths
let combined = [];
let longPath = [];
for (let i = 0; i < paths.length; i++){
for (let k = 0; k < i; k++){
//if (paths[i].path[0] == paths[k].path[0] && paths[i].path[0] == currentTree.id && paths[i].path[1] != paths[k].path[1]){
//console.log("TestTree:", paths[i].path[0].id ) //tree vs id
if (paths[i].path[0].id == paths[k].path[0].id && paths[i].path[0].id == currentTree.id && paths[i].path[1].id != paths[k].path[1].id){
if (paths[i].distance > paths[k].distance){
combined = JSON.parse(JSON.stringify(paths[k])); //only reliable way I could find to copy an array of objects
longPath = JSON.parse(JSON.stringify(paths[i]));
}
else{
combined = JSON.parse(JSON.stringify(paths[i]));
longPath = JSON.parse(JSON.stringify(paths[k]));
}
combined.path = combined.path.reverse();
//combined.path = combined.path.splice(0, 1);
combined.path.shift;
combined.path = combined.path.concat(longPath.path);
combined.distance += longPath.distance;
//combinedPaths.push(combined);
paths.push(combined);
}
}
}
traveledRoots.push(currentTree);
}
//sort and combine paths
}
else if (currentTree.leaves.length == 0){
//"return" currentTrea as a path
//let pathObj = {path: [currentTree.id], distance: 0};
let pathObj = {path: [currentTree], distance: 0}; //tree vs id
paths.push(pathObj);
traveledRoots.push(currentTree);
}
}
//paths.concat(combinedPaths);
return paths;
}
function permutation(arrayIn){
var tempArray = arrayIn.slice(); //make sure not make a reference to the original
var permArray = [];
for (var i = 0; i < tempArray.length; i++){
var endIndex = tempArray.length - i - 1; //get last index of the array (slowly ignoring more and more as we go)
var randNum = Math.floor(Math.random()*(endIndex + 1)); //need rand()*10 = 0-9, so we need to add one to be able to roll the last index
permArray.push(tempArray[randNum]); //push to permutation
tempArray[randNum] = tempArray[endIndex]; //replace drawn numbers with the last index [0, 1, 2, 3] --> [0, 3, 2, -- 3] if 1 is drawn (where -- indicates the end array index we use afterwards)
}
return permArray;
}
function drawMaze(maze, x, y, nodeSize){
//maze[x][y][{id:, up:, down:, left:, right:}]
ctx.clearRect(0, 0, canvas.width, canvas.height);
var width = maze.length;
var height = maze[0].length;
for (var yCoord = 0; yCoord < height; yCoord++){
for (var xCoord = 0; xCoord < width; xCoord++){
var topLeft = {x:(x + xCoord * nodeSize), y:(y + yCoord * nodeSize)};
var topRight = {x:(x + (xCoord + 1) * nodeSize), y:(y + yCoord * nodeSize)};
var bottomLeft = {x:(x + xCoord * nodeSize), y:(y + (yCoord + 1) * nodeSize)};
var bottomRight = {x:(x + (xCoord + 1) * nodeSize), y:(y + (yCoord + 1) * nodeSize)};
var mazeTile = maze[xCoord][yCoord];
ctx.strokeStyle = "black";
if(mazeTile["up"] == 1){
ctx.globalCompositeOperation = "source-over";
}
else{
ctx.globalCompositeOperation = "destination-out";
}
ctx.beginPath();
ctx.moveTo(topLeft.x, topLeft.y);
ctx.lineTo(topRight.x, topRight.y)
ctx.stroke();
if(mazeTile["down"] == 1){
ctx.globalCompositeOperation = "source-over";
}
else{
ctx.globalCompositeOperation = "destination-out";
}
ctx.beginPath();
ctx.moveTo(bottomLeft.x, bottomLeft.y);
ctx.lineTo(bottomRight.x, bottomRight.y)
ctx.stroke();
if(mazeTile["left"] == 1){
ctx.globalCompositeOperation = "source-over";
}
else{
ctx.globalCompositeOperation = "destination-out";
}
ctx.beginPath();
ctx.moveTo(topLeft.x, topLeft.y);
ctx.lineTo(bottomLeft.x, bottomLeft.y)
ctx.stroke();
if(mazeTile["right"] == 1){
ctx.globalCompositeOperation = "source-over";
}
else{
ctx.globalCompositeOperation = "destination-out";
}
ctx.beginPath();
ctx.moveTo(topRight.x, topRight.y);
ctx.lineTo(bottomRight.x, bottomRight.y)
ctx.stroke();
ctx.globalCompositeOperation = "source-over";
ctx.fillStyle = "#000000"
ctx.font = "10px Arial";
var text = ctx.measureText(mazeTile.id); //text size could stand to be a little more dynamic and account for changing node size
ctx.fillText(mazeTile.id, topLeft.x + (nodeSize-text.width)/2, topLeft.y + (nodeSize+10)/2);
}
}
}
function generateMaze(width, height, xPos, yPos, nodeSize){
var setList = [];
var coords = [];
var mazeSize = width*height;
var adjacentRooms = {up:0, down:0, left:0, right:0};
var maze = [];
var roomCount = 0;
var roomStack = [];
////////////Drawing code
/*
ctx.beginPath();
ctx.moveTo(xPos,yPos);
ctx.lineTo(xPos + width*nodeSize, yPos);
ctx.lineTo(xPos + width*nodeSize, yPos + height*nodeSize);
ctx.lineTo(xPos, yPos + height*nodeSize);
ctx.lineTo(xPos, yPos);
ctx.stroke();
for(var y = 0; y < height; y++){
ctx.moveTo(xPos, yPos + y*nodeSize);
ctx.lineTo(xPos+width*nodeSize, yPos + y*nodeSize);
ctx.stroke();
}
for(var x = 0; x < width; x++){
ctx.moveTo(xPos + x*nodeSize, yPos );
ctx.lineTo(xPos + x*nodeSize, yPos+height*nodeSize);
ctx.stroke();
}
*/
/////////
//this creates a [y][x] coordinate system for the maze
/*
for (var y = 0; y < height; y++){
var row = [];
for (var x = 0; x < width; x++){
var entry = x + y*height;
var cell = {id: entry, up:1, right:1, down:1, left:1};
row.push(cell);
//create a set of sets since we have the loop anyway
var set = [entry];
setList.push(set);
coords[entry] = {x:x, y:y};
}
maze.push(row);
}
*/
for (var x = 0; x < width; x++){
var column = [];
for (var y = 0; y < height; y++){
var entry = x + y*width;
var cell = {id: entry, up:1, right:1, down:1, left:1};
column.push(cell);
//create a set of sets since we have the loop anyway
var set = [entry];
setList.push(set);
coords[entry] = {x:x, y:y};
}
maze.push(column);
}
//console.log("Maze:", maze);
Loop1:
for (var i = 0; i < mazeSize; i++){
//iterate unions
var originalLength = setList.length;
if(setList.length == 1){
break;
}
var permArray = permutation(setList);
//var currentIndex = Math.floor(Math.random()*i); //get random value within permutation
currentSet = permArray[0]; //get first value in the permutation of sets
var currentTileIndex = currentSet.length - 1; //last value of the set
//if we're still looking for a contiguous tile
Loop2:
do{
//var currentTile = currentSet[Math.floor(Math.random()*currentSet.length)]; //pick random tile
var currentTile = currentSet[currentTileIndex]; //choose from the last value to be put in the set
var xCoord = coords[currentTile].x;
var yCoord = coords[currentTile].y;
var above = yCoord - 1;
var below = yCoord + 1;
var left = xCoord - 1;
var right = xCoord + 1;
//convert to tile id if it exists and store in adjacent rooms
if(above >= 0){
adjacentRooms["up"] = above*width + xCoord;
}
else{
adjacentRooms["up"] = NaN;
}
if(below < height){
adjacentRooms["down"] = below*width + xCoord;
}
else{
adjacentRooms["down"] = NaN;
}
if(left >= 0){
adjacentRooms["left"] = currentTile - 1;
}
else{
adjacentRooms["left"] = NaN;
}
if(right < width){
adjacentRooms["right"] = currentTile + 1;
}
else{
adjacentRooms["right"] = NaN;
}
var validDirections = [];
//check if direction is both valid and not in the current set
for(var direction in adjacentRooms){
var currentDirection = adjacentRooms[direction];
if(currentDirection || currentDirection == 0){
var notInSet = true;
if(currentSet.indexOf(currentDirection) > -1){
notInSet = false;
}
if(notInSet){
validDirections.push(direction);
//console.log(currentTile, validDirections)
}
}
}
//if no direction, move back from the edge of the set by one and try again
if (validDirections.length == 0){
currentTileIndex -= 1; //probably should make an edge case for if this reaches 0
continue Loop2;
}
var move = permutation(validDirections);
move = move[0];
//break down the wall and merge the contiguous sets
maze[xCoord][yCoord][move] = 0; //break down the wall
if(move == "up"){
maze[xCoord][above]["down"] = 0;
}
else if(move == "down"){
maze[xCoord][below]["up"] = 0;
}
else if(move == "left"){
maze[left][yCoord]["right"] = 0;
}
else if(move == "right"){
maze[right][yCoord]["left"] = 0;
}
else{
console.log("What did you do? (move code)");
}
console.log("Opened wall:", xCoord, yCoord, move)
var cmpArray = permArray.slice(1);
for(var k = 0; k < (permArray.length-1); k++){ //compare sets but skip the first one, which should be the current set
var aSet = cmpArray[k];
if(aSet.indexOf(adjacentRooms[move]) > -1){//if the newly merged tile is in the set
currentSet = currentSet.concat(aSet); //create union of the two sets
permArray[0] = currentSet;
permArray.splice(k+1, 1); //remove the merged set from list of permutated sets
break;
}
}
//draw
/*
currentSet.forEach(function(tile){
var xCoord = coords[tile].x;
var yCoord = coords[tile].y;
var topLeft = {x:(xPos + xCoord * nodeSize), y:(yPos + yCoord * nodeSize)};
var topRight = {x:(xPos + (xCoord + 1) * nodeSize), y:(yPos + yCoord * nodeSize)};
var bottomLeft = {x:(xPos + xCoord * nodeSize), y:(yPos + (yCoord + 1) * nodeSize)};
var bottomRight = {x:(xPos + (xCoord + 1) * nodeSize), y:(yPos + (yCoord + 1) * nodeSize)};
var mazeTile = maze[xCoord][yCoord];
ctx.strokeStyle = "black";
if(mazeTile["up"] == 1){
ctx.globalCompositeOperation = "source-over";
}
else{
ctx.globalCompositeOperation = "destination-out";
}
ctx.beginPath();
ctx.moveTo(topLeft.x, topLeft.y);
ctx.lineTo(topRight.x, topRight.y)
ctx.stroke();
if(mazeTile["down"] == 1){
ctx.globalCompositeOperation = "source-over";
}
else{
ctx.globalCompositeOperation = "destination-out";
}
ctx.beginPath();
ctx.moveTo(bottomLeft.x, bottomLeft.y);
ctx.lineTo(bottomRight.x, bottomRight.y)
ctx.stroke();
if(mazeTile["left"] == 1){
ctx.globalCompositeOperation = "source-over";
}
else{
ctx.globalCompositeOperation = "destination-out";
}
ctx.beginPath();
ctx.moveTo(topLeft.x, topLeft.y);
ctx.lineTo(bottomLeft.x, bottomLeft.y)
ctx.stroke();
if(mazeTile["right"] == 1){
ctx.globalCompositeOperation = "source-over";
}
else{
ctx.globalCompositeOperation = "destination-out";
}
ctx.beginPath();
ctx.moveTo(topRight.x, topRight.y);
ctx.lineTo(bottomRight.x, bottomRight.y)
ctx.stroke();
ctx.globalCompositeOperation = "source-over";
});
*/
setList = permArray;
//console.log("Maze:", maze)
console.log("SetList:", setList, "MazeSize:", mazeSize, "i:", i, )
//alert("this is a box");
if(setList.length == originalLength){
var a = 1; // this is just a breakpoint place
}
break;
}
while(setList.length != 1); //stop once we've cut out a set
}
return maze;
}
//generateMaze(5,5);
function testTest(arrayIn){
var a;
var b;
for (var k = 0; k < arrayIn.length; k++)
for (var i = (k+1); i < arrayIn.length; i++){
a = arrayIn[k];
b = arrayIn[i];
if(a == b){
return false;
}
}
return true;
}
//var adjacentRoomsT = {up:0, down:0, left:0, right:0};
//for (var direction in adjacentRoomsT){
// console.log(direction)
//}
//console.log("I'm awake, I'm awake")
//console.log(generateMaze(2,2));
/*
var testMatrix = [];
for(var y = 0; y < 6; y++){
var row = [];
for( var x = 0; x < 6; x++){
var fill = x + y*3;
var test = {id:fill};
row.push(test);
}
testMatrix.push(row);
}
drawMaze(testMatrix, 100, 100, 10);
*/
var nodeSize = 20;
var width = 10;
var height = 10;
var pathBorder = 2; //this is a dumb way to do this
maze = generateMaze(width, height, 0, 0, nodeSize);
drawMaze(maze, 0, 0, 20);
document.addEventListener("keypress", function (e) {
var key = e.which || e.keyCode;
if (key === 13) {
x = Number(document.getElementById("xValue").value);
y = Number(document.getElementById("yValue").value);
drawMaze(maze, 0, 0, 20);
t = mazeToTree(maze, x, y);
console.log(t);
paths = findLongestPath(t);
console.log(paths);
var longestLength = 0;
var index = 0;
for (let i = 0; i < paths.length; i++){
if (longestLength < paths[i].distance){
longestLength = paths[i].distance;
index = i;
}
}
longestPath = paths[index].path;
var pathBase = mazeToTree(maze, longestPath[0].x, longestPath[0].y);
console.log(paths[index]);
for (let i = 0; i < longestPath.length; i++){
var currentId = longestPath[i];
//var xCoord = currentId%width;
//var yCoord = Math.floor(currentId/width);
var xCoord = longestPath[i].x;
var yCoord = longestPath[i].y;
var topLeft = {x:(x + xCoord * nodeSize), y:(y + yCoord * nodeSize)};
//var topRight = {x:(x + (xCoord + 1) * nodeSize), y:(y + yCoord * nodeSize)};
//var bottomLeft = {x:(x + xCoord * nodeSize), y:(y + (yCoord + 1) * nodeSize)};
//var bottomRight = {x:(x + (xCoord + 1) * nodeSize), y:(y + (yCoord + 1) * nodeSize)};
ctx.beginPath();
//ctx.strokeStyle="#FF0000";
ctx.fillStyle="#FF0000";
let xOffset = Number(topLeft.x) + nodeSize/(pathBorder*2);
let yOffset = Number(topLeft.y) + nodeSize/(pathBorder*2);
let rectWidth = nodeSize - (nodeSize/pathBorder);
let rectHeight = nodeSize - (nodeSize/pathBorder);
ctx.fillRect(xOffset, yOffset, rectWidth, rectHeight);
}
}
});
//while(waitingForKeyPress){
// mazeToTree(maze, x, y)
//}
//var arara = [];
//console.log(arara.length);
</script>
</body>
</html>