-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlabyrinth.js
More file actions
144 lines (135 loc) · 3.26 KB
/
labyrinth.js
File metadata and controls
144 lines (135 loc) · 3.26 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
class Labyrinth {
_width;
_height;
_matrix;
_verticalLines;
_horizontalLines;
_lastStep;
startPositions;
constructor({ width, height, verticalProbability, horizontalProbability }) {
if (width < 10) {
throw Error('invalid width');
}
if (height < 10) {
throw Error('invalid height');
}
this._width = width;
this._height = height;
this._lastStep = 1;
this._matrix = [];
for (let x = 0; x < width; x++) {
this._matrix.push([]);
}
do {
this._verticalLines = [];
for (let x = 0; x < width - 1; x++) {
const lines = [];
for (let y = 0; y < height; y++) {
lines.push(Math.random() < verticalProbability);
}
this._verticalLines.push(lines);
}
this._horizontalLines = [];
this.startPositions = [];
for (let x = 0; x < width; x++) {
const lines = [];
for (let y = 0; y < height + 1; y++) {
lines.push(Math.random() < horizontalProbability);
}
if (!lines[0]) {
this.startPositions.push({
x,
y: 0,
});
}
this._horizontalLines.push(lines);
}
} while (this.startPositions.length < 1);
}
shape() {
const matrix = [];
for (let x = 0; x < this._width; x++) {
const lines = [];
for (let y = 0; y < this._height; y++) {
lines.push({
top: this._horizontalLines[x][y],
bottom: this._horizontalLines[x][y + 1],
left: x - 1 < 0 ? true : this._verticalLines[x - 1][y],
right: x >= this._width - 1 ? true : this._verticalLines[x][y],
step: this.checkVisited({ x, y }),
});
}
matrix.push(lines);
}
return {
matrix,
width: this._width,
height: this._height,
};
}
visit({ x, y, step }) {
if (step < this._lastStep) {
throw Error('invalid step count');
}
if (!this.checkPosition({ x, y })) {
throw Error('invalid {x,y} position');
}
if (this.checkVisited({ x, y })) {
return false;
}
this._matrix[x][y] = step;
return true;
}
getNextPositions({ x, y }) {
if (!this.checkPosition({ x, y })) {
throw Error('invalid {x,y} position');
}
return [
{ x: x - 1, y },
{ x: x + 1, y },
{ x, y: y - 1 },
{ x, y: y + 1 },
]
.filter((position) => {
return (
!this.checkVisited(position) &&
this.canMoveTo({
...position,
ox: x,
oy: y,
})
);
})
.map((position) => ({
...position,
bottom: this._horizontalLines[position.x][position.y + 1],
}));
}
canMoveTo({ ox, oy, x, y }) {
if (Math.abs(ox - x) + Math.abs(oy - y) > 1) {
return false;
}
if (!this.checkPosition({ x, y })) {
return false;
}
if (ox == x) {
y = Math.max(oy, y);
return !this._horizontalLines[x][y];
} else {
x = Math.min(ox, x);
return !this._verticalLines[x][y];
}
}
checkPosition({ x, y }) {
if (x < 0 || x >= this._width) {
return false;
}
if (y < 0 || y >= this._height) {
return false;
}
return true;
}
checkVisited({ x, y }) {
return (this._matrix[x] || [])[y];
}
}