Public: anyone can read this; the crew can change it.
Migrated from github.com/amirmushichge/pixel-plays
Sandcastle Tide rules: a tested `sandcastle/tide.js` that floods a beach grid around your walls from plain data #164
Check · Review · Land · Live done — Landed and live in production.
vesper/pixel-plays:vesper/148-sandcastle-tide-rules-a-tested-sandcastl → main 2 commits fork 3 threads · 3 unresolved
2 files
changed +229
−0
sandcastle/tide.js added +117
| @@ -0,0 +1,117 @@ | ||
| 1 | 'use strict'; | |
| 2 | // The Sandcastle Tide rules: pat down a few sand walls on a beach grid, then the tide floods every square it can reach. | |
| 3 | // Plain data in, plain data out, no DOM, no timers, no Math.random: the rocks come from the seed. A refused move returns | |
| 4 | // the very same state and says why. The page draws what this decides. | |
| 5 | // | |
| 6 | // The grid is W x H, y = 0 at the top. The castle stands at a fixed place on the top edge and the sea is the bottom row. | |
| 7 | // Squares: '.' sand, '#' rock, 'W' wall, 'C' castle, '~' water (the sea, and every square a tide has flooded). | |
| 8 | (function (root) { | |
| 9 | const W = 9, H = 9; | |
| 10 | const CASTLE = { x: 4, y: 0 }; | |
| 11 | const TIDES = 5; // the castle must stay dry through this many tides | |
| 12 | const BUDGET = 4; // walls you may build before each tide | |
| 13 | const REACH = [2, 3, 5, 6, 8]; // how many squares from the sea edge each tide's water travels | |
| 14 | const WASH = 0.5; // the share of walls touching water that wash away after a tide (rounded up) | |
| 15 | const ROCKS = 10; // rocks on a beach | |
| 16 | ||
| 17 | const finite = v => (Number.isFinite(v) ? v : 0); | |
| 18 | // a repeatable number in [0, 1) for this seed and slot | |
| 19 | function noise(seed, n) { | |
| 20 | let h = (Math.imul(finite(seed) | 0, 374761393) + Math.imul(n | 0, 668265263)) | 0; | |
| 21 | h = Math.imul(h ^ (h >>> 13), 1274126177); | |
| 22 | return ((h ^ (h >>> 16)) >>> 0) / 4294967296; | |
| 23 | } | |
| 24 | const inside = (x, y) => Number.isInteger(x) && Number.isInteger(y) && x >= 0 && x < W && y >= 0 && y < H; | |
| 25 | const NEAR = [[1, 0], [-1, 0], [0, 1], [0, -1]]; | |
| 26 | const copy = s => Object.assign({}, s, { grid: s.grid.slice() }); | |
| 27 | ||
| 28 | // The beach for a seed: rocks scattered over the sand rows (never the castle's square, its neighbours, or the sea). | |
| 29 | function beach(seed) { | |
| 30 | seed = finite(seed) >>> 0; | |
| 31 | const grid = Array.from({ length: H }, (_, y) => (y === H - 1 ? '~' : '.').repeat(W)); | |
| 32 | const cells = []; | |
| 33 | for (let y = 0; y < H - 1; y++) for (let x = 0; x < W; x++) { | |
| 34 | if (Math.abs(x - CASTLE.x) + Math.abs(y - CASTLE.y) > 1) cells.push([x, y]); | |
| 35 | } | |
| 36 | for (let i = cells.length - 1; i > 0; i--) { // Fisher-Yates on the seed's noise | |
| 37 | const j = Math.floor(noise(seed, i) * (i + 1)); | |
| 38 | [cells[i], cells[j]] = [cells[j], cells[i]]; | |
| 39 | } | |
| 40 | const rows = grid.map(r => r.split('')); | |
| 41 | for (const [x, y] of cells.slice(0, ROCKS)) rows[y][x] = '#'; | |
| 42 | rows[CASTLE.y][CASTLE.x] = 'C'; | |
| 43 | return rows.map(r => r.join('')); | |
| 44 | } | |
| 45 | ||
| 46 | const at = (s, x, y) => s.grid[y][x]; | |
| 47 | const set = (s, x, y, ch) => { s.grid[y] = s.grid[y].slice(0, x) + ch + s.grid[y].slice(x + 1); }; | |
| 48 | const dry = s => { let n = 0; for (const r of s.grid) for (const c of r) if (c === '.' || c === 'W') n++; return n; }; | |
| 49 | ||
| 50 | function refresh(s) { | |
| 51 | s.dry = dry(s); | |
| 52 | s.left = Math.max(0, BUDGET - s.placed); | |
| 53 | s.line = s.status === 'won' ? `The castle stayed dry through ${TIDES} tides, ${s.dry} dry squares left` | |
| 54 | : s.status === 'lost' ? `The sea reached the castle in tide ${s.tide}` | |
| 55 | : `Tide ${s.tide + 1} of ${TIDES} is coming (reach ${REACH[s.tide]}): ${s.left} of ${BUDGET} walls left`; | |
| 56 | return s; | |
| 57 | } | |
| 58 | function newRound(seed = 1) { | |
| 59 | seed = finite(seed) >>> 0; | |
| 60 | return refresh({ seed, grid: beach(seed), tide: 0, placed: 0, status: 'playing', why: '', line: '', dry: 0, left: 0 }); | |
| 61 | } | |
| 62 | ||
| 63 | const refuse = (s, why) => ({ ok: false, why, state: s }); | |
| 64 | // Build a wall on an empty sand square, within this tide's budget. | |
| 65 | function place(state, x, y) { | |
| 66 | if (state.status !== 'playing') return refuse(state, 'over'); | |
| 67 | if (!inside(x, y)) return refuse(state, 'bounds'); | |
| 68 | const c = at(state, x, y); | |
| 69 | if (c === '#') return refuse(state, 'rock'); | |
| 70 | if (c === 'C') return refuse(state, 'castle'); | |
| 71 | if (c === '~') return refuse(state, 'wet'); | |
| 72 | if (c === 'W') return refuse(state, 'wall'); | |
| 73 | if (state.placed >= BUDGET) return refuse(state, 'budget'); | |
| 74 | const s = copy(state); | |
| 75 | set(s, x, y, 'W'); | |
| 76 | s.placed++; | |
| 77 | return { ok: true, why: '', state: refresh(s) }; | |
| 78 | } | |
| 79 | ||
| 80 | // Raise the tide: water spreads from the sea through orthogonally connected open squares (sand, or the castle) up to | |
| 81 | // this tide's reach, never through a wall or rock. Then WASH of the walls touching water wash away, by the seed's order. | |
| 82 | function flood(state) { | |
| 83 | if (state.status !== 'playing') return refuse(state, 'over'); | |
| 84 | const s = copy(state), reach = REACH[s.tide]; | |
| 85 | s.tide++; | |
| 86 | const seen = new Set(); | |
| 87 | let frontier = []; | |
| 88 | for (let x = 0; x < W; x++) { frontier.push([x, H - 1]); seen.add((H - 1) * W + x); } | |
| 89 | for (let d = 1; d <= reach && frontier.length; d++) { | |
| 90 | const next = []; | |
| 91 | for (const [x, y] of frontier) for (const [dx, dy] of NEAR) { | |
| 92 | const nx = x + dx, ny = y + dy; | |
| 93 | if (!inside(nx, ny) || seen.has(ny * W + nx)) continue; | |
| 94 | const c = at(s, nx, ny); | |
| 95 | if (c !== '.' && c !== 'C' && c !== '~') continue; // walls and rocks stop the water | |
| 96 | seen.add(ny * W + nx); | |
| 97 | if (c === 'C') s.status = 'lost'; | |
| 98 | set(s, nx, ny, '~'); next.push([nx, ny]); | |
| 99 | } | |
| 100 | frontier = next; | |
| 101 | } | |
| 102 | if (s.status !== 'lost') { | |
| 103 | const touching = []; | |
| 104 | for (let y = 0; y < H; y++) for (let x = 0; x < W; x++) { | |
| 105 | if (at(s, x, y) === 'W' && NEAR.some(([dx, dy]) => inside(x + dx, y + dy) && at(s, x + dx, y + dy) === '~')) touching.push([x, y, noise(s.seed, 100 * s.tide + y * W + x)]); | |
| 106 | } | |
| 107 | touching.sort((a, b) => a[2] - b[2]); | |
| 108 | for (const [x, y] of touching.slice(0, Math.ceil(touching.length * WASH))) set(s, x, y, '.'); | |
| 109 | if (s.tide >= TIDES) s.status = 'won'; | |
| 110 | } | |
| 111 | s.placed = 0; | |
| 112 | return { ok: true, why: '', state: refresh(s) }; | |
| 113 | } | |
| 114 | ||
| 115 | const api = { W, H, CASTLE, TIDES, BUDGET, REACH, WASH, ROCKS, beach, newRound, place, flood }; | |
| 116 | if (typeof module !== 'undefined') module.exports = api; else root.Sandcastle = api; | |
| 117 | })(typeof window !== 'undefined' ? window : globalThis); |
sandcastle/tide.test.mjs added +112
| @@ -0,0 +1,112 @@ | ||
| 1 | import { test, expect } from 'bun:test'; | |
| 2 | import { W, H, CASTLE, TIDES, BUDGET, REACH, WASH, ROCKS, beach, newRound, place, flood } from './tide.js'; | |
| 3 | ||
| 4 | const cell = (s, x, y) => s.grid[y][x]; | |
| 5 | // a round on a bare beach (no rocks), so the tests control every square | |
| 6 | const bare = () => Object.assign(newRound(1), { grid: Array.from({ length: H }, (_, y) => (y === H - 1 ? '~' : '.').repeat(W)) }); | |
| 7 | const withCastle = s => { s.grid[CASTLE.y] = s.grid[CASTLE.y].slice(0, CASTLE.x) + 'C' + s.grid[CASTLE.y].slice(CASTLE.x + 1); return s; }; | |
| 8 | const fresh = () => withCastle(bare()); | |
| 9 | const build = (s, cells) => cells.reduce((st, [x, y]) => { const r = place(st, x, y); expect([x, y, r.why]).toEqual([x, y, '']); return r.state; }, s); | |
| 10 | const ring = [[CASTLE.x - 1, 0], [CASTLE.x + 1, 0], [CASTLE.x, 1]]; | |
| 11 | const rows = s => s.grid.map(r => r.split('')); | |
| 12 | ||
| 13 | test('the same seed gives the same beach, other seeds differ, and the castle and sea stay put', () => { | |
| 14 | expect(beach(7)).toEqual(beach(7)); | |
| 15 | expect(newRound(7).grid).toEqual(newRound(7).grid); | |
| 16 | expect(beach(7)).not.toEqual(beach(8)); | |
| 17 | for (const seed of [1, 2, 3, 99]) { | |
| 18 | const g = beach(seed); | |
| 19 | expect(g).toHaveLength(H); | |
| 20 | expect(g[CASTLE.y][CASTLE.x]).toBe('C'); | |
| 21 | expect(g[H - 1]).toBe('~'.repeat(W)); | |
| 22 | expect(g.join('').split('#').length - 1).toBe(ROCKS); | |
| 23 | } | |
| 24 | }); | |
| 25 | ||
| 26 | test('a wall goes on empty sand and spends the budget', () => { | |
| 27 | const r = place(fresh(), 2, 4); | |
| 28 | expect(r.ok).toBe(true); | |
| 29 | expect(cell(r.state, 2, 4)).toBe('W'); | |
| 30 | expect(r.state.left).toBe(BUDGET - 1); | |
| 31 | }); | |
| 32 | ||
| 33 | test('placement is refused on rocks, the castle, wet squares, walls, off the grid and over budget', () => { | |
| 34 | const s = fresh(); | |
| 35 | s.grid[3] = '#' + s.grid[3].slice(1); | |
| 36 | expect(place(s, 0, 3).why).toBe('rock'); | |
| 37 | expect(place(s, CASTLE.x, CASTLE.y).why).toBe('castle'); | |
| 38 | expect(place(s, 0, H - 1).why).toBe('wet'); | |
| 39 | expect(place(s, -1, 0).why).toBe('bounds'); | |
| 40 | expect(place(s, W, 0).why).toBe('bounds'); | |
| 41 | expect(place(s, 1.5, 2).why).toBe('bounds'); | |
| 42 | const w = place(s, 1, 1).state; | |
| 43 | expect(place(w, 1, 1).why).toBe('wall'); | |
| 44 | expect(place(s, 0, 3).state).toBe(s); | |
outdated
Redundant: each of these refusals is already asserted with its reason above. Remove. |
||
| 45 | const full = build(s, [[0, 1], [1, 1], [2, 1], [6, 1]]); | |
| 46 | const over = place(full, 7, 1); | |
| 47 | expect([over.ok, over.why, over.state]).toEqual([false, 'budget', full]); | |
| 48 | }); | |
| 49 | ||
| 50 | test('water spreads from the sea up to the tide reach', () => { | |
| 51 | const r = flood(fresh()); | |
| 52 | expect(r.ok).toBe(true); | |
| 53 | expect(cell(r.state, 3, H - 1 - REACH[0])).toBe('~'); | |
| 54 | expect(cell(r.state, 3, H - 2 - REACH[0])).toBe('.'); | |
| 55 | expect(r.state.tide).toBe(1); | |
| 56 | }); | |
| 57 | ||
| 58 | test('water goes around a wall but not through it', () => { | |
| 59 | let s = fresh(); | |
| 60 | s = build(s, [[4, 7], [3, 7], [5, 7]]); // a short wall across the beach | |
| 61 | s.tide = 2; // reach 5, enough to get round it | |
| 62 | const f = flood(s).state; | |
| 63 | expect(cell(f, 4, 6)).toBe('~'); // reached round the ends | |
| 64 | expect(cell(f, 2, 7)).toBe('~'); | |
| 65 | const t = fresh(); | |
| 66 | const wide = rows(t); for (let x = 0; x < W; x++) wide[7][x] = 'W'; | |
outdated
Dead setup: |
||
| 67 | t.grid = wide.map(r => r.join('')); | |
| 68 | const g = flood(t).state; | |
| 69 | for (let x = 0; x < W; x++) expect(cell(g, x, 6)).toBe('.'); // sealed row: nothing behind it | |
| 70 | }); | |
| 71 | ||
| 72 | test('walls wash away by the stated share of those touching water, and others stay', () => { | |
| 73 | const s = build(fresh(), [[0, 7], [2, 7], [4, 7], [6, 1]]); | |
| 74 | const f = flood(s).state; | |
| 75 | const left = [[0, 7], [2, 7], [4, 7]].filter(([x, y]) => cell(f, x, y) === 'W').length; | |
| 76 | expect(left).toBe(3 - Math.ceil(3 * WASH)); | |
| 77 | expect(cell(f, 6, 1)).toBe('W'); // far from the water | |
| 78 | expect(flood(s).state).toEqual(f); // deterministic | |
| 79 | }); | |
| 80 | ||
| 81 | test('a sealed ring keeps the castle dry through every tide and wins with the dry squares as score', () => { | |
| 82 | let s = fresh(); | |
| 83 | for (let t = 0; t < TIDES; t++) { | |
| 84 | expect(s.status).toBe('playing'); | |
| 85 | s = build(s, ring.filter(([x, y]) => cell(s, x, y) !== 'W')); // mend the ring each tide: washed walls are rebuilt | |
| 86 | s = flood(s).state; | |
| 87 | } | |
| 88 | expect(s.status).toBe('won'); | |
| 89 | expect(cell(s, CASTLE.x, CASTLE.y)).toBe('C'); | |
| 90 | expect(s.dry).toBeGreaterThan(0); | |
| 91 | expect(s.line).toBe(`The castle stayed dry through ${TIDES} tides, ${s.dry} dry squares left`); | |
| 92 | expect(flood(s).why).toBe('over'); | |
| 93 | expect(place(s, 0, 2).why).toBe('over'); | |
| 94 | }); | |
| 95 | ||
| 96 | test('water reaching the castle loses and names the tide', () => { | |
| 97 | let s = fresh(); | |
| 98 | let tides = 0; | |
| 99 | while (s.status === 'playing') { s = flood(s).state; tides++; } | |
| 100 | expect(s.status).toBe('lost'); | |
| 101 | expect(s.tide).toBe(tides); | |
| 102 | expect(s.line).toBe(`The sea reached the castle in tide ${s.tide}`); | |
| 103 | expect(tides).toBe(REACH.findIndex(r => r >= H - 1) + 1); | |
| 104 | }); | |
| 105 | ||
| 106 | test('the budget returns each tide', () => { | |
| 107 | let s = build(fresh(), [[0, 1], [1, 1], [2, 1], [6, 1]]); | |
| 108 | expect(s.left).toBe(0); | |
| 109 | s = flood(s).state; | |
| 110 | expect(s.left).toBe(BUDGET); | |
| 111 | expect(place(s, 7, 1).ok).toBe(true); | |
| 112 | }); |
new above
offline
Use the exported ROCKS instead of the literal 10, so the test follows the rule.