Public: anyone can read this; the crew can change it.
Migrated from github.com/amirmushichge/pixel-plays
Dry Stone Wall rules: a tested `stone-wall/wall.js` that lays stones in courses so no joint sits over another, from plain data #225
torque/pixel-plays:torque/204-dry-stone-wall-rules-a-tested-stone-wall → main 2 commits fork
2 files
changed +259
−0
stone-wall/wall.js added +139
| @@ -0,0 +1,139 @@ | ||
| 1 | 'use strict'; | |
| 2 | // The Dry Stone Wall rules: build a field wall course by course from a pile of stones of different widths, so that no | |
| 3 | // joint (the gap between two stones in a course) sits directly over a joint of the course below. | |
| 4 | // Plain data in, plain data out, no DOM, no timers, no Math.random: the pile comes from the seed. | |
| 5 | // A rejected move returns the very same state and says why. The page draws what this decides. | |
| 6 | // | |
| 7 | // The pile is made by cutting every course into stones whose joints avoid the joints of the course beneath, then | |
| 8 | // shuffling all the stones together, so a wall can always be finished; the player can still run out of room by | |
| 9 | // laying stones in the wrong places, and that is the loss line. | |
| 10 | (function (root) { | |
| 11 | const WALL_WIDTH = 12; // the wall is 12 stone-units wide (one unit is a hand's breadth) | |
| 12 | const COURSES = 4; // a stated number of courses to build | |
| 13 | const MIN_STONE = 2, MAX_STONE = 5; // stone widths in units | |
| 14 | const STONE_SCORE = 10; // points for each stone laid | |
| 15 | const BONUS_WIDTH = 3; // a course with no stone narrower than this earns the bonus | |
| 16 | const COURSE_BONUS = 50; // points for such a course | |
| 17 | ||
| 18 | const finite = v => (Number.isFinite(v) ? v : 0); | |
| 19 | // a repeatable number in [0, 1) for this seed and slot | |
| 20 | function noise(seed, n) { | |
| 21 | let h = (Math.imul(finite(seed) | 0, 374761393) + Math.imul(n | 0, 668265263)) | 0; | |
| 22 | h = Math.imul(h ^ (h >>> 13), 1274126177); | |
| 23 | return ((h ^ (h >>> 16)) >>> 0) / 4294967296; | |
| 24 | } | |
| 25 | ||
| 26 | // joints of a run of stone widths: the running totals short of the wall's end | |
| 27 | function jointsOf(widths) { | |
| 28 | const out = []; | |
| 29 | let at = 0; | |
| 30 | for (const w of widths) { at += w; if (at < WALL_WIDTH) out.push(at); } | |
| 31 | return out; | |
| 32 | } | |
| 33 | ||
| 34 | // cut one course: depth-first, trying widths in a seeded order, skipping any joint in `avoid` | |
| 35 | function cut(seed, salt, avoid) { | |
| 36 | const widths = []; | |
| 37 | let k = 0; | |
| 38 | function go(at) { | |
| 39 | if (at === WALL_WIDTH) return true; | |
| 40 | const order = []; | |
| 41 | for (let w = MIN_STONE; w <= MAX_STONE; w++) order.push([noise(seed, salt + 31 * (k++)), w]); | |
| 42 | order.sort((a, b) => a[0] - b[0] || a[1] - b[1]); | |
| 43 | for (const [, w] of order) { | |
| 44 | const end = at + w, rest = WALL_WIDTH - end; | |
| 45 | if (rest < 0 || rest === 1 || (end < WALL_WIDTH && avoid.has(end))) continue; | |
| 46 | widths.push(w); | |
| 47 | if (go(end)) return true; | |
| 48 | widths.pop(); | |
| 49 | } | |
| 50 | return false; | |
| 51 | } | |
| 52 | return go(0) ? widths : null; | |
| 53 | } | |
| 54 | ||
| 55 | function makePile(seed) { | |
| 56 | for (let attempt = 0; ; attempt++) { | |
| 57 | const stones = []; | |
| 58 | let avoid = new Set(), ok = true; | |
| 59 | for (let c = 0; c < COURSES && ok; c++) { | |
| 60 | const w = cut(seed, 1000 * (attempt + 1) + 100 * c, avoid); | |
| 61 | if (!w) { ok = false; break; } | |
| 62 | stones.push(...w); | |
| 63 | avoid = new Set(jointsOf(w)); | |
| 64 | } | |
| 65 | if (!ok) continue; | |
| 66 | return stones.map((w, i) => [noise(seed, 777 + i), i, w]).sort((a, b) => a[0] - b[0] || a[1] - b[1]).map(e => e[2]); | |
| 67 | } | |
| 68 | } | |
| 69 | ||
| 70 | const copy = s => Object.assign({}, s, { courses: s.courses.map(c => [...c]), current: [...s.current] }); | |
| 71 | const sum = (s, list) => list.reduce((t, i) => t + s.pile[i], 0); | |
| 72 | const used = s => new Set([...s.courses.flat(), ...s.current]); | |
| 73 | const below = s => (s.courses.length ? new Set(jointsOf(s.courses[s.courses.length - 1].map(i => s.pile[i]))) : new Set()); | |
| 74 | ||
| 75 | // why a stone cannot be laid now, or '' when it can | |
| 76 | function refusal(s, i) { | |
| 77 | if (!Number.isInteger(i) || i < 0 || i >= s.pile.length) return 'bad-stone'; | |
| 78 | if (used(s).has(i)) return 'used'; | |
| 79 | const end = sum(s, s.current) + s.pile[i]; | |
| 80 | if (end > s.width) return 'overrun'; | |
| 81 | if (end < s.width && below(s).has(end)) return 'joint'; | |
| 82 | // a gap of one unit could never be filled | |
| 83 | if (s.width - end > 0 && s.width - end < MIN_STONE) return 'gap'; | |
| 84 | return ''; | |
| 85 | } | |
| 86 | const placeable = s => s.pile.map((_, i) => i).filter(i => !refusal(s, i)); | |
| 87 | ||
| 88 | function refresh(s) { | |
| 89 | if (s.status === 'over') { | |
| 90 | s.line = s.result === 'won' | |
| 91 | ? `Wall built: ${s.courses.length} courses, ${s.score} points` | |
| 92 | : `The wall is stuck at course ${s.courses.length + 1} of ${s.total}: no stone left will fit, ${s.score} points`; | |
| 93 | } else { | |
| 94 | s.line = `Course ${s.courses.length + 1} of ${s.total}: ${sum(s, s.current)} of ${s.width} laid, ${s.score} points`; | |
| 95 | } | |
| 96 | return s; | |
| 97 | } | |
| 98 | ||
| 99 | // start(seed) -> a fresh wall with its seeded pile of stone widths | |
| 100 | const start = (seed = 1) => refresh({ | |
| 101 | seed: finite(seed) >>> 0, width: WALL_WIDTH, total: COURSES, pile: makePile(finite(seed) >>> 0), | |
| 102 | courses: [], current: [], score: 0, status: 'playing', result: '', line: '', | |
| 103 | }); | |
| 104 | ||
| 105 | // place(state, stoneIndex) -> { ok, why, event, state }; event is '', 'course', 'won' or 'lost'. | |
| 106 | function place(s, i) { | |
| 107 | if (s.status !== 'playing') return { ok: false, why: 'over', event: '', state: s }; | |
| 108 | const why = refusal(s, i); | |
| 109 | if (why) return { ok: false, why, event: '', state: s }; | |
| 110 | const n = copy(s); | |
| 111 | n.current.push(i); | |
| 112 | n.score += STONE_SCORE; | |
| 113 | let event = ''; | |
| 114 | if (sum(n, n.current) === n.width) { | |
| 115 | if (n.current.every(j => n.pile[j] >= BONUS_WIDTH)) n.score += COURSE_BONUS; | |
| 116 | n.courses.push(n.current); n.current = []; | |
| 117 | event = 'course'; | |
| 118 | if (n.courses.length === n.total) { n.status = 'over'; n.result = 'won'; event = 'won'; } | |
| 119 | } | |
| 120 | if (n.status === 'playing' && !placeable(n).length) { n.status = 'over'; n.result = 'lost'; event = 'lost'; } | |
| 121 | return { ok: true, why: '', event, state: refresh(n) }; | |
| 122 | } | |
| 123 | ||
| 124 | // undo(state): lifts the last stone of the current course, and its points | |
| 125 | function undo(s) { | |
| 126 | if (s.status !== 'playing') return { ok: false, why: 'over', event: '', state: s }; | |
| 127 | if (!s.current.length) return { ok: false, why: 'empty', event: '', state: s }; | |
| 128 | const n = copy(s); | |
| 129 | n.current.pop(); | |
| 130 | n.score -= STONE_SCORE; | |
| 131 | return { ok: true, why: '', event: '', state: refresh(n) }; | |
| 132 | } | |
| 133 | ||
| 134 | const api = { | |
| 135 | WALL_WIDTH, COURSES, MIN_STONE, MAX_STONE, STONE_SCORE, BONUS_WIDTH, COURSE_BONUS, | |
| 136 | start, place, undo, placeable, jointsOf, | |
| 137 | }; | |
| 138 | if (typeof module !== 'undefined') module.exports = api; else root.StoneWall = api; | |
| 139 | })(typeof window !== 'undefined' ? window : globalThis); |
stone-wall/wall.test.mjs added +120
| @@ -0,0 +1,120 @@ | ||
| 1 | import { test, expect } from 'bun:test'; | |
| 2 | import { | |
| 3 | WALL_WIDTH, COURSES, MIN_STONE, MAX_STONE, STONE_SCORE, BONUS_WIDTH, COURSE_BONUS, start, place, undo, placeable, jointsOf, | |
| 4 | } from './wall.js'; | |
| 5 | ||
| 6 | // a state built by hand: pile of widths, nothing laid | |
| 7 | const withPile = (pile) => ({ ...start(1), pile, courses: [], current: [], score: 0, status: 'playing', result: '' }); | |
| 8 | const idx = (s, w, skip = []) => s.pile.findIndex((x, i) => x === w && !skip.includes(i)); | |
| 9 | ||
| 10 | // play greedily with the first placeable stone, preferring the one that lets the wall be finished (depth-first) | |
| 11 | function solve(s) { | |
| 12 | if (s.status === 'over') return s.result === 'won' ? s : null; | |
| 13 | for (const i of placeable(s)) { | |
| 14 | const r = place(s, i); | |
| 15 | const out = solve(r.state); | |
| 16 | if (out) return out; | |
| 17 | } | |
| 18 | return null; | |
| 19 | } | |
| 20 | ||
| 21 | test('the same seed gives the same pile, other seeds differ, and the pile can fill every course', () => { | |
| 22 | expect(start(7)).toEqual(start(7)); | |
| 23 | const seen = new Set(); | |
| 24 | for (let seed = 1; seed <= 20; seed++) seen.add(start(seed).pile.join()); | |
| 25 | expect(seen.size).toBeGreaterThan(5); | |
| 26 | for (let seed = 0; seed < 40; seed++) { | |
| 27 | const s = start(seed); | |
| 28 | expect(s.width).toBe(WALL_WIDTH); | |
| 29 | expect(s.total).toBe(COURSES); | |
| 30 | expect(s.pile.reduce((a, b) => a + b, 0)).toBe(WALL_WIDTH * COURSES); | |
| 31 | for (const w of s.pile) { expect(w).toBeGreaterThanOrEqual(MIN_STONE); expect(w).toBeLessThanOrEqual(MAX_STONE); } | |
| 32 | const won = solve(s); | |
| 33 | expect(won).not.toBeNull(); | |
| 34 | for (let c = 1; c < won.courses.length; c++) { | |
| 35 | const lo = jointsOf(won.courses[c - 1].map(i => won.pile[i])); | |
| 36 | const hi = jointsOf(won.courses[c].map(i => won.pile[i])); | |
| 37 | expect(hi.filter(j => lo.includes(j))).toEqual([]); | |
| 38 | } | |
| 39 | } | |
| 40 | }); | |
| 41 | ||
| 42 | test('a stone that overruns the wall is refused, and the state is returned untouched', () => { | |
| 43 | const s = withPile([5, 5, 5, 2]); | |
| 44 | let r = place(s, 0); r = place(r.state, 1); | |
| 45 | const res = place(r.state, 2); | |
| 46 | expect(res).toMatchObject({ ok: false, why: 'overrun', event: '' }); | |
| 47 | expect(res.state).toBe(r.state); | |
| 48 | }); | |
| 49 | ||
| 50 | test('a stone that would put a joint over a joint below is refused', () => { | |
| 51 | const s = withPile([6, 6, 6, 3, 3, 3, 3]); | |
| 52 | let r = place(s, 0); r = place(r.state, 1); // course 1: 6 + 6, joint at 6 | |
| 53 | expect(r.event).toBe('course'); | |
| 54 | const stacked = place(r.state, 2); // 6 would end at 6, directly over the joint | |
| 55 | expect(stacked).toMatchObject({ ok: false, why: 'joint' }); | |
| 56 | r = place(r.state, 3); // 3 ends at 3: fine | |
| 57 | expect(r.ok).toBe(true); | |
| 58 | expect(place(r.state, 4)).toMatchObject({ ok: false, why: 'joint' }); // 3 + 3 = 6 | |
| 59 | expect(place(r.state, 99).why).toBe('bad-stone'); | |
| 60 | expect(place(r.state, 3).why).toBe('used'); | |
| 61 | }); | |
| 62 | ||
| 63 | test('a course completes when filled exactly, and the next begins', () => { | |
| 64 | const s = withPile([4, 4, 4, 3, 3, 3, 3]); | |
| 65 | let r = place(s, 0); r = place(r.state, 1); | |
| 66 | expect(r.event).toBe(''); | |
| 67 | r = place(r.state, 2); | |
| 68 | expect(r.event).toBe('course'); | |
| 69 | expect(r.state.courses).toEqual([[0, 1, 2]]); | |
| 70 | expect(r.state.current).toEqual([]); | |
| 71 | expect(r.state.line).toContain('Course 2 of 4'); | |
| 72 | }); | |
| 73 | ||
| 74 | test('undo lifts the last stone of the current course and its points, and not a finished course', () => { | |
| 75 | const s = withPile([4, 4, 4, 3, 3]); | |
| 76 | expect(undo(s)).toMatchObject({ ok: false, why: 'empty' }); | |
| 77 | let r = place(s, 0); r = place(r.state, 1); | |
| 78 | const u = undo(r.state); | |
| 79 | expect(u.ok).toBe(true); | |
| 80 | expect(u.state.current).toEqual([0]); | |
| 81 | expect(u.state.score).toBe(STONE_SCORE); | |
| 82 | expect(place(u.state, 1).ok).toBe(true); | |
| 83 | r = place(r.state, 2); | |
| 84 | expect(undo(r.state)).toMatchObject({ ok: false, why: 'empty' }); | |
| 85 | }); | |
| 86 | ||
| 87 | test('each stone scores, and a course of no stone under the stated width earns the bonus', () => { | |
| 88 | expect(BONUS_WIDTH).toBeGreaterThan(MIN_STONE); | |
| 89 | const s = withPile([4, 4, 4, 2, 5, 5, 3, 3, 3, 3]); | |
| 90 | let r = place(s, 0); r = place(r.state, 1); | |
| 91 | expect(r.state.score).toBe(2 * STONE_SCORE); | |
| 92 | r = place(r.state, 2); | |
| 93 | expect(r.state.score).toBe(3 * STONE_SCORE + COURSE_BONUS); | |
| 94 | // second course: 2 + 5 + 5 has a short stone, so no bonus | |
| 95 | r = place(r.state, 3); r = place(r.state, 4); r = place(r.state, 5); | |
| 96 | expect(r.event).toBe('course'); | |
| 97 | expect(r.state.score).toBe(6 * STONE_SCORE + COURSE_BONUS); | |
| 98 | }); | |
| 99 | ||
| 100 | test('the win line: every course built ends the game won', () => { | |
| 101 | const won = solve(start(3)); | |
| 102 | expect(won.status).toBe('over'); | |
| 103 | expect(won.result).toBe('won'); | |
| 104 | expect(won.courses).toHaveLength(COURSES); | |
| 105 | expect(won.line).toContain('Wall built'); | |
| 106 | expect(place(won, 0)).toMatchObject({ ok: false, why: 'over' }); | |
| 107 | expect(undo(won).why).toBe('over'); | |
| 108 | }); | |
| 109 | ||
| 110 | test('the loss line: no stone left can be placed', () => { | |
| 111 | const s = withPile([6, 6, 6, 6]); | |
| 112 | let r = place(s, 0); r = place(r.state, 1); | |
| 113 | // course 1 is done with a joint at 6, and every stone left would stack on it | |
| 114 | expect(r.event).toBe('lost'); | |
| 115 | expect(r.state.courses).toHaveLength(1); | |
| 116 | expect(r.state.status).toBe('over'); | |
| 117 | expect(r.state.result).toBe('lost'); | |
| 118 | expect(r.state.line).toContain('stuck'); | |
| 119 | expect(place(r.state, 2).why).toBe('over'); | |
| 120 | }); |
new above
offline