-
Notifications
You must be signed in to change notification settings - Fork 0
/
maze.html
189 lines (166 loc) · 11.5 KB
/
maze.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
<html><head><title>Entombed Maze Generator</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<style>
body { width: 30em; } p { width: 20em; } canvas, div { float: right; }
td { width: 1ex; line-height: 0; vertical-align: bottom; color: dodgerblue; }
tr:nth-child(odd) td { border-top: .2ex solid white; }
tr:nth-child(even) td { border-bottom: .2ex solid white; }
.bg0 { background: black; } .bg1 { background: rgb(190,173,81); }
.new { color: dodgerblue; position: relative; top: .6ex; }
</style></head><body>
<canvas width="320" height="178"></canvas>
<div><h2>Генератор лабиринтов из Entombed</h2>
<p>Для каждой генерируемой ячейки (<span class="new">*</span>)
рассматриваются пять ранее сгенерированных (три сверху и две слева),
и в соответствии с таблицей выбирается состояние новой ячейки:
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAA0AAAANCAYAAABy6+R8AAAAPklEQVQoFWN4/vz5/2nTpv0vKysjiEHqQOoZQIxjx479JwaA1IHUM4BsIAWA1I9q+v8fHC2DPSDIShHkpD0AEIVaC+5fze8AAAAASUVORK5CYII=" /> – проход,
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAA0AAAANCAYAAABy6+R8AAAAqElEQVQoFWOYd/r/f9GG//8ZSgljkDqQegZiNcAMBauHcXDRAnX//++/8/9/wAIkl+BSDBNfcOY/GHz4TqSmgk0QDSDSoB+HJpBTYDY4zEBoSFiJEAfLwxSBJN5/g5io0AZhg7SBnAdTA6dhjAvPICaDNJ5/CmGDxGDyKDSMA3IaTCNIC8jjyM6FqQPTyBxkjSgeR494ZE0gNkgjSpygawDxyUoR5KQ9AN41yQz6o80KAAAAAElFTkSuQmCC" /> – стена,
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAA0AAAANCAYAAABy6+R8AAAAX0lEQVQoFWN4/vz5/2nTpv0vKysjiEHqQOoZQIxjx479JwaA1IHUM4BsIAWA1A+Aps+fP//fvXs3TgySBwEU54E04AtBkDyGJrJsAhtDBIHiPCLUg5WQr4msFEFO2gMA+IJBfqiksRkAAAAASUVORK5CYII=" /> – случайный выбор.</p>
</div>
<table>
<tr><td></td><td class="bg0"></td><td class="bg0"></td><td class="bg0"></td><td rowspan="2"><input type="checkbox" id="magic00000" checked="checked" /></td></tr>
<tr><td class="bg0"></td><td class="bg0"></td><td>*</td></tr>
<tr><td></td><td class="bg0"></td><td class="bg0"></td><td class="bg1"></td><td rowspan="2"><input type="checkbox" id="magic00001" checked="checked" /></td></tr>
<tr><td class="bg0"></td><td class="bg0"></td><td>*</td></tr>
<tr><td></td><td class="bg0"></td><td class="bg1"></td><td class="bg0"></td><td rowspan="2"><input type="checkbox" id="magic00010" checked="checked" /></td></tr>
<tr><td class="bg0"></td><td class="bg0"></td><td>*</td></tr>
<tr><td></td><td class="bg0"></td><td class="bg1"></td><td class="bg1"></td><td rowspan="2"><input type="checkbox" id="magic00011" indeterminate="true" /></td></tr>
<tr><td class="bg0"></td><td class="bg0"></td><td>*</td></tr>
<tr><td></td><td class="bg1"></td><td class="bg0"></td><td class="bg0"></td><td rowspan="2"><input type="checkbox" id="magic00100" /></td></tr>
<tr><td class="bg0"></td><td class="bg0"></td><td>*</td></tr>
<tr><td></td><td class="bg1"></td><td class="bg0"></td><td class="bg1"></td><td rowspan="2"><input type="checkbox" id="magic00101" /></td></tr>
<tr><td class="bg0"></td><td class="bg0"></td><td>*</td></tr>
<tr><td></td><td class="bg1"></td><td class="bg1"></td><td class="bg0"></td><td rowspan="2"><input type="checkbox" id="magic00110" indeterminate="true" /></td></tr>
<tr><td class="bg0"></td><td class="bg0"></td><td>*</td></tr>
<tr><td></td><td class="bg1"></td><td class="bg1"></td><td class="bg1"></td><td rowspan="2"><input type="checkbox" id="magic00111" indeterminate="true" /></td></tr>
<tr><td class="bg0"></td><td class="bg0"></td><td>*</td></tr>
<tr><td></td><td class="bg0"></td><td class="bg0"></td><td class="bg0"></td><td rowspan="2"><input type="checkbox" id="magic01000" checked="checked" /></td></tr>
<tr><td class="bg0"></td><td class="bg1"></td><td>*</td></tr>
<tr><td></td><td class="bg0"></td><td class="bg0"></td><td class="bg1"></td><td rowspan="2"><input type="checkbox" id="magic01001" checked="checked" /></td></tr>
<tr><td class="bg0"></td><td class="bg1"></td><td>*</td></tr>
<tr><td></td><td class="bg0"></td><td class="bg1"></td><td class="bg0"></td><td rowspan="2"><input type="checkbox" id="magic01010" checked="checked" /></td></tr>
<tr><td class="bg0"></td><td class="bg1"></td><td>*</td></tr>
<tr><td></td><td class="bg0"></td><td class="bg1"></td><td class="bg1"></td><td rowspan="2"><input type="checkbox" id="magic01011" checked="checked" /></td></tr>
<tr><td class="bg0"></td><td class="bg1"></td><td>*</td></tr>
<tr><td></td><td class="bg1"></td><td class="bg0"></td><td class="bg0"></td><td rowspan="2"><input type="checkbox" id="magic01100" indeterminate="true" /></td></tr>
<tr><td class="bg0"></td><td class="bg1"></td><td>*</td></tr>
<tr><td></td><td class="bg1"></td><td class="bg0"></td><td class="bg1"></td><td rowspan="2"><input type="checkbox" id="magic01101" /></td></tr>
<tr><td class="bg0"></td><td class="bg1"></td><td>*</td></tr>
<tr><td></td><td class="bg1"></td><td class="bg1"></td><td class="bg0"></td><td rowspan="2"><input type="checkbox" id="magic01110" /></td></tr>
<tr><td class="bg0"></td><td class="bg1"></td><td>*</td></tr>
<tr><td></td><td class="bg1"></td><td class="bg1"></td><td class="bg1"></td><td rowspan="2"><input type="checkbox" id="magic01111" /></td></tr>
<tr><td class="bg0"></td><td class="bg1"></td><td>*</td></tr>
<tr><td></td><td class="bg0"></td><td class="bg0"></td><td class="bg0"></td><td rowspan="2"><input type="checkbox" id="magic10000" checked="checked" /></td></tr>
<tr><td class="bg1"></td><td class="bg0"></td><td>*</td></tr>
<tr><td></td><td class="bg0"></td><td class="bg0"></td><td class="bg1"></td><td rowspan="2"><input type="checkbox" id="magic10001" checked="checked" /></td></tr>
<tr><td class="bg1"></td><td class="bg0"></td><td>*</td></tr>
<tr><td></td><td class="bg0"></td><td class="bg1"></td><td class="bg0"></td><td rowspan="2"><input type="checkbox" id="magic10010" checked="checked" /></td></tr>
<tr><td class="bg1"></td><td class="bg0"></td><td>*</td></tr>
<tr><td></td><td class="bg0"></td><td class="bg1"></td><td class="bg1"></td><td rowspan="2"><input type="checkbox" id="magic10011" indeterminate="true" /></td></tr>
<tr><td class="bg1"></td><td class="bg0"></td><td>*</td></tr>
<tr><td></td><td class="bg1"></td><td class="bg0"></td><td class="bg0"></td><td rowspan="2"><input type="checkbox" id="magic10100" /></td></tr>
<tr><td class="bg1"></td><td class="bg0"></td><td>*</td></tr>
<tr><td></td><td class="bg1"></td><td class="bg0"></td><td class="bg1"></td><td rowspan="2"><input type="checkbox" id="magic10101" /></td></tr>
<tr><td class="bg1"></td><td class="bg0"></td><td>*</td></tr>
<tr><td></td><td class="bg1"></td><td class="bg1"></td><td class="bg0"></td><td rowspan="2"><input type="checkbox" id="magic10110" /></td></tr>
<tr><td class="bg1"></td><td class="bg0"></td><td>*</td></tr>
<tr><td></td><td class="bg1"></td><td class="bg1"></td><td class="bg1"></td><td rowspan="2"><input type="checkbox" id="magic10111" /></td></tr>
<tr><td class="bg1"></td><td class="bg0"></td><td>*</td></tr>
<tr><td></td><td class="bg0"></td><td class="bg0"></td><td class="bg0"></td><td rowspan="2"><input type="checkbox" id="magic11000" indeterminate="true" /></td></tr>
<tr><td class="bg1"></td><td class="bg1"></td><td>*</td></tr>
<tr><td></td><td class="bg0"></td><td class="bg0"></td><td class="bg1"></td><td rowspan="2"><input type="checkbox" id="magic11001" /></td></tr>
<tr><td class="bg1"></td><td class="bg1"></td><td>*</td></tr>
<tr><td></td><td class="bg0"></td><td class="bg1"></td><td class="bg0"></td><td rowspan="2"><input type="checkbox" id="magic11010" checked="checked" /></td></tr>
<tr><td class="bg1"></td><td class="bg1"></td><td>*</td></tr>
<tr><td></td><td class="bg0"></td><td class="bg1"></td><td class="bg1"></td><td rowspan="2"><input type="checkbox" id="magic11011" indeterminate="true" /></td></tr>
<tr><td class="bg1"></td><td class="bg1"></td><td>*</td></tr>
<tr><td></td><td class="bg1"></td><td class="bg0"></td><td class="bg0"></td><td rowspan="2"><input type="checkbox" id="magic11100" indeterminate="true" /></td></tr>
<tr><td class="bg1"></td><td class="bg1"></td><td>*</td></tr>
<tr><td></td><td class="bg1"></td><td class="bg0"></td><td class="bg1"></td><td rowspan="2"><input type="checkbox" id="magic11101" /></td></tr>
<tr><td class="bg1"></td><td class="bg1"></td><td>*</td></tr>
<tr><td></td><td class="bg1"></td><td class="bg1"></td><td class="bg0"></td><td rowspan="2"><input type="checkbox" id="magic11110" /></td></tr>
<tr><td class="bg1"></td><td class="bg1"></td><td>*</td></tr>
<tr><td></td><td class="bg1"></td><td class="bg1"></td><td class="bg1"></td><td rowspan="2"><input type="checkbox" id="magic11111" /></td></tr>
<tr><td class="bg1"></td><td class="bg1"></td><td>*</td></tr>
</table>
<script>
$('input[type=checkbox]')
.each(function() { this.indeterminate = !!this.getAttribute('indeterminate'); })
.click(function() {
if (this.checked && !this.getAttribute('indeterminate')) {
// unchecked, going indeterminate
this.checked = false;
this.indeterminate = true;
this.setAttribute('indeterminate', true);
} else if (this.getAttribute('indeterminate')) {
// indeterminate, going checked
this.removeAttribute('indeterminate');
} else {
// checked, going unchecked
}
});
const canvas = $('canvas').get()[0];
const context = canvas.getContext('2d');
context.fillStyle = 'black';
context.fillRect(0, 0, 320, 178);
function prrow(seed, frame) {
if (frame)
setTimeout(prrow, 40, seed, frame-1);
else
setTimeout(rowgen, 40);
context.drawImage(canvas, 0, -1);
context.fillStyle = 'black';
context.fillRect(0, 177, 320, 1);
context.fillStyle = 'rgb(190,173,81)';
for (let i=0; i<8; i++) {
if (seed & 1) {
context.fillRect(144-16*i, 177, 16, 1);
context.fillRect(160+16*i, 177, 16, 1);
}
seed >>= 1;
}
context.fillRect(0, 177, 32, 1);
context.fillRect(288, 177, 32, 1);
}
function rowgen() {
// prepend and append random bits to last row
let lastrowpadded = Math.random() < 0.5;
lastrowpadded <<= 8;
lastrowpadded |= lastrows[lastrows.length-1];
lastrowpadded <<= 1;
lastrowpadded |= Math.random() < 0.5;
// last two bits generated in current row, initial value = 10
let lasttwo = 2
let newrow = 0
// iterate from 7...0 , inclusive
for (let i=7; i>=0; i--) {
let threeabove = (lastrowpadded >> i) & 7;
let newbit = $('#magic' + (32 | lasttwo<<3 | threeabove).toString(2).substring(1)).get(0);
if (newbit.indeterminate)
newbit = Math.random() < 0.5;
else
newbit = +newbit.checked;
newrow = (newrow << 1) | newbit;
lasttwo = ((lasttwo << 1) | newbit) & 3;
}
// now do postprocessing
lastrows.push(newrow);
lastrows = lastrows.slice(-11);
// postprocessing condition 1
let history = lastrows.map(b => b & 0xf0);
if (history.indexOf(0) == -1)
if (!history.filter(b => b & 0x80).length)
lastrows[lastrows.length-1] = 0;
// postprocessing condition 2
history = lastrows.slice(-7).map(b => b & 0xf);
if (history.indexOf(0) == -1) {
let comparator = lastrows.length>=9 ? lastrows[lastrows.length-9]&1 : 0;
if (!history.filter(b => (b&1) != comparator).length)
lastrows[lastrows.length-1] &= 0xf0;
}
prrow(lastrows[lastrows.length-1], 16);
}
var lastrows = [0];
rowgen();
</script>
</body></html>