-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
398 lines (367 loc) · 11.9 KB
/
index.html
File metadata and controls
398 lines (367 loc) · 11.9 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
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Enclose Horse</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
image-rendering: pixelated;
}
body {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
min-height: 100vh;
background: #fff;
font-family: "Chicago", "Geneva", "Monaco", monospace;
}
#status {
color: #000;
font-size: 0.9rem;
margin-top: 0.75rem;
display: flex;
align-items: center;
gap: 1rem;
}
#status label {
font-size: 0.75rem;
color: #000;
cursor: pointer;
display: flex;
align-items: center;
gap: 0.3rem;
}
canvas {
border: 2px solid #000;
background: #fff;
}
</style>
</head>
<body>
<canvas id="game" width="640" height="640"></canvas>
<div id="status">
<span id="counts"></span
><label
><input type="checkbox" id="showProtected" /> show protected</label
>
</div>
<script>
const canvas = document.getElementById("game");
const ctx = canvas.getContext("2d");
const GRID = 10;
const CELL = canvas.width / GRID;
const MAX_STONES = 10;
const WATER_TARGET = 20;
const edgeTargets = [
[0, -1],
[0, 1],
[-1, 0],
[1, 0],
];
function minCutToEdge(hc, hr, waterSet) {
const N = GRID * GRID;
const SRC = N * 2;
const SINK = SRC + 1;
const NODES = SINK + 1;
const cap = Array.from({ length: NODES }, () => new Int32Array(NODES));
const adj = Array.from({ length: NODES }, () => []);
function addEdge(u, v, c) {
cap[u][v] += c;
adj[u].push(v);
adj[v].push(u);
}
const id = (c, r) => r * GRID + c;
const IN = (c, r) => id(c, r) * 2;
const OUT = (c, r) => id(c, r) * 2 + 1;
for (let r = 0; r < GRID; r++) {
for (let c = 0; c < GRID; c++) {
if (waterSet.has(`${c},${r}`)) continue;
if (c === hc && r === hr) {
addEdge(IN(c, r), OUT(c, r), 1000);
} else {
addEdge(IN(c, r), OUT(c, r), 1);
}
for (const [dc, dr] of edgeTargets) {
const nc = c + dc,
nr = r + dr;
if (nc < 0 || nr < 0 || nc >= GRID || nr >= GRID) continue;
if (waterSet.has(`${nc},${nr}`)) continue;
addEdge(OUT(c, r), IN(nc, nr), 1000);
}
if (c === 0 || r === 0 || c === GRID - 1 || r === GRID - 1) {
addEdge(OUT(c, r), SINK, 1000);
}
}
}
addEdge(SRC, IN(hc, hr), 1000);
let totalFlow = 0;
while (true) {
const parent = new Int32Array(NODES).fill(-1);
parent[SRC] = SRC;
const queue = [SRC];
let found = false;
for (let qi = 0; qi < queue.length && !found; qi++) {
const u = queue[qi];
for (const v of adj[u]) {
if (parent[v] === -1 && cap[u][v] > 0) {
parent[v] = u;
if (v === SINK) {
found = true;
break;
}
queue.push(v);
}
}
}
if (!found) break;
let flow = Infinity;
for (let v = SINK; v !== SRC; v = parent[v]) {
flow = Math.min(flow, cap[parent[v]][v]);
}
for (let v = SINK; v !== SRC; v = parent[v]) {
cap[parent[v]][v] -= flow;
cap[v][parent[v]] += flow;
}
totalFlow += flow;
if (totalFlow > MAX_STONES) return totalFlow;
}
return totalFlow;
}
function generateMap() {
const hc = 3 + Math.floor(Math.random() * 4);
const hr = 3 + Math.floor(Math.random() * 4);
const prot = new Set();
prot.add(`${hc},${hr}`);
for (const [pdc, pdr] of edgeTargets) {
let c = hc,
r = hr;
const sideways = Math.random() < 0.5 ? 1 : -1;
const atTargetEdge = () =>
(pdc === -1 && c === 0) ||
(pdc === 1 && c === GRID - 1) ||
(pdr === -1 && r === 0) ||
(pdr === 1 && r === GRID - 1);
while (!atTargetEdge()) {
if (Math.random() < 0.7) {
c += pdc;
r += pdr;
} else {
if (pdc === 0) c += sideways;
else r += sideways;
}
c = Math.max(0, Math.min(GRID - 1, c));
r = Math.max(0, Math.min(GRID - 1, r));
prot.add(`${c},${r}`);
}
}
const w = new Set();
const spiralTiles = [];
for (let ring = 0; ring < Math.ceil(GRID / 2); ring++) {
const lo = ring,
hi = GRID - 1 - ring;
for (let c = lo; c <= hi; c++) spiralTiles.push([c, lo]);
for (let r = lo + 1; r <= hi; r++) spiralTiles.push([hi, r]);
for (let c = hi - 1; c >= lo; c--) spiralTiles.push([c, hi]);
for (let r = hi - 1; r > lo; r--) spiralTiles.push([lo, r]);
}
let i = 0;
while (i < spiralTiles.length) {
const [c, r] = spiralTiles[i];
const key = `${c},${r}`;
if (c !== hc || r !== hr) {
if (!prot.has(key)) {
w.add(key);
}
}
i += 1 + Math.floor(Math.random() * 3) + 1;
}
return { hc, hr, prot, w };
}
let attempts = 0;
let horseCol, horseRow, protected_, water;
while (true) {
attempts++;
const map = generateMap();
const cut = minCutToEdge(map.hc, map.hr, map.w);
if (cut <= MAX_STONES - 3) {
horseCol = map.hc;
horseRow = map.hr;
protected_ = map.prot;
water = map.w;
break;
}
}
const stones = new Set();
function drawStoneFill(col, row) {
const x = col * CELL;
const y = row * CELL;
ctx.fillStyle = "#000";
ctx.fillRect(x, y, CELL, CELL);
ctx.fillStyle = "#fff";
for (let py = 0; py < CELL; py += 4) {
for (let px = 0; px < CELL; px += 4) {
if ((px / 4 + py / 4) % 2 === 0) {
ctx.fillRect(x + px, y + py, 2, 2);
}
}
}
}
function drawHorse(col, row) {
const x = col * CELL;
const y = row * CELL;
ctx.font = `${CELL * 0.8}px serif`;
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText("♞", x + CELL / 2, y + CELL / 2 + 2);
}
function isEdge(c, r) {
return c === 0 || r === 0 || c === GRID - 1 || r === GRID - 1;
}
function isBlocked(c, r) {
return water.has(`${c},${r}`) || stones.has(`${c},${r}`);
}
function findPathToEdge() {
const dirs = [
[0, -1],
[0, 1],
[-1, 0],
[1, 0],
];
const visited = new Set();
const prev = new Map();
const queue = [[horseCol, horseRow]];
visited.add(`${horseCol},${horseRow}`);
while (queue.length > 0) {
const [cx, cy] = queue.shift();
if (isEdge(cx, cy)) {
const path = [];
let key = `${cx},${cy}`;
while (key) {
const [px, py] = key.split(",").map(Number);
path.unshift({ c: px, r: py });
key = prev.get(key);
}
return { path, enclosed: null };
}
for (const [dx, dy] of dirs) {
const nx = cx + dx,
ny = cy + dy;
const nk = `${nx},${ny}`;
if (nx < 0 || ny < 0 || nx >= GRID || ny >= GRID) continue;
if (visited.has(nk) || isBlocked(nx, ny)) continue;
visited.add(nk);
prev.set(nk, `${cx},${cy}`);
queue.push([nx, ny]);
}
}
return { path: null, enclosed: visited };
}
let hovering = false;
let escapePath = null;
let enclosedTiles = null;
function updateStatus() {
const el = document.getElementById("counts");
const enclosed = enclosedTiles ? enclosedTiles.size : 0;
el.textContent = `Stones: ${stones.size}/${MAX_STONES} · Enclosed: ${enclosed} · Map attempts: ${attempts}`;
}
function draw() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.strokeStyle = "#000";
ctx.lineWidth = 1;
for (let r = 0; r < GRID; r++) {
for (let c = 0; c < GRID; c++) {
const key = `${c},${r}`;
if (water.has(key)) {
ctx.fillStyle = "#000";
ctx.fillRect(c * CELL, r * CELL, CELL, CELL);
} else if (stones.has(key)) {
drawStoneFill(c, r);
}
ctx.strokeRect(c * CELL, r * CELL, CELL, CELL);
}
}
if (document.getElementById("showProtected").checked) {
for (const key of protected_) {
const [c, r] = key.split(",").map(Number);
if (!water.has(key) && !stones.has(key)) {
ctx.fillStyle = "rgba(0, 0, 0, 0.08)";
ctx.fillRect(c * CELL, r * CELL, CELL, CELL);
}
}
}
if (enclosedTiles) {
ctx.fillStyle = "#000";
for (const key of enclosedTiles) {
const [c, r] = key.split(",").map(Number);
const x = c * CELL,
y = r * CELL;
for (let py = 0; py < CELL; py += 4) {
for (let px = 0; px < CELL; px += 4) {
if ((px / 4 + py / 4) % 4 === 0) {
ctx.fillRect(x + px, y + py, 2, 2);
}
}
}
}
} else if (hovering && escapePath) {
ctx.fillStyle = "rgba(0, 0, 0, 0.12)";
for (const { c, r } of escapePath) {
if (c === horseCol && r === horseRow) continue;
ctx.fillRect(c * CELL, r * CELL, CELL, CELL);
}
}
ctx.fillStyle = "#000";
drawHorse(horseCol, horseRow);
}
canvas.addEventListener("mousemove", (e) => {
const rect = canvas.getBoundingClientRect();
const col = Math.floor((e.clientX - rect.left) / CELL);
const row = Math.floor((e.clientY - rect.top) / CELL);
const wasHovering = hovering;
hovering = col === horseCol && row === horseRow;
if (hovering && !wasHovering && !enclosedTiles) {
const result = findPathToEdge();
escapePath = result.path;
}
if (hovering !== wasHovering) draw();
});
canvas.addEventListener("mouseleave", () => {
hovering = false;
escapePath = null;
draw();
});
canvas.addEventListener("click", (e) => {
const rect = canvas.getBoundingClientRect();
const col = Math.floor((e.clientX - rect.left) / CELL);
const row = Math.floor((e.clientY - rect.top) / CELL);
const key = `${col},${row}`;
if (col === horseCol && row === horseRow) return;
if (water.has(key)) return;
if (stones.has(key)) {
stones.delete(key);
enclosedTiles = null;
updateStatus();
draw();
return;
}
if (enclosedTiles) return;
if (stones.size >= MAX_STONES) return;
stones.add(key);
const result = findPathToEdge();
if (result.enclosed) {
enclosedTiles = result.enclosed;
}
updateStatus();
draw();
});
document.getElementById("showProtected").addEventListener("change", draw);
updateStatus();
draw();
</script>
</body>
</html>