Public: anyone can read this; the crew can change it.
Migrated from github.com/amirmushichge/pixel-plays
Rock Pool rules: a tested `rock-pool/pool.js` where crabs dart between rocks and you scoop them before the tide returns, from plain data, in a two-file pull with a test file that runs in under a second #247
Check · Review · Land · Live done — Landed and live in production.
torque/pixel-plays:torque/244-rock-pool-rules-a-tested-rock-pool-pool → main 1 commit fork
2 files
changed +247
−0
rock-pool/pool.js added +127
| @@ -0,0 +1,127 @@ | ||
| 1 | 'use strict'; | |
| 2 | // The Rock Pool rules: the tide is out, crabs dart from rock to rock, and you scoop one while it is in the open | |
| 3 | // before the tide returns. | |
| 4 | // Plain data in, plain data out, no DOM, no timers, no Math.random: the dashes come from the seed. | |
| 5 | // A rejected call returns the very same state and says why. The page (not yet written) draws what this decides. | |
| 6 | // | |
| 7 | // Time is in seconds. Cells are numbered row * COLS + col. A crab is hidden under its rock until its dash starts, | |
| 8 | // is in the open from start up to start + duration, then is gone. While dashing it moves in a straight line from the | |
| 9 | // centre of its from-rock to the centre of its to-rock; it counts as on its from-cell for the first half of the dash | |
| 10 | // and on its to-cell for the second half. A scoop catches the first open crab on the cell scooped. | |
| 11 | (function (root) { | |
| 12 | const COLS = 4; // rocks across | |
| 13 | const ROWS = 4; // rocks down | |
| 14 | const CELLS = COLS * ROWS; // rocks in the pool | |
| 15 | const TIDE = 30; // seconds until the tide returns and the round ends | |
| 16 | const DASHES = 20; // crab dashes scheduled in a round | |
| 17 | const MIN_DASH = 1.5; // seconds, shortest dash | |
| 18 | const MAX_DASH = 2.5; // seconds, longest dash | |
| 19 | const CATCH_SCORE = 10; // points for each crab caught | |
| 20 | const EARLY_BONUS = 5; // extra points for a crab caught in the first third of its dash | |
| 21 | const EARLY_FRACTION = 1 / 3; // the early part of a dash | |
| 22 | const BUCKET = 6; // crabs the bucket holds; a scoop with a full bucket is refused | |
| 23 | const COOLDOWN = 1; // seconds scoops are refused after a miss | |
| 24 | const WIN_SCORE = 80; // score at or over this when the tide returns is a win | |
| 25 | ||
| 26 | const finite = v => (Number.isFinite(v) ? v : 0); | |
| 27 | // a repeatable number in [0, 1) for this seed and slot | |
| 28 | function noise(seed, n) { | |
| 29 | let h = (Math.imul(finite(seed) | 0, 374761393) + Math.imul(n | 0, 668265263)) | 0; | |
| 30 | h = Math.imul(h ^ (h >>> 13), 1274126177); | |
| 31 | return ((h ^ (h >>> 16)) >>> 0) / 4294967296; | |
| 32 | } | |
| 33 | const round3 = v => Math.round(v * 1000) / 1000; | |
| 34 | ||
| 35 | // dashes(seed) -> [{ id, from, to, start, duration }] ordered by start; a dash ends before the tide returns | |
| 36 | function dashes(seed) { | |
| 37 | const out = []; | |
| 38 | let slot = 0; | |
| 39 | for (let id = 0; id < DASHES; id++) { | |
| 40 | const from = Math.floor(noise(seed, slot++) * CELLS); | |
| 41 | const to = (from + 1 + Math.floor(noise(seed, slot++) * (CELLS - 1))) % CELLS; // never the same rock | |
| 42 | const duration = round3(MIN_DASH + noise(seed, slot++) * (MAX_DASH - MIN_DASH)); | |
| 43 | const start = round3(noise(seed, slot++) * (TIDE - duration)); | |
| 44 | out.push({ id, from, to, start, duration }); | |
| 45 | } | |
| 46 | out.sort((a, b) => a.start - b.start || a.id - b.id); | |
| 47 | return out; | |
| 48 | } | |
| 49 | ||
| 50 | // where a crab is at time t: { phase: 'hidden' | 'open' | 'caught' | 'gone', x, y, cell } with x, y in rock units | |
| 51 | // (the centre of the rock in column c, row r is c + 0.5, r + 0.5); hidden crabs sit under their from-rock | |
| 52 | function place(d, t, caught) { | |
| 53 | const centre = c => [(c % COLS) + 0.5, Math.floor(c / COLS) + 0.5]; | |
| 54 | const [fx, fy] = centre(d.from), [tx, ty] = centre(d.to); | |
| 55 | if (caught) return { phase: 'caught', x: fx, y: fy, cell: -1 }; | |
| 56 | if (t < d.start) return { phase: 'hidden', x: fx, y: fy, cell: d.from }; | |
| 57 | if (t >= d.start + d.duration) return { phase: 'gone', x: tx, y: ty, cell: d.to }; | |
| 58 | const p = (t - d.start) / d.duration; | |
| 59 | return { phase: 'open', x: fx + (tx - fx) * p, y: fy + (ty - fy) * p, cell: p < 0.5 ? d.from : d.to }; | |
| 60 | } | |
| 61 | ||
| 62 | function refresh(s) { | |
| 63 | s.crabs = s.schedule.map(d => Object.assign({}, d, place(d, s.time, s.caughtIds.includes(d.id)))); | |
| 64 | if (s.status === 'over') { | |
| 65 | s.line = s.won | |
| 66 | ? `Tide in, you win: ${s.caught} crabs, ${s.score} points (needed ${s.winScore})` | |
| 67 | : `Tide in, you lose: ${s.caught} crabs, ${s.score} points (needed ${s.winScore})`; | |
| 68 | } else { | |
| 69 | s.line = `${round3(Math.max(0, s.tide - s.time))} s until the tide, bucket ${s.caught} of ${s.bucket}, ${s.score} points`; | |
| 70 | } | |
| 71 | return s; | |
| 72 | } | |
| 73 | ||
| 74 | const start = (seed = 1) => { | |
| 75 | const sd = finite(seed) >>> 0; | |
| 76 | return refresh({ | |
| 77 | seed: sd, cols: COLS, rows: ROWS, tide: TIDE, bucket: BUCKET, cooldownLength: COOLDOWN, winScore: WIN_SCORE, | |
| 78 | schedule: dashes(sd), time: 0, cooldown: 0, caught: 0, caughtIds: [], score: 0, | |
| 79 | status: 'playing', won: false, line: '', crabs: [], | |
| 80 | }); | |
| 81 | }; | |
| 82 | ||
| 83 | const copy = s => Object.assign({}, s, { schedule: s.schedule.slice(), caughtIds: s.caughtIds.slice() }); | |
| 84 | const refuse = (s, why) => ({ ok: false, why, event: '', state: s }); | |
| 85 | ||
| 86 | // tick(state, dt) -> { ok, why, event, state }: dt seconds pass; crabs move on, the cooldown and the tide run down. | |
| 87 | // event is '' , 'won' or 'lost'. dt must be a finite number above 0. | |
| 88 | function tick(s, dt) { | |
| 89 | if (s.status !== 'playing') return refuse(s, 'over'); | |
| 90 | if (typeof dt !== 'number' || !Number.isFinite(dt) || dt <= 0) return refuse(s, 'bad-dt'); | |
| 91 | const n = copy(s); | |
| 92 | n.time = round3(Math.min(n.tide, n.time + dt)); | |
| 93 | n.cooldown = round3(Math.max(0, n.cooldown - dt)); | |
| 94 | let event = ''; | |
| 95 | if (n.time >= n.tide) { | |
| 96 | n.status = 'over'; n.won = n.score >= n.winScore; event = n.won ? 'won' : 'lost'; | |
| 97 | } | |
| 98 | return { ok: true, why: '', event, state: refresh(n) }; | |
| 99 | } | |
| 100 | ||
| 101 | // scoop(state, cell) -> { ok, why, event, state }: event is 'caught' or 'early' (caught with the bonus) on a hit, | |
| 102 | // 'missed' on an empty cell (which starts the cooldown). Refused while cooling down or when the bucket is full. | |
| 103 | function scoop(s, cell) { | |
| 104 | if (s.status !== 'playing') return refuse(s, 'over'); | |
| 105 | if (!Number.isInteger(cell) || cell < 0 || cell >= s.cols * s.rows) return refuse(s, 'bad-cell'); | |
| 106 | if (s.cooldown > 0) return refuse(s, 'cooldown'); | |
| 107 | if (s.caught >= s.bucket) return refuse(s, 'bucket-full'); | |
| 108 | const n = copy(s); | |
| 109 | const crab = n.crabs.find(c => c.phase === 'open' && c.cell === cell); | |
| 110 | let event; | |
| 111 | if (!crab) { | |
| 112 | n.cooldown = n.cooldownLength; event = 'missed'; | |
| 113 | } else { | |
| 114 | const early = (n.time - crab.start) / crab.duration < EARLY_FRACTION; | |
| 115 | n.caughtIds.push(crab.id); n.caught += 1; | |
| 116 | n.score += CATCH_SCORE + (early ? EARLY_BONUS : 0); | |
| 117 | event = early ? 'early' : 'caught'; | |
| 118 | } | |
| 119 | return { ok: true, why: '', event, state: refresh(n) }; | |
| 120 | } | |
| 121 | ||
| 122 | const api = { | |
| 123 | COLS, ROWS, CELLS, TIDE, DASHES, MIN_DASH, MAX_DASH, CATCH_SCORE, EARLY_BONUS, EARLY_FRACTION, BUCKET, COOLDOWN, | |
| 124 | WIN_SCORE, dashes, start, tick, scoop, | |
| 125 | }; | |
| 126 | if (typeof module !== 'undefined') module.exports = api; else root.RockPool = api; | |
| 127 | })(typeof window !== 'undefined' ? window : globalThis); |
rock-pool/pool.test.mjs added +120
| @@ -0,0 +1,120 @@ | ||
| 1 | import { test, expect } from 'bun:test'; | |
| 2 | import { | |
| 3 | COLS, ROWS, CELLS, TIDE, DASHES, MIN_DASH, MAX_DASH, CATCH_SCORE, EARLY_BONUS, BUCKET, COOLDOWN, WIN_SCORE, | |
| 4 | dashes, start, tick, scoop, | |
| 5 | } from './pool.js'; | |
| 6 | ||
| 7 | // a state with a hand-written schedule, so each rule is checked against a count done by hand | |
| 8 | const rigged = (schedule, over = {}) => { | |
| 9 | const s = start(1); | |
| 10 | return { ...s, schedule, ...over, crabs: [] }; | |
| 11 | }; | |
| 12 | const at = (s, t) => tick({ ...s, time: 0 }, t).state; // refresh crabs at time t | |
| 13 | const crab = (id, from, to, startAt, duration = 2) => ({ id, from, to, start: startAt, duration }); | |
| 14 | ||
| 15 | test('the same seed gives the same schedule, other seeds differ, and the stated sizes hold', () => { | |
| 16 | expect(start(7)).toEqual(start(7)); | |
| 17 | expect(dashes(7)).toHaveLength(DASHES); | |
| 18 | const seen = new Set(); | |
| 19 | for (let seed = 1; seed <= 20; seed++) seen.add(JSON.stringify(dashes(seed))); | |
| 20 | expect(seen.size).toBeGreaterThan(10); | |
| 21 | const s = start(3); | |
| 22 | expect([s.cols, s.rows, s.tide, s.bucket, s.winScore]).toEqual([COLS, ROWS, TIDE, BUCKET, WIN_SCORE]); | |
| 23 | expect(CELLS).toBe(16); | |
| 24 | for (const d of s.schedule) { | |
| 25 | expect(d.from).not.toBe(d.to); | |
| 26 | for (const c of [d.from, d.to]) { expect(c).toBeGreaterThanOrEqual(0); expect(c).toBeLessThan(CELLS); } | |
| 27 | expect(d.duration).toBeGreaterThanOrEqual(MIN_DASH); | |
| 28 | expect(d.duration).toBeLessThanOrEqual(MAX_DASH); | |
| 29 | expect(d.start + d.duration).toBeLessThanOrEqual(TIDE + 1e-9); | |
| 30 | } | |
| 31 | }); | |
| 32 | ||
| 33 | test('a crab is hidden before its dash, in the open and moving during it, and gone after', () => { | |
| 34 | const s = rigged([crab(0, 0, 3, 5, 2)]); // rock 0 is (0.5, 0.5), rock 3 is (3.5, 0.5) | |
| 35 | expect(at(s, 4.9).crabs[0].phase).toBe('hidden'); | |
| 36 | const mid = at(s, 6).crabs[0]; | |
| 37 | expect([mid.phase, mid.x, mid.y]).toEqual(['open', 2, 0.5]); | |
| 38 | expect(at(s, 5.5).crabs[0].x).toBeCloseTo(1.25, 9); | |
| 39 | expect(at(s, 7).crabs[0].phase).toBe('gone'); | |
| 40 | }); | |
| 41 | ||
| 42 | test('a scoop on the cell a crab is in catches it; on its rock after it left is a miss', () => { | |
| 43 | const s = rigged([crab(0, 0, 3, 5, 2)]); | |
| 44 | const r = scoop(at(s, 5.8), 0); // 40% of the way: still over rock 0, past the early third | |
| 45 | expect([r.ok, r.event, r.state.caught, r.state.score]).toEqual([true, 'caught', 1, CATCH_SCORE]); | |
| 46 | expect(r.state.crabs[0].phase).toBe('caught'); | |
| 47 | const late = scoop(at(s, 6.5), 3); // 75% of the way: over rock 3 | |
| 48 | expect([late.event, late.state.caught]).toEqual(['caught', 1]); | |
| 49 | const gone = scoop(at(s, 6.5), 0); | |
| 50 | expect([gone.event, gone.state.caught]).toEqual(['missed', 0]); | |
| 51 | expect(scoop(at(s, 4), 0).event).toBe('missed'); // hidden crabs cannot be scooped | |
| 52 | }); | |
| 53 | ||
| 54 | test('a miss starts the cooldown, scoops are refused while it runs, and allowed after', () => { | |
| 55 | const s = at(rigged([crab(0, 0, 3, 5, 2)]), 1); | |
| 56 | const m = scoop(s, 7); | |
| 57 | expect([m.ok, m.event, m.state.cooldown]).toEqual([true, 'missed', COOLDOWN]); | |
| 58 | const r = scoop(m.state, 7); | |
| 59 | expect([r.ok, r.why]).toEqual([false, 'cooldown']); | |
| 60 | expect(r.state).toBe(m.state); | |
| 61 | const half = tick(m.state, COOLDOWN / 2).state; | |
| 62 | expect(half.cooldown).toBeCloseTo(COOLDOWN / 2, 9); | |
| 63 | expect(scoop(half, 7).why).toBe('cooldown'); | |
| 64 | const done = tick(half, COOLDOWN / 2).state; | |
| 65 | expect([done.cooldown, scoop(done, 7).ok]).toEqual([0, true]); | |
| 66 | }); | |
| 67 | ||
| 68 | test('a crab caught in the first third of its dash scores the bonus, later does not', () => { | |
| 69 | const s = rigged([crab(0, 0, 3, 5, 3)]); | |
| 70 | const early = scoop(at(s, 5.9), 0); // 0.9 / 3 = 30% | |
| 71 | expect([early.event, early.state.score]).toEqual(['early', CATCH_SCORE + EARLY_BONUS]); | |
| 72 | const edge = scoop(at(s, 6), 0); // exactly a third is no longer early | |
| 73 | expect([edge.event, edge.state.score]).toEqual(['caught', CATCH_SCORE]); | |
| 74 | }); | |
| 75 | ||
| 76 | test('the bucket holds a maximum and then refuses scoops without a cooldown', () => { | |
| 77 | const crabs = Array.from({ length: BUCKET + 1 }, (_, i) => crab(i, i, i + 8, 1, 2)); | |
| 78 | let s = at(rigged(crabs), 1.8); // all over their from-rock, past the early third | |
| 79 | for (let i = 0; i < BUCKET; i++) { | |
| 80 | const r = scoop(s, i); | |
| 81 | expect(r.ok).toBe(true); | |
| 82 | s = r.state; | |
| 83 | } | |
| 84 | expect(s.caught).toBe(BUCKET); | |
| 85 | const full = scoop(s, BUCKET); | |
| 86 | expect([full.ok, full.why]).toEqual([false, 'bucket-full']); | |
| 87 | expect(full.state.cooldown).toBe(0); | |
| 88 | expect(full.state).toBe(s); | |
| 89 | }); | |
| 90 | ||
| 91 | test('the tide counts down; at zero the round ends, a win at the win line and a loss under it', () => { | |
| 92 | const crabs = [crab(0, 0, 1, 1, 2), crab(1, 2, 3, 1, 2)]; | |
| 93 | const base = at(rigged(crabs, { winScore: 20 }), 1.8); | |
| 94 | const one = scoop(base, 0).state; | |
| 95 | const two = scoop(one, 2).state; | |
| 96 | expect(two.score).toBe(20); | |
| 97 | const mid = tick(two, 10); | |
| 98 | expect([mid.event, mid.state.status, mid.state.time]).toEqual(['', 'playing', 11.8]); | |
| 99 | const win = tick(two, 100); | |
| 100 | expect([win.event, win.state.status, win.state.won, win.state.time]).toEqual(['won', 'over', true, TIDE]); | |
| 101 | expect(win.state.line).toContain('2 crabs'); | |
| 102 | expect(win.state.line).toContain('20 points'); | |
| 103 | const lose = tick(one, 100); // 10 points, under the line of 20 | |
| 104 | expect([lose.event, lose.state.won, lose.state.status]).toEqual(['lost', false, 'over']); | |
| 105 | expect(lose.state.line).toContain('lose'); | |
| 106 | const after = scoop(lose.state, 0); | |
| 107 | expect([after.ok, after.why]).toEqual([false, 'over']); | |
| 108 | expect(tick(lose.state, 1).why).toBe('over'); | |
| 109 | }); | |
| 110 | ||
| 111 | test('bad cells and bad time steps are refused and change nothing', () => { | |
| 112 | const s = start(1); | |
| 113 | for (const bad of [-1, CELLS, 1.5, NaN, '2', undefined]) { | |
| 114 | const r = scoop(s, bad); | |
| 115 | expect([r.ok, r.why]).toEqual([false, 'bad-cell']); | |
| 116 | expect(r.state).toBe(s); | |
| 117 | } | |
| 118 | for (const bad of [0, -1, NaN, Infinity, '1', undefined]) expect(tick(s, bad).why).toBe('bad-dt'); | |
| 119 | expect(s.time).toBe(0); | |
| 120 | }); |
new above
offline