-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMazeDrawTest.html
More file actions
740 lines (529 loc) · 15.4 KB
/
Copy pathMazeDrawTest.html
File metadata and controls
740 lines (529 loc) · 15.4 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
<!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.rootId = -1;
this.xCoord = -1;
this.yCoord = -1;
this.leaves = [];
this.distances = [];
}
init(id, x, y){
this.rootId = id;
this.xCoord = x;
this.yCoord = y;
this.leaves = [];
this.distances = [];
}
initFromNodeObject(nodeObj){
this.rootId = nodeObj.id;
this.xCoord = nodeObj.x;
this.yCoord = nodeObj.y;
this.leaves = [];
this.distances = [];
}
}
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(tileCoord, maze, lastTile){
var nodeObj = {id: -1, x: -1, y: -1, lastTileId: -1};
var travelStack = [];
travelStack.push(lastTile);
var xCoord = tileCoord.x;
var yCoord = tileCoord.y;
var tile = maze[xCoord][yCoord];
let tileMove;
while(!isNode(tile)){
let newX = xCoord;
let newY = yCoord;
nodeObj.lastTile = 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)){
tile = tileMove;
xCoord = newX;
yCoord = newY;
nodeObj.id = tile.id;
nodeObj.x = newX;
nodeObj.y = newY;
travelStack.push(tile.id);
travelDirStack.push(dir);
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;
}
}
}
}
}
return nodeObj;
}
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 links = [];
var distances = [];
//let tree = new treeRoot(tile["id"], xCoord, yCoord);
var travelStack = [];
var travelDirStack = [];
travelStack.push(tile.id);
travelDirStack.push("Start");
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;
while(!isNode(tile)){
let newX = xCoord;
let newY = yCoord;
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)){
tile = tileMove;
xCoord = newX;
yCoord = newY;
travelStack.push(tile.id);
travelDirStack.push(dir);
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;
}
}
}
}
}
console.log(travelStack);
console.log(travelDirStack);
let tree = new treeRoot();//.init(tile["id"], xCoord, yCoord);
tree.init(tile["id"], xCoord, yCoord);
return tree;
}
function findLongestPath(mazeTree){
//maze[x][y][{id:, up:, down:, left:, right:}]
}
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:}]
var width = maze.length;
var height = maze[0].length;
/*
ctx.beginPath();
ctx.moveTo(x,y);
ctx.lineTo(x + width*nodeSize, y);
ctx.lineTo(x + width*nodeSize, y + height*nodeSize);
ctx.lineTo(x, y + height*nodeSize);
ctx.lineTo(x, y);
ctx.stroke();
*/
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();
});
}
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);
*/
maze = generateMaze(15, 15, 0, 0, 20);
document.addEventListener("keypress", function (e) {
var key = e.which || e.keyCode;
if (key === 13) {
x = document.getElementById("xValue").value;
y = document.getElementById("yValue").value;
t = mazeToTree(maze, x, y)
console.log(t);
}
});
//while(waitingForKeyPress){
// mazeToTree(maze, x, y)
//}
//var arara = [];
//console.log(arara.length);
</script>
</body>
</html>