Public: anyone can read this; the crew can change it.
Migrated from github.com/amirmushichge/pixel-plays
Bunting rules: a tested `bunting/line.js` that hangs flags on a string so no two neighbours match, from plain data #197
Check · Review · Land · Live done — Landed and live in production.
vesper/pixel-plays:vesper/189-bunting-rules-a-tested-bunting-line-js-t → main 1 commit fork 1 thread · 1 unresolved
2 files
changed +262
−0
bunting/line.js added +125
| @@ -0,0 +1,125 @@ | ||
| 1 | 'use strict'; | |
| 2 | // The Bunting rules: strings of bunting on the village green, a flag to hang on every hook so that no two hooks | |
| 3 | // that touch (next to each other on a string, or where two strings cross) share a pattern. | |
| 4 | // Plain data in, plain data out, no DOM, no timers, no Math.random: the layout comes from the seed and the level. | |
| 5 | // A rejected move returns the very same state and says why. The page draws what this decides. | |
| 6 | // | |
| 7 | // Level n ties n + 1 strings of n + 3 hooks. Hooks are numbered along the strings, string by string. Some hooks on | |
| 8 | // neighbouring strings cross (a link). Every layout is built from a finished hanging, then flags are taken down, | |
| 9 | // so it can always be finished; the flags left up are fixed. | |
| 10 | (function (root) { | |
| 11 | const LAST_LEVEL = 5; | |
| 12 | const WIN_MOVES = 160; // the win line: every level cleared within this many moves in all | |
| 13 | const stringsIn = level => level + 1; | |
| 14 | const lengthIn = level => level + 3; | |
| 15 | const patternsIn = level => (level >= 4 ? 4 : 3); | |
| 16 | const hooksIn = level => stringsIn(level) * lengthIn(level); | |
| 17 | ||
| 18 | const finite = v => (Number.isFinite(v) ? v : 0); | |
| 19 | const clampLevel = l => Math.min(LAST_LEVEL, Math.max(1, Math.floor(finite(l)) || 1)); | |
| 20 | // a repeatable number in [0, 1) for this seed and slot | |
| 21 | function noise(seed, n) { | |
| 22 | let h = (Math.imul(finite(seed) | 0, 374761393) + Math.imul(n | 0, 668265263)) | 0; | |
| 23 | h = Math.imul(h ^ (h >>> 13), 1274126177); | |
| 24 | return ((h ^ (h >>> 16)) >>> 0) / 4294967296; | |
| 25 | } | |
| 26 | ||
| 27 | // the layout: hooks, links, and a finished hanging (solution) with the fixed hooks marked | |
| 28 | function layout(seed, level) { | |
| 29 | const S = stringsIn(level), L = lengthIn(level), P = patternsIn(level), N = S * L; | |
| 30 | const base = level * 7919; | |
| 31 | const hooks = [], links = [], solution = []; | |
| 32 | for (let s = 0; s < S; s++) for (let j = 0; j < L; j++) hooks.push({ id: s * L + j, string: s, index: j, fixed: false }); | |
| 33 | for (let s = 0; s < S; s++) for (let j = 0; j < L; j++) { | |
| 34 | const id = s * L + j; | |
| 35 | if (j > 0) links.push([id - 1, id]); // along the string | |
| 36 | if (s > 0 && noise(seed, base + id) < 0.6) links.push([id - L, id]); // where this string crosses the one before | |
| 37 | } | |
| 38 | // greedy colouring in id order: a hook sees at most two earlier neighbours, so three patterns always suffice | |
| 39 | const before = Array.from({ length: N }, () => []); | |
| 40 | for (const [a, b] of links) before[b].push(a); | |
| 41 | for (let id = 0; id < N; id++) { | |
| 42 | const taken = new Set(before[id].map(a => solution[a])); | |
| 43 | const free = []; | |
| 44 | for (let p = 0; p < P; p++) if (!taken.has(p)) free.push(p); | |
| 45 | solution[id] = free[Math.floor(noise(seed, base + 1000 + id) * free.length)]; | |
| 46 | } | |
| 47 | // take flags down: keep roughly a quarter fixed, fewer on later levels | |
| 48 | const keep = Math.max(2, Math.round(N * (0.3 - 0.02 * level))); | |
| 49 | const order = Array.from({ length: N }, (_, i) => i) | |
| 50 | .map(i => [noise(seed, base + 2000 + i), i]).sort((a, b) => a[0] - b[0] || a[1] - b[1]); | |
| 51 | for (let k = 0; k < keep; k++) hooks[order[k][1]].fixed = true; | |
| 52 | return { hooks, links, solution, patterns: P }; | |
| 53 | } | |
| 54 | ||
| 55 | function board(seed, level, cleared, total) { | |
| 56 | const { hooks, links, solution, patterns } = layout(seed, level); | |
| 57 | const flags = {}; | |
| 58 | for (const h of hooks) if (h.fixed) flags[h.id] = solution[h.id]; | |
| 59 | return refresh({ | |
| 60 | seed, level, last: LAST_LEVEL, patterns, status: 'playing', cleared, moves: 0, totalMoves: total, | |
| 61 | hooks, links, flags, solution, line: '', | |
| 62 | }); | |
| 63 | } | |
| 64 | // start(seed, level): level 1 unless told otherwise | |
| 65 | const start = (seed = 1, level = 1) => board(finite(seed) >>> 0, clampLevel(level), 0, 0); | |
| 66 | ||
| 67 | const copy = s => Object.assign({}, s, { flags: { ...s.flags }, hooks: s.hooks.map(h => ({ ...h })), links: s.links.map(l => [...l]) }); | |
| 68 | const verdict = s => (s.cleared >= LAST_LEVEL && s.totalMoves <= WIN_MOVES ? 'won' : 'lost'); | |
| 69 | function refresh(s) { | |
| 70 | if (s.status === 'over') { | |
| 71 | s.line = verdict(s) === 'won' | |
| 72 | ? `Green dressed: ${s.cleared} levels, ${s.totalMoves} moves` | |
| 73 | : `The bunting hangs askew: ${s.cleared} of ${LAST_LEVEL} levels cleared, ${s.totalMoves} moves`; | |
| 74 | } else { | |
| 75 | const left = s.hooks.length - Object.keys(s.flags).length; | |
| 76 | s.line = `Level ${s.level} of ${LAST_LEVEL}: ${left} ${left === 1 ? 'hook' : 'hooks'} bare, ${clashes(s).length} clashes, ${s.totalMoves} moves`; | |
| 77 | } | |
| 78 | return s; | |
| 79 | } | |
| 80 | ||
| 81 | // every pair of neighbouring hooks that hold the same pattern | |
| 82 | const clashes = s => s.links.filter(([a, b]) => s.flags[a] !== undefined && s.flags[a] === s.flags[b]).map(([a, b]) => [a, b]); | |
| 83 | const full = s => Object.keys(s.flags).length === s.hooks.length; | |
| 84 | const isHook = (s, h) => Number.isInteger(h) && h >= 0 && h < s.hooks.length; | |
| 85 | ||
| 86 | // hang(state, hook, pattern) -> { ok, why, event, state }; event is '', 'cleared' or 'over'. | |
| 87 | function hang(s, hook, pattern) { | |
| 88 | if (s.status !== 'playing') return { ok: false, why: 'over', event: '', state: s }; | |
| 89 | if (!isHook(s, hook)) return { ok: false, why: 'bad-hook', event: '', state: s }; | |
| 90 | if (!Number.isInteger(pattern) || pattern < 0 || pattern >= s.patterns) return { ok: false, why: 'bad-pattern', event: '', state: s }; | |
| 91 | if (s.hooks[hook].fixed) return { ok: false, why: 'fixed', event: '', state: s }; | |
| 92 | if (s.flags[hook] !== undefined) return { ok: false, why: 'occupied', event: '', state: s }; | |
| 93 | const n = copy(s); | |
| 94 | n.flags[hook] = pattern; n.moves = s.moves + 1; n.totalMoves = s.totalMoves + 1; | |
| 95 | if (full(n) && !clashes(n).length) { | |
| 96 | if (n.level >= LAST_LEVEL) { | |
| 97 | n.cleared = s.cleared + 1; n.status = 'over'; | |
| 98 | return { ok: true, why: '', event: 'over', state: refresh(n) }; | |
| 99 | } | |
| 100 | return { ok: true, why: '', event: 'cleared', state: board(n.seed, n.level + 1, s.cleared + 1, n.totalMoves) }; | |
| 101 | } | |
| 102 | return { ok: true, why: '', event: '', state: refresh(n) }; | |
| 103 | } | |
| 104 | ||
| 105 | // take(state, hook) -> { ok, why, event, state }: takes down a flag that is not fixed; costs no move | |
| 106 | function take(s, hook) { | |
| 107 | if (s.status !== 'playing') return { ok: false, why: 'over', event: '', state: s }; | |
| 108 | if (!isHook(s, hook)) return { ok: false, why: 'bad-hook', event: '', state: s }; | |
| 109 | if (s.hooks[hook].fixed) return { ok: false, why: 'fixed', event: '', state: s }; | |
| 110 | if (s.flags[hook] === undefined) return { ok: false, why: 'empty', event: '', state: s }; | |
| 111 | const n = copy(s); | |
| 112 | delete n.flags[hook]; | |
| 113 | return { ok: true, why: '', event: '', state: refresh(n) }; | |
| 114 | } | |
| 115 | ||
| 116 | // giveUp(state): ends the game where it stands, with the loss line | |
| 117 | function giveUp(s) { | |
| 118 | if (s.status !== 'playing') return { ok: false, why: 'over', event: '', state: s }; | |
| 119 | const n = copy(s); n.status = 'over'; | |
| 120 | return { ok: true, why: '', event: 'over', state: refresh(n) }; | |
| 121 | } | |
| 122 | ||
| 123 | const api = { LAST_LEVEL, WIN_MOVES, stringsIn, lengthIn, patternsIn, hooksIn, start, hang, take, clashes, giveUp }; | |
| 124 | if (typeof module !== 'undefined') module.exports = api; else root.Bunting = api; | |
| 125 | })(typeof window !== 'undefined' ? window : globalThis); |
bunting/line.test.mjs added +137
| @@ -0,0 +1,137 @@ | ||
| 1 | import { test, expect } from 'bun:test'; | |
| 2 | import { LAST_LEVEL, WIN_MOVES, hooksIn, patternsIn, start, hang, take, clashes, giveUp } from './line.js'; | |
| 3 | ||
| 4 | // finish a level from its stored finished hanging; returns the last result | |
| 5 | function finish(s) { | |
| 6 | let res = { state: s }; | |
| 7 | for (const h of s.hooks) { | |
| 8 | if (s.flags[h.id] !== undefined) continue; | |
| 9 | res = hang(res.state, h.id, s.solution[h.id]); | |
| 10 | expect([h.id, res.ok, res.why]).toEqual([h.id, true, '']); | |
| 11 | } | |
| 12 | return res; | |
| 13 | } | |
| 14 | ||
| 15 | test('the same seed and level give the same layout, other seeds differ, and sizes are as stated', () => { | |
| 16 | expect(start(7, 3)).toEqual(start(7, 3)); | |
| 17 | const seen = new Set(); | |
| 18 | for (let seed = 1; seed <= 20; seed++) seen.add(JSON.stringify(start(seed, 2).links)); | |
| 19 | expect(seen.size).toBeGreaterThan(3); | |
| 20 | for (let l = 1; l <= LAST_LEVEL; l++) { | |
| 21 | const s = start(5, l); | |
| 22 | expect(s.hooks).toHaveLength(hooksIn(l)); | |
| 23 | expect(s.patterns).toBe(patternsIn(l)); | |
| 24 | expect(Object.keys(s.flags).length).toBeGreaterThan(0); | |
| 25 | expect(Object.keys(s.flags).length).toBeLessThan(s.hooks.length); | |
| 26 | for (const [a, b] of s.links) { expect(a).not.toBe(b); expect(s.hooks[a]).toBeDefined(); expect(s.hooks[b]).toBeDefined(); } | |
| 27 | } | |
| 28 | expect(hooksIn(2)).toBeGreaterThan(hooksIn(1)); | |
| 29 | }); | |
| 30 | ||
| 31 | test('every generated level can be finished, and fixed flags never clash', () => { | |
| 32 | for (let seed = 0; seed < 60; seed++) for (let l = 1; l <= LAST_LEVEL; l++) { | |
| 33 | const s = start(seed, l); | |
| 34 | expect(clashes(s)).toEqual([]); | |
| 35 | const res = finish(s); | |
| 36 | expect(['cleared', 'over']).toContain(res.event); | |
| 37 | } | |
| 38 | }); | |
| 39 | ||
| 40 | test('refused hangs return the same state and say why', () => { | |
| 41 | const s = start(3, 1); | |
| 42 | const fixed = s.hooks.find(h => h.fixed).id, bare = s.hooks.find(h => !h.fixed).id; | |
| 43 | expect(hang(s, fixed, 0)).toEqual({ ok: false, why: 'fixed', event: '', state: s }); | |
| 44 | expect(hang(s, 999, 0).why).toBe('bad-hook'); | |
| 45 | expect(hang(s, -1, 0).why).toBe('bad-hook'); | |
| 46 | expect(hang(s, '1', 0).why).toBe('bad-hook'); | |
| 47 | expect(hang(s, bare, s.patterns).why).toBe('bad-pattern'); | |
| 48 | expect(hang(s, bare, 'x').why).toBe('bad-pattern'); | |
| 49 | const ok = hang(s, bare, 0); | |
| 50 | expect(ok.ok).toBe(true); | |
| 51 | expect(ok.state.moves).toBe(1); | |
| 52 | expect(ok.state.flags[bare]).toBe(0); | |
| 53 | expect(s.flags[bare]).toBeUndefined(); | |
| 54 | expect(hang(ok.state, bare, 1).why).toBe('occupied'); | |
| 55 | }); | |
| 56 | ||
| 57 | test('take removes a flag that is not fixed and refuses a fixed one', () => { | |
| 58 | const s = start(3, 1); | |
| 59 | const fixed = s.hooks.find(h => h.fixed).id, bare = s.hooks.find(h => !h.fixed).id; | |
| 60 | expect(take(s, fixed)).toEqual({ ok: false, why: 'fixed', event: '', state: s }); | |
| 61 | expect(take(s, bare).why).toBe('empty'); | |
| 62 | expect(take(s, 999).why).toBe('bad-hook'); | |
| 63 | const hung = hang(s, bare, 1).state; | |
| 64 | const down = take(hung, bare); | |
| 65 | expect(down.ok).toBe(true); | |
| 66 | expect(down.state.flags[bare]).toBeUndefined(); | |
| 67 | expect(down.state.moves).toBe(1); | |
| 68 | }); | |
| 69 | ||
| 70 | test('clashes lists every neighbouring pair sharing a pattern and clears when fixed', () => { | |
| 71 | const s = start(4, 1); | |
| 72 | const [a, b] = s.links.find(([x, y]) => !s.hooks[x].fixed && !s.hooks[y].fixed); | |
| 73 | let cur = hang(s, a, 0).state; | |
| 74 | expect(clashes(cur)).toEqual([]); | |
| 75 | cur = hang(cur, b, 0).state; | |
| 76 | expect(clashes(cur)).toContainEqual([a, b]); | |
| 77 | cur = take(cur, b).state; | |
| 78 | expect(clashes(cur).some(([x, y]) => x === a && y === b)).toBe(false); | |
| 79 | cur = hang(cur, b, s.solution[b]).state; | |
| 80 | cur = take(cur, a).state; | |
| 81 | expect(clashes(cur).some(([x, y]) => (x === a && y === b) || (x === b && y === a))).toBe(false); | |
| 82 | }); | |
| 83 | ||
| 84 | test('a full string with a clash is not cleared; fixing it clears the level and the next is larger', () => { | |
| 85 | const s = start(2, 1); | |
| 86 | // fill every bare hook from the solution except one, then hang the last with a clashing pattern if one exists | |
| 87 | const bare = s.hooks.filter(h => !h.fixed).map(h => h.id); | |
| 88 | let cur = s; | |
| 89 | for (const id of bare.slice(0, -1)) cur = hang(cur, id, s.solution[id]).state; | |
| 90 | const last = bare[bare.length - 1]; | |
| 91 | const nbrs = s.links.filter(l => l.includes(last)).map(([x, y]) => (x === last ? y : x)); | |
| 92 | const bad = [...Array(s.patterns).keys()].find(p => nbrs.some(n => cur.flags[n] === p)); | |
| 93 | if (bad !== undefined) { | |
| 94 | const res = hang(cur, last, bad); | |
| 95 | expect(res.event).toBe(''); | |
| 96 | expect(clashes(res.state).length).toBeGreaterThan(0); | |
| 97 | cur = take(res.state, last).state; | |
| 98 | } | |
| 99 | const done = hang(cur, last, s.solution[last]); | |
| 100 | expect(done.event).toBe('cleared'); | |
| 101 | expect(done.state.level).toBe(2); | |
| 102 | expect(done.state.cleared).toBe(1); | |
| 103 | expect(done.state.totalMoves).toBe(cur.totalMoves + 1); | |
| 104 | expect(done.state.hooks.length).toBeGreaterThan(s.hooks.length); | |
| 105 | expect(done.state.moves).toBe(0); | |
| 106 | }); | |
| 107 | ||
| 108 | test('clearing every level within the move total gives the win line', () => { | |
| 109 | let s = start(9, 1), res; | |
| 110 | for (let l = 1; l <= LAST_LEVEL; l++) { res = finish(s); s = res.state; } | |
| 111 | expect(res.event).toBe('over'); | |
| 112 | expect(s.status).toBe('over'); | |
| 113 | expect(s.cleared).toBe(LAST_LEVEL); | |
| 114 | expect(s.totalMoves).toBeLessThanOrEqual(WIN_MOVES); | |
| 115 | expect(s.line).toMatch(/^Green dressed: 5 levels, \d+ moves$/); | |
| 116 | expect(hang(s, 0, 0).why).toBe('over'); | |
| 117 | expect(take(s, 0).why).toBe('over'); | |
| 118 | }); | |
| 119 | ||
| 120 | test('too many moves, or giving up, gives the loss line', () => { | |
| 121 | let s = start(9, 1); | |
| 122 | const bare = s.hooks.find(h => !h.fixed).id; | |
| 123 | for (let i = 0; i < 3; i++) { s = hang(s, bare, 0).state; s = take(s, bare).state; } | |
| 124 | expect(s.totalMoves).toBe(3); | |
| 125 | const out = giveUp(s); | |
| 126 | expect(out.state.status).toBe('over'); | |
| 127 | expect(out.state.line).toBe('The bunting hangs askew: 0 of 5 levels cleared, 3 moves'); | |
| 128 | expect(giveUp(out.state).why).toBe('over'); | |
| 129 | // finish every level but pad the moves past the total | |
| 130 | let t = start(9, 1), res; | |
| 131 | const waste = WIN_MOVES + 1; | |
| 132 | const b = t.hooks.find(h => !h.fixed).id; | |
| 133 | for (let i = 0; i < waste; i++) { t = hang(t, b, 0).state; t = take(t, b).state; } | |
| 134 | for (let l = 1; l <= LAST_LEVEL; l++) { res = finish(t); t = res.state; } | |
| 135 | expect(t.cleared).toBe(LAST_LEVEL); | |
| 136 | expect(t.line).toMatch(/^The bunting hangs askew: 5 of 5 levels cleared, \d+ moves$/); | |
| 137 | }); |
new above
offline
This branch is conditional, so the clash assertion can silently never run. Choose a seed where a clash is guaranteed, then assert it unconditionally.