-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
executable file
·264 lines (213 loc) · 7.06 KB
/
index.html
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
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body {
background-color: darkblue;
color: greenyellow;
font-size: 18px;
}
pre {
font-family: Consolas,Menlo,Monaco,Lucida Console,Liberation Mono,DejaVu Sans Mono,Bitstream Vera Sans Mono,Courier New,monospace;
}
.wrapper {
display: flex;
align-items: center;
justify-content: center;
height: 100%;
}
</style>
</head>
<body>
<div class="wrapper">
<pre id="field"><span id="testChar">X</span></pre>
</div>
<script>
const tc = document.getElementById("testChar");
const charWidth = tc.offsetWidth;
const charHeight = tc.offsetHeight;
const up = 1;
const right = 2;
const down = 4;
const left = 8;
const intervalDuration = 150;
const propability = 0.5;
let changeInterval;
init(propability)
function init(propability) {
window.clearInterval(changeInterval);
let width;
let height;
[width, height] = getFieldDimensions();
const field = createInitialField(width, height, propability);
removeLooseEnds(field, width - 1, height - 1);
showField(field);
changeInterval = window.setInterval(() => {
let x, y;
[x, y] = getRandomPoint(width, height);
removeAny(field, x, y);
[x, y] = getRandomPoint(width, height);
addAny(field, x, y);
showField(field);
}, intervalDuration);
}
function getRandomPoint(width, height) {
let x = Math.floor(Math.random() * (width - 1));
let y = Math.floor(Math.random() * (height - 1));
return [x, y];
}
function createInitialField(width, height, propability) {
const field = [];
let upperNeighbour;
let leftNeighbour;
let hasUpConnection;
let hasLeftConnection;
let hasDownConnection;
let hasRightConnection;
for (let y = 0; y < height; y++) {
field[y] = [];
for (let x = 0; x < width; x++) {
leftNeighbour = (x > 0) ? field[y][x - 1] : 0;
upperNeighbour = (y > 0) ? field[y - 1][x] : 0;
hasUpConnection = !!(upperNeighbour & down);
hasLeftConnection = !!(leftNeighbour & right);
if (x == width - 1) {
hasRightConnection = false;
} else {
hasRightConnection = propably(propability);
}
if (y == height - 1) {
hasDownConnection = false;
} else {
hasDownConnection = propably(propability);
}
if ((hasUpConnection != hasLeftConnection) && !(hasDownConnection || hasRightConnection) && !(x == width - 1 && y == height - 1)) {
if (y == height - 1) {
hasRightConnection = true;
} else if (x == width - 1) {
hasDownConnection = true;
} else if (propably(propability)) {
hasDownConnection = true;
} else {
hasRightConnection = true;
}
}
if (!(hasUpConnection || hasLeftConnection) && (hasDownConnection != hasRightConnection)) {
hasRightConnection = false;
hasDownConnection = false;
}
field[y][x] = (hasUpConnection ? up : 0) + (hasRightConnection ? right : 0) + (hasDownConnection ? down : 0) + (hasLeftConnection ? left : 0);
}
}
return field;
}
function removeAny(field, x, y) {
const dir = getRandomPossibleDirection(x, y, field);
let nX, nY;
if (field[y][x] & dir) {
field[y][x] = field[y][x] & ~dir;
removeLooseEnds(field, x, y);
[nX, nY] = getPointInDirection(x, y, dir);
field[nY][nX] = field[nY][nX] & ~getOppositeDirection(dir);
removeLooseEnds(field, nX, nY);
}
}
function addAny(field, x, y) {
const dir = getRandomPossibleDirection(x, y, field);
let nX, nY;
if (!(field[y][x] & dir)) {
field[y][x] = field[y][x] | dir;
continueLostEnds(field, x, y);
[nX, nY] = getPointInDirection(x, y, dir);
field[nY][nX] = field[nY][nX] | getOppositeDirection(dir);
continueLostEnds(field, nX, nY);
}
}
function continueLostEnds(field, x, y) {
let nX, nY;
let dir;
while (hasLessThanTwoConnections(field[y][x])) {
dir = getNewDirection(x, y, field);
[nX, nY] = getPointInDirection(x, y, dir);
field[y][x] = field[y][x] | dir;
field[nY][nX] = field[nY][nX] | getOppositeDirection(dir);
[x, y] = [nX, nY];
}
}
function removeLooseEnds(field, x, y) {
let nX, nY;
let dir;
while (field[y][x] != 0 && hasLessThanTwoConnections(field[y][x])) {
dir = field[y][x];
field[y][x] = 0;
[nX, nY] = getPointInDirection(x, y, dir);
field[nY][nX] = field[nY][nX] & ~getOppositeDirection(dir);
[x, y] = [nX, nY];
}
}
function hasLessThanTwoConnections(current) {
return [0,1,2,4,8].indexOf(current) != -1;
}
function getNewDirection(x, y, field) {
const current = field[y][x];
let dir;
do {
dir = getRandomPossibleDirection(x, y, field);
} while (current & dir);
return dir;
}
function getRandomPossibleDirection(x, y, field) {
let dir;
do {
dir = Math.pow(2, Math.floor(Math.random() * 4));
} while (!isDirPossible(x, y, field[0].length, field.length, dir));
return dir;
}
function field2String(field) {
//const symbols = [' ', '1', '2', '└', '4', '│', '┌', '├', '8', '┘', '─', '┴', '┐', '┤', '┬', '┼'];
const symbols = [' ', '1', '2', '╚', '4', '║', '╔', '╠', '8', '╝', '═', '╩', '╗', '╣', '╦', '╬'];
let str = '';
for (let y = 0; y < field.length; y++) {
for (let x = 0; x < field[0].length; x++) {
str += symbols[field[y][x]];
}
str += '\n';
}
return str;
}
function showField(field) {
const str = field2String(field);
document.getElementById('field').innerText = str;
}
function propably(propability) {
return Math.random() < propability;
}
function getFieldDimensions() {
const maxWidth = window.innerWidth * 0.7;
const maxHeight = window.innerHeight * 0.7;
return [Math.floor(maxWidth / charWidth), Math.floor(maxHeight / charHeight)];
}
function getPointInDirection(x, y, dir) {
switch (dir) {
case up: return [x, y - 1];
case right: return [x + 1, y];
case down: return [x, y + 1];
case left: return [x -1, y];
}
}
function isDirPossible(x, y, maxX, maxY, dir) {
let nX, nY;
[nX, nY] = getPointInDirection(x, y, dir);
return nX > -1 && nX < maxX && nY > -1 && nY < maxY;
}
function getOppositeDirection(dir) {
switch (dir) {
case up: return down;
case right: return left;
case down: return up;
case left: return right;
}
}
</script>
</body>
</html>