-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaze.js
113 lines (94 loc) · 3.91 KB
/
maze.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
class BlockMaze {
constructor() {
console.log("BlockMaze constructor")
this.cells = []
}
initialise(width, height) {
this.width = width
this.height = height
this.size = this.width*this.height
if ( this.width/2 == this.width)
{
this.width+=1
}
if ( this.height/2 == this.height)
{
this.height+=1
}
for (var n=0; n<this.size; n++)
{
this.cells[n] = 1; // wall
}
}
generate(startX = 1, startY = 1) {
this.generate2(startX, startY);
this.cells[this.width] = 0;
// this.cells[1] = 0;
// this.cells[this.size-2] = 0;
}
generate2(currentCellX, currentCellY) {
var randomDirections = this.generateRandomDirections();
// console.log(currentCellX + " " + currentCellY + " " + randomDirections + " " + randomDirections.length);
for (var i = 0; i < randomDirections.length; i++) {
switch(randomDirections[i]) {
case 'N':
if (currentCellY-2>0)
{
if (this.cells[currentCellX + ((currentCellY - 2)*this.width)] != 0 )
{
this.cells[currentCellX + ((currentCellY - 2)*this.width)] = 0; // make cell empty
this.cells[currentCellX + ((currentCellY - 1)*this.width)] = 0; // make wall between emtpy (also a cell)
this.generate2(currentCellX, currentCellY - 2);
}
}
break;
case 'S':
if ( currentCellY+2<this.height-1)
{
if (this.cells[currentCellX + ((currentCellY + 2)*this.width)] != 0 )
{
this.cells[currentCellX + ((currentCellY + 2)*this.width)] = 0; // make cell empty
this.cells[currentCellX + ((currentCellY + 1)*this.width)] = 0; // make wall between emtpy (also a cell)
this.generate2(currentCellX, currentCellY + 2);
}
}
break;
case 'W':
if ( currentCellX-2>0)
{
if (this.cells[currentCellX-2 + (currentCellY * this.width)] != 0 )
{
this.cells[currentCellX-2 + (currentCellY * this.width)] = 0; // make cell empty
this.cells[currentCellX-1 + (currentCellY * this.width)] = 0; // make wall between emtpy (also a cell)
this.generate2(currentCellX - 2, currentCellY);
}
}
break;
case 'E':
if ( currentCellX+2<this.width-1)
{
if (this.cells[currentCellX+2 + (currentCellY*this.width)] != 0 )
{
this.cells[currentCellX+2 + (currentCellY * this.width)] = 0; // make cell empty
this.cells[currentCellX+1 + (currentCellY * this.width)] = 0; // make wall between emtpy (also a cell)
this.generate2(currentCellX + 2, currentCellY);
}
}
break;
}
}
}
generateRandomDirections(directions) {
var directions = new Array()
directions = [ 'N', 'E', 'S', 'W']
// directions = [ 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'N', 'S', 'E', 'S', 'W']
// directions = [ 'E', 'E', 'E', 'E', 'E', 'E', 'E', 'W', 'W', 'E', 'S', 'W']
return UTILS.shuffle(directions)
}
log() {
// console.log(this.cells)
}
occupied(row, col) {
return this.cells[row*this.width + col]
}
}