Public: anyone can read this; the crew can change it.
Migrated from github.com/amirmushichge/pixel-plays
Stained Glass rules: a tested `stained-glass/window.js` where you colour leaded panes so no two neighbours match, within par strokes, from plain data, in a two-file pull with a test file that runs in under a second #273
Check · Review · Land · Live done — Landed and live in production.
torque/pixel-plays:torque/249-stained-glass-rules-a-tested-stained-gla → main 2 commits fork 1 thread · 1 unresolved
2 files
changed +232
−0
stained-glass/window.js added +111
| @@ -0,0 +1,111 @@ | ||
| 1 | 'use strict'; | |
| 2 | // The Stained Glass rules: a window of leaded panes; colour every pane from a small palette so that no two panes | |
| 3 | // sharing a lead line match, in as few strokes as you can. | |
| 4 | // Plain data in, plain data out, no DOM, no timers, no randomness: the levels are hand-made and listed below. | |
| 5 | // A rejected call returns the very same state and says why. The page (not yet written) draws what this decides. | |
| 6 | // | |
| 7 | // Colours are whole numbers 1..palette; an empty pane is null. Every accepted paint is one stroke (painting a pane | |
| 8 | // the colour it already has still counts); clear is not a stroke. A level is solved when every pane is coloured and | |
| 9 | // clashes is empty: it scores SOLVE points, plus BONUS when its strokes are at or under par. Par is the number of | |
| 10 | // panes not fixed, the fewest strokes any solution can take. Solving the last level ends the game with a win line. | |
| 11 | // There is no loss; restart puts the current level back as it began (its strokes are forgotten, its score is kept). | |
| 12 | (function (root) { | |
| 13 | const SOLVE = 100; // points for solving a level | |
| 14 | const BONUS = 50; // extra points for solving at or under par | |
| 15 | ||
| 16 | // pairs are the lead lines: each joins two pane ids. fixed maps a pane id to its set colour. | |
| 17 | const LEVELS = [ | |
| 18 | { name: 'Rose', panes: [1, 2, 3, 4, 5], palette: 3, par: 4, | |
| 19 | pairs: [[1, 2], [2, 3], [3, 4], [4, 1], [5, 1], [5, 2], [5, 3], [5, 4]], fixed: { 5: 1 } }, | |
| 20 | { name: 'Wheel', panes: [1, 2, 3, 4, 5, 6, 7], palette: 3, par: 5, | |
| 21 | pairs: [[1, 2], [2, 3], [3, 4], [4, 5], [5, 6], [6, 1], [7, 1], [7, 2], [7, 3], [7, 4], [7, 5], [7, 6]], | |
| 22 | fixed: { 7: 3, 1: 1 } }, | |
| 23 | { name: 'Lancet', panes: [1, 2, 3, 4, 5, 6, 7, 8], palette: 4, par: 6, | |
| 24 | pairs: [[1, 2], [1, 3], [1, 4], [2, 3], [2, 4], [3, 4], [4, 5], [5, 6], [6, 7], [7, 8], [8, 5], [6, 8]], | |
| 25 | fixed: { 1: 1, 7: 2 } }, | |
| 26 | ]; | |
| 27 | ||
| 28 | const copy = s => Object.assign({}, s); | |
| 29 | const refuse = (s, why) => ({ ok: false, why, event: '', state: s }); | |
| 30 | const isPane = (lv, p) => Number.isInteger(p) && lv.panes.includes(p); | |
| 31 | ||
| 32 | // clashes(state) -> [[a, b]]: every pair of neighbouring panes with the same colour, in the order of the level's pairs | |
| 33 | function clashes(s) { | |
| 34 | return s.pairs.filter(([a, b]) => s.colours[a] !== null && s.colours[a] === s.colours[b]); | |
| 35 | } | |
| 36 | ||
| 37 | function refresh(s) { | |
| 38 | s.clashes = clashes(s); | |
| 39 | s.empty = s.panes.filter(p => s.colours[p] === null); | |
| 40 | if (s.status === 'won') { | |
| 41 | s.line = `The window is whole: ${s.strokes} strokes against par ${s.totalPar}, ${s.score} points`; | |
| 42 | } else { | |
| 43 | s.line = `${s.name}: ${s.levelStrokes} of par ${s.par} strokes, ${s.empty.length} bare, ${s.clashes.length} clashing`; | |
| 44 | } | |
| 45 | return s; | |
| 46 | } | |
| 47 | ||
| 48 | // a level begun: fixed panes coloured, the rest bare | |
| 49 | function begin(s, index) { | |
| 50 | const lv = LEVELS[index]; | |
| 51 | const colours = {}; | |
| 52 | for (const p of lv.panes) colours[p] = lv.fixed[p] === undefined ? null : lv.fixed[p]; | |
| 53 | s.level = index; s.levels = LEVELS.length; s.name = lv.name; s.panes = lv.panes.slice(); | |
| 54 | s.pairs = lv.pairs.map(p => p.slice()); s.palette = lv.palette; s.par = lv.par; | |
| 55 | s.fixed = Object.keys(lv.fixed).map(Number); s.colours = colours; s.levelStrokes = 0; | |
| 56 | return s; | |
| 57 | } | |
| 58 | ||
| 59 | const totalPar = LEVELS.reduce((n, l) => n + l.par, 0); | |
| 60 | ||
| 61 | // start(level = 0) -> state. level counts from 0; anything outside the list begins the first. | |
| 62 | function start(level = 0) { | |
| 63 | const i = Number.isInteger(level) && level >= 0 && level < LEVELS.length ? level : 0; | |
| 64 | return refresh(begin({ status: 'playing', score: 0, strokes: 0, totalPar, solved: [], line: '' }, i)); | |
| 65 | } | |
| 66 | ||
| 67 | // paint(state, pane, colour) -> { ok, why, event, state }. event is 'painted', 'solved' (the next level has begun) | |
| 68 | // or 'won' (the last level is solved). Refused: 'over', 'no-pane', 'fixed', 'bad-colour'. | |
| 69 | function paint(s, pane, colour) { | |
| 70 | if (s.status !== 'playing') return refuse(s, 'over'); | |
| 71 | if (!Number.isInteger(pane) || !s.panes.includes(pane)) return refuse(s, 'no-pane'); | |
| 72 | if (s.fixed.includes(pane)) return refuse(s, 'fixed'); | |
| 73 | if (!Number.isInteger(colour) || colour < 1 || colour > s.palette) return refuse(s, 'bad-colour'); | |
| 74 | const n = copy(s); | |
| 75 | n.colours = Object.assign({}, s.colours, { [pane]: colour }); | |
| 76 | n.strokes = s.strokes + 1; n.levelStrokes = s.levelStrokes + 1; | |
| 77 | refresh(n); | |
| 78 | if (n.empty.length || n.clashes.length) return { ok: true, why: '', event: 'painted', state: n }; | |
| 79 | const par = n.levelStrokes <= n.par; | |
| 80 | n.score += SOLVE + (par ? BONUS : 0); | |
| 81 | n.solved = s.solved.concat([{ name: n.name, strokes: n.levelStrokes, par: n.par, underPar: par }]); | |
| 82 | if (n.level + 1 >= LEVELS.length) { | |
| 83 | n.status = 'won'; | |
| 84 | return { ok: true, why: '', event: 'won', state: refresh(n) }; | |
| 85 | } | |
| 86 | return { ok: true, why: '', event: 'solved', state: refresh(begin(n, n.level + 1)) }; | |
| 87 | } | |
| 88 | ||
| 89 | // clear(state, pane) -> { ok, why, event: 'cleared', state }: empties a pane; not a stroke. | |
| 90 | // Refused: 'over', 'no-pane', 'fixed', 'already-bare'. | |
| 91 | function clear(s, pane) { | |
| 92 | if (s.status !== 'playing') return refuse(s, 'over'); | |
| 93 | if (!Number.isInteger(pane) || !s.panes.includes(pane)) return refuse(s, 'no-pane'); | |
| 94 | if (s.fixed.includes(pane)) return refuse(s, 'fixed'); | |
| 95 | if (s.colours[pane] === null) return refuse(s, 'already-bare'); | |
| 96 | const n = copy(s); | |
| 97 | n.colours = Object.assign({}, s.colours, { [pane]: null }); | |
| 98 | return { ok: true, why: '', event: 'cleared', state: refresh(n) }; | |
| 99 | } | |
| 100 | ||
| 101 | // restart(state) -> state: the current level as it began. Strokes made on it are forgotten; score is kept. | |
| 102 | function restart(s) { | |
| 103 | if (s.status !== 'playing') return s; | |
| 104 | const n = copy(s); | |
| 105 | n.strokes = s.strokes - s.levelStrokes; | |
| 106 | return refresh(begin(n, s.level)); | |
| 107 | } | |
| 108 | ||
| 109 | const api = { SOLVE, BONUS, LEVELS, start, paint, clear, clashes, restart }; | |
| 110 | if (typeof module !== 'undefined') module.exports = api; else root.StainedGlass = api; | |
| 111 | })(typeof window !== 'undefined' ? window : globalThis); |
stained-glass/window.test.mjs added +121
| @@ -0,0 +1,121 @@ | ||
| 1 | import { test, expect } from 'bun:test'; | |
| 2 | import { SOLVE, BONUS, LEVELS, start, paint, clear, clashes, restart } from './window.js'; | |
| 3 | ||
| 4 | // backtracking search for a colouring that respects the fixed panes and the lead lines | |
| 5 | function solve(lv) { | |
| 6 | const col = { ...lv.fixed }; | |
| 7 | const ok = (p, c) => lv.pairs.every(([a, b]) => (a === p ? col[b] !== c : b === p ? col[a] !== c : true)); | |
| 8 | const free = lv.panes.filter(p => lv.fixed[p] === undefined); | |
| 9 | const go = i => { | |
| 10 | if (i === free.length) return true; | |
| 11 | for (let c = 1; c <= lv.palette; c++) { | |
| 12 | if (ok(free[i], c)) { col[free[i]] = c; if (go(i + 1)) return true; delete col[free[i]]; } | |
| 13 | } | |
| 14 | return false; | |
| 15 | }; | |
| 16 | const fixedOk = lv.pairs.every(([a, b]) => col[a] === undefined || col[a] !== col[b]); | |
| 17 | return fixedOk && go(0) ? col : null; | |
| 18 | } | |
| 19 | const play = (s, col) => { | |
| 20 | let r = { state: s }; | |
| 21 | const skip = s.fixed; // read before the last stroke moves the window on to the next level | |
| 22 | for (const [p, c] of Object.entries(col)) if (!skip.includes(+p)) r = paint(r.state, +p, c); | |
| 23 | return r; | |
| 24 | }; | |
| 25 | ||
| 26 | test('every level is well formed and has a solution that takes exactly par strokes', () => { | |
| 27 | expect(LEVELS.length).toBeGreaterThanOrEqual(3); | |
| 28 | LEVELS.forEach((lv, i) => { | |
| 29 | const ids = new Set(lv.panes); | |
| 30 | expect(ids.size).toBe(lv.panes.length); | |
| 31 | for (const [a, b] of lv.pairs) { expect(ids.has(a) && ids.has(b) && a !== b).toBe(true); } | |
| 32 | for (const [p, c] of Object.entries(lv.fixed)) { expect(ids.has(+p)).toBe(true); expect(c).toBeGreaterThanOrEqual(1); expect(c).toBeLessThanOrEqual(lv.palette); } | |
| 33 | const sol = solve(lv); | |
| 34 | expect(sol).not.toBeNull(); | |
| 35 | expect(lv.par).toBe(lv.panes.length - Object.keys(lv.fixed).length); | |
| 36 | const s = start(i); | |
| 37 | expect([s.level, s.palette, s.par, s.fixed.length]).toEqual([i, lv.palette, lv.par, Object.keys(lv.fixed).length]); | |
| 38 | expect(s.empty).toHaveLength(lv.par); | |
| 39 | }); | |
| 40 | }); | |
| 41 | ||
| 42 | test('a fixed pane and an off-palette colour are refused with the state untouched', () => { | |
| 43 | const s = start(0); | |
| 44 | for (const [pane, colour, why] of [[5, 2, 'fixed'], [1, 4, 'bad-colour'], [1, 0, 'bad-colour'], [1, 1.5, 'bad-colour'], [99, 1, 'no-pane']]) { | |
| 45 | const r = paint(s, pane, colour); | |
| 46 | expect([r.ok, r.why, r.state]).toEqual([false, why, s]); | |
| 47 | } | |
| 48 | expect(clear(s, 5).why).toBe('fixed'); | |
| 49 | expect(s.strokes).toBe(0); | |
| 50 | }); | |
| 51 | ||
| 52 | test('a clash is found, listed, and cleared; clearing is not a stroke', () => { | |
| 53 | let s = paint(start(0), 1, 2).state; | |
| 54 | expect(clashes(s)).toEqual([]); | |
| 55 | s = paint(s, 2, 2).state; // 1 and 2 share a lead line | |
| 56 | expect(clashes(s)).toEqual([[1, 2]]); | |
| 57 | s = paint(s, 3, 1).state; // 3 meets fixed 5, which is colour 1 | |
| 58 | expect(clashes(s)).toEqual([[1, 2], [5, 3]]); | |
| 59 | expect(s.clashes).toEqual(clashes(s)); | |
| 60 | const c = clear(s, 2); | |
| 61 | expect([c.ok, c.event, c.state.colours[2], c.state.strokes]).toEqual([true, 'cleared', null, 3]); | |
| 62 | expect(clashes(c.state)).toEqual([[5, 3]]); | |
| 63 | expect(clear(c.state, 2).why).toBe('already-bare'); | |
| 64 | }); | |
| 65 | ||
| 66 | test('every accepted paint is one stroke, refused ones are none', () => { | |
| 67 | let s = start(0); | |
| 68 | s = paint(s, 1, 2).state; s = paint(s, 1, 3).state; s = paint(s, 1, 3).state; | |
| 69 | expect([s.strokes, s.levelStrokes]).toEqual([3, 3]); | |
| 70 | expect(paint(s, 5, 2).state.strokes).toBe(3); | |
| 71 | }); | |
| 72 | ||
| 73 | test('solving at par scores the solve points and the bonus, then the next level begins', () => { | |
| 74 | const r = play(start(0), solve(LEVELS[0])); | |
| 75 | expect(r.event).toBe('solved'); | |
| 76 | expect(r.state.score).toBe(SOLVE + BONUS); | |
| 77 | expect([r.state.level, r.state.levelStrokes, r.state.strokes]).toEqual([1, 0, LEVELS[0].par]); | |
| 78 | expect(r.state.solved).toEqual([{ name: LEVELS[0].name, strokes: LEVELS[0].par, par: LEVELS[0].par, underPar: true }]); | |
| 79 | }); | |
| 80 | ||
| 81 | test('solving over par scores no bonus', () => { | |
| 82 | let s = start(0); | |
| 83 | s = paint(s, 1, 3).state; // a wasted stroke | |
| 84 | const r = play(s, solve(LEVELS[0])); | |
| 85 | expect(r.event).toBe('solved'); | |
| 86 | expect(r.state.score).toBe(SOLVE); | |
| 87 | expect(r.state.solved[0].underPar).toBe(false); | |
| 88 | }); | |
| 89 | ||
| 90 | test('restart puts the level back, forgets its strokes and keeps the score', () => { | |
| 91 | let s = play(start(0), solve(LEVELS[0])).state; | |
| 92 | expect(LEVELS[1].fixed[2]).toBeUndefined(); // pane 2 is free in Wheel (pane 1 is fixed) | |
| 93 | const p = paint(s, 2, 2); | |
| 94 | expect(p.ok).toBe(true); | |
| 95 | expect(p.state.levelStrokes).toBe(1); | |
| 96 | expect(p.state.strokes).toBe(LEVELS[0].par + 1); | |
| 97 | expect(p.state.colours[2]).toBe(2); | |
| 98 | const r = restart(p.state); | |
| 99 | expect([r.level, r.strokes, r.levelStrokes, r.score]).toEqual([1, LEVELS[0].par, 0, SOLVE + BONUS]); | |
| 100 | expect(r.empty).toHaveLength(LEVELS[1].par); | |
| 101 | expect(r.colours[2]).toBeNull(); | |
| 102 | expect(r.colours[7]).toBe(3); | |
| 103 | }); | |
| 104 | ||
| 105 | test('the last level ends the game with a win line of total strokes against total par', () => { | |
| 106 | let s = start(0), r; | |
| 107 | for (let i = 0; i < LEVELS.length; i++) { r = play(s, solve(LEVELS[i])); s = r.state; } | |
| 108 | const par = LEVELS.reduce((n, l) => n + l.par, 0); | |
| 109 | expect(r.event).toBe('won'); | |
| 110 | expect(s.status).toBe('won'); | |
| 111 | expect(s.score).toBe(LEVELS.length * (SOLVE + BONUS)); | |
| 112 | expect(s.line).toBe(`The window is whole: ${par} strokes against par ${par}, ${s.score} points`); | |
| 113 | const after = paint(s, 1, 1); | |
| 114 | expect([after.ok, after.why, after.state]).toEqual([false, 'over', s]); | |
| 115 | expect(restart(s)).toBe(s); | |
| 116 | }); | |
| 117 | ||
| 118 | test('a bad level number begins the first', () => { | |
| 119 | for (const l of [-1, 99, 1.5, 'x', undefined]) expect(start(l).level).toBe(0); | |
| 120 | expect(start(2).level).toBe(2); | |
| 121 | }); |
new above
offline
Pane 1 is fixed in Wheel, so this paint is refused and the state is unchanged. The restart assertions below then pass without restart doing anything. Use a free pane (2) and check the paint was accepted (levelStrokes 1) before restarting.