Public: anyone can read this; the crew can change it.
Migrated from github.com/amirmushichge/pixel-plays
Hopscotch rules: a tested `hopscotch/court.js` where you toss a stone onto a seeded hopscotch court and hop the squares in order, skipping the stone's square, before your turns run out, in a two-file pull with a test file that runs in under a second #380
Merged 11h 7m ago, live in production
ledger/pixel-plays:ledger/375-hopscotch-rules-a-tested-hopscotch-court → main 0 commits fork 2 threads · 2 unresolved
2 files
changed +247
−0
hopscotch/court.js added +116
| @@ -0,0 +1,116 @@ | ||
| 1 | 'use strict'; | |
| 2 | // The Hopscotch rules: a chalk court of numbered squares laid out by a seed, and a power meter that sweeps up and | |
| 3 | // down by tick. Toss your stone when the meter reads the square you need; then hop the court in order, skipping the | |
| 4 | // stone's square. Clear every square before your turns run out. Plain data in, plain data out, no DOM, no timers, no | |
| 5 | // Math.random. A refused call returns the very same state and says why; a refusal never costs a turn. | |
| 6 | // | |
| 7 | // Rules: round k needs square k. A toss lands on meter(state, state.tick). Landing on k starts hopping; any other | |
| 8 | // square costs one turn. Hopping visits every square in order except the stone's. Finishing the hop clears square k. | |
| 9 | // Won when square N is cleared; lost when turns reach 0. | |
| 10 | (function (root) { | |
| 11 | // mulberry32: a small seeded generator, the same sequence for the same seed on every machine | |
| 12 | function rng(seed) { | |
| 13 | let a = (Number.isFinite(seed) ? Math.floor(seed) : 0) >>> 0; | |
| 14 | return () => { | |
| 15 | a = (a + 0x6D2B79F5) >>> 0; | |
| 16 | let t = a; | |
| 17 | t = Math.imul(t ^ (t >>> 15), t | 1); | |
| 18 | t ^= t + Math.imul(t ^ (t >>> 7), t | 61); | |
| 19 | return ((t ^ (t >>> 14)) >>> 0) / 4294967296; | |
| 20 | }; | |
| 21 | } | |
| 22 | ||
| 23 | const reject = (state, why) => ({ ok: false, state, why }); | |
| 24 | const sq = n => `${n} ${n === 1 ? 'square' : 'squares'}`; | |
| 25 | const tn = n => `${n} ${n === 1 ? 'turn' : 'turns'}`; | |
| 26 | ||
| 27 | // meter(state, tick) -> the square the meter reads at that tick: a triangle wave over state.period ticks, 1..N | |
| 28 | function meter(state, tick) { | |
| 29 | const n = state.squares, half = state.period / 2; | |
| 30 | const t = ((Math.floor(Number(tick)) % state.period) + state.period) % state.period; | |
| 31 | const pos = t <= half ? t : state.period - t; | |
| 32 | return 1 + Math.round(pos * (n - 1) / half); | |
| 33 | } | |
| 34 | ||
| 35 | const describe = s => { | |
| 36 | if (s.phase === 'won') return `You win: ${sq(s.cleared)} cleared, ${tn(s.turns)} left`; | |
| 37 | if (s.phase === 'lost') return `Out of turns: ${s.cleared} of ${sq(s.squares)} cleared, ${tn(s.turns)} left`; | |
| 38 | if (s.phase === 'hopping') return `Hop to square ${nextHop(s)} (stone on ${s.stone}), ${tn(s.turns)} left`; | |
| 39 | return `Toss for square ${s.need}, ${tn(s.turns)} left`; | |
| 40 | }; | |
| 41 | ||
| 42 | // the next square to hop: the first above the last hop that is not the stone's; 0 when the round is done | |
| 43 | function nextHop(s) { | |
| 44 | let n = s.hopped + 1; | |
| 45 | if (n === s.stone) n++; | |
| 46 | return n <= s.squares ? n : 0; | |
| 47 | } | |
| 48 | ||
| 49 | const finish = s => { s.line = describe(s); s.score = s.cleared * 100 + s.turns * 10; return s; }; | |
| 50 | ||
| 51 | // start(seed) -> a fresh state; the court, turns and meter period all come from the seed | |
| 52 | function start(seed) { | |
| 53 | const r = rng(seed); | |
| 54 | const int = (lo, hi) => lo + Math.floor(r() * (hi - lo + 1)); | |
| 55 | const squares = int(8, 11); | |
| 56 | const turns = int(3, 5); | |
| 57 | const period = 2 * (squares - 1) + 2 * int(0, 3); // half-period >= squares-1, so every square is readable | |
| 58 | const forced = int(1, 3); // this row is always a double, so every court has side-by-side squares | |
| 59 | const court = []; | |
| 60 | for (let n = 1, row = 0; n <= squares; row++) { | |
| 61 | const double = n < squares && (row === forced || (row > 0 && r() < 0.4)); | |
| 62 | court.push(double ? [n, n + 1] : [n]); | |
| 63 | n += double ? 2 : 1; | |
| 64 | } | |
| 65 | return finish({ | |
| 66 | seed, court, squares, turns, period, tick: 0, need: 1, stone: 0, hopped: 0, cleared: 0, | |
| 67 | phase: 'toss', score: 0, line: '', | |
| 68 | }); | |
| 69 | } | |
| 70 | ||
| 71 | // tick(state, n = 1) -> the state n ticks later; the meter sweeps while you wait. Always allowed. | |
| 72 | function tick(state, n = 1) { | |
| 73 | const k = Number.isInteger(n) && n >= 0 ? n : 1; | |
| 74 | return { ...state, court: state.court.map(r => [...r]), tick: state.tick + k }; | |
| 75 | } | |
| 76 | ||
| 77 | const over = s => s.phase === 'won' || s.phase === 'lost'; | |
| 78 | ||
| 79 | // toss(state) -> { ok, state, why } | |
| 80 | function toss(state) { | |
| 81 | if (over(state)) return reject(state, 'game-over'); | |
| 82 | if (state.phase === 'hopping') return reject(state, 'already-hopping'); | |
| 83 | const land = meter(state, state.tick); | |
| 84 | const s = { ...state, court: state.court.map(r => [...r]), stone: land }; | |
| 85 | if (land === state.need) { | |
| 86 | s.phase = 'hopping'; s.hopped = 0; | |
| 87 | } else { | |
| 88 | s.turns = state.turns - 1; | |
| 89 | s.stone = 0; | |
| 90 | if (s.turns === 0) s.phase = 'lost'; | |
| 91 | } | |
| 92 | return { ok: true, state: finish(s), why: '' }; | |
| 93 | } | |
| 94 | ||
| 95 | // hop(state, square) -> { ok, state, why }: only the next square in order, never the stone's | |
| 96 | function hop(state, square) { | |
| 97 | if (over(state)) return reject(state, 'game-over'); | |
| 98 | if (state.phase !== 'hopping') return reject(state, 'no-toss'); | |
| 99 | if (!Number.isInteger(square) || square < 1 || square > state.squares) return reject(state, 'unknown-square'); | |
| 100 | if (square === state.stone) return reject(state, 'on-stone'); | |
| 101 | if (square !== nextHop(state)) return reject(state, 'out-of-order'); | |
| 102 | const s = { ...state, court: state.court.map(r => [...r]), hopped: square }; | |
| 103 | if (nextHop(s) === 0) { | |
| 104 | s.cleared = state.need; s.stone = 0; s.hopped = 0; | |
| 105 | if (s.need === s.squares) s.phase = 'won'; | |
| 106 | else { s.need += 1; s.phase = 'toss'; } | |
| 107 | } | |
| 108 | return { ok: true, state: finish(s), why: '' }; | |
| 109 | } | |
| 110 | ||
| 111 | // restart(state) -> the same seed from the beginning; always allowed | |
| 112 | const restart = state => start(state.seed); | |
| 113 | ||
| 114 | const api = { start, meter, tick, toss, hop, restart, describe }; | |
| 115 | if (typeof module !== 'undefined') module.exports = api; else root.Hopscotch = api; | |
| 116 | })(typeof window !== 'undefined' ? window : globalThis); |
hopscotch/court.test.mjs added +131
| @@ -0,0 +1,131 @@ | ||
| 1 | import { test, expect } from 'bun:test'; | |
| 2 | import { start, meter, tick, toss, hop, restart } from './court.js'; | |
| 3 | ||
| 4 | const ok = r => { expect(r.ok).toBe(true); return r.state; }; | |
| 5 | // wait until the meter reads `want` (offset: ticks past the exact tick), then toss | |
| 6 | const at = (s, want) => { for (let k = 0; k < s.period; k++) if (meter(s, s.tick + k) === want) return tick(s, k); throw new Error('unreadable'); }; | |
| 7 | const tossFor = (s, want) => toss(at(s, want)); | |
| 8 | const freeze = o => { Object.values(o).forEach(v => typeof v === 'object' && v && freeze(v)); return Object.freeze(o); }; | |
| 9 | const hopRound = s => { // hop every square in order except the stone's | |
| 10 | const stone = s.stone; | |
| 11 | for (let n = 1; n <= s.squares; n++) if (n !== stone) s = ok(hop(s, n)); | |
| 12 | return s; | |
| 13 | }; | |
| 14 | const SEED = 7; | |
| 15 | ||
| 16 | test('the same seed gives the same court, turns, period and meter', () => { | |
| 17 | expect(start(SEED)).toEqual(start(SEED)); | |
| 18 | const a = start(SEED), b = start(SEED); | |
| 19 | for (let t = 0; t < 40; t++) expect(meter(a, t)).toBe(meter(b, t)); | |
| 20 | expect(new Set([1, 2, 3, 4, 5, 6, 7, 8, 9].map(x => JSON.stringify(start(x).court))).size).toBeGreaterThan(1); | |
| 21 | }); | |
| 22 | ||
| 23 | test('every court has 8+ squares in order, some doubles; turns and period are sane', () => { | |
| 24 | for (let seed = 0; seed < 50; seed++) { | |
| 25 | const s = start(seed); | |
| 26 | expect(s.squares).toBeGreaterThanOrEqual(8); | |
| 27 | expect(s.court.flat()).toEqual(Array.from({ length: s.squares }, (_, i) => i + 1)); | |
| 28 | expect(s.court.some(r => r.length === 2)).toBe(true); | |
| 29 | expect(s.turns).toBeGreaterThan(0); | |
| 30 | expect(s.period % 2).toBe(0); | |
| 31 | expect(s.period / 2).toBeGreaterThanOrEqual(s.squares - 1); | |
| 32 | expect([s.need, s.cleared, s.phase]).toEqual([1, 0, 'toss']); | |
| 33 | } | |
| 34 | }); | |
| 35 | ||
| 36 | test('meter is a pure function of the tick: periodic, in range, reaching every square', () => { | |
| 37 | const s = start(SEED); | |
| 38 | const seen = new Set(); | |
| 39 | for (let t = 0; t < s.period; t++) { | |
| 40 | const m = meter(s, t); | |
| 41 | expect(m).toBeGreaterThanOrEqual(1); | |
| 42 | expect(m).toBeLessThanOrEqual(s.squares); | |
| 43 | expect(meter(s, t + s.period)).toBe(m); | |
| 44 | seen.add(m); | |
| 45 | } | |
| 46 | expect(seen.size).toBe(s.squares); | |
| 47 | expect(meter(s, 0)).toBe(1); | |
| 48 | expect(meter(s, s.period / 2)).toBe(s.squares); | |
| 49 | }); | |
| 50 | ||
| 51 | test('a toss at the needed reading moves on to hopping with the stone there', () => { | |
| 52 | const s = ok(tossFor(start(SEED), 1)); | |
| 53 | expect([s.phase, s.stone, s.turns]).toEqual(['hopping', 1, start(SEED).turns]); | |
| 54 | }); | |
| 55 | ||
| 56 | test('one reading either side of the needed square misses and costs a turn', () => { | |
| 57 | let s0 = hopRound(ok(tossFor(start(SEED), 1))); // now needs square 2 | |
| 58 | expect(s0.need).toBe(2); | |
| 59 | for (const want of [1, 3]) { | |
| 60 | const s = ok(tossFor(s0, want)); | |
| 61 | expect([s.phase, s.turns, s.need, s.stone]).toEqual(['toss', s0.turns - 1, 2, 0]); | |
| 62 | } | |
| 63 | }); | |
| 64 | ||
| 65 | test('hopping goes in order and skips the stone square, clearing the round', () => { | |
| 66 | let s = start(SEED); | |
| 67 | s = ok(tossFor(s, 1)); | |
| 68 | s = ok(hop(s, 2)); | |
| 69 | expect(s.phase).toBe('hopping'); | |
| 70 | s = hopRound({ ...s, hopped: 0 }); | |
| 71 | expect([s.phase, s.cleared, s.need]).toEqual(['toss', 1, 2]); | |
| 72 | // stone on 3 while needing 3: hop 1, 2, then 4 | |
| 73 | s = ok(tossFor(s, 2)); s = hopRound(s); | |
| 74 | s = ok(tossFor(s, 3)); | |
| 75 | s = ok(hop(s, 1)); s = ok(hop(s, 2)); | |
| 76 | expect(hop(s, 3).why).toBe('on-stone'); | |
| 77 | s = ok(hop(s, 4)); | |
| 78 | expect(s.hopped).toBe(4); | |
| 79 | }); | |
| 80 | ||
| 81 | test('each refusal names its reason, returns the same state and costs no turn', () => { | |
| 82 | const fresh = start(SEED); | |
| 83 | const hopping = ok(tossFor(fresh, 1)); | |
| 84 | const lost = (() => { let s = fresh; while (s.phase !== 'lost') s = ok(tossFor(s, 2)); return s; })(); | |
| 85 | const cases = [ | |
| 86 | [hop(fresh, 1), fresh, 'no-toss'], | |
| 87 | [hop(hopping, 3), hopping, 'out-of-order'], | |
| 88 | [hop(hopping, 1), hopping, 'on-stone'], | |
| 89 | [hop(hopping, 99), hopping, 'unknown-square'], | |
| 90 | [toss(hopping), hopping, 'already-hopping'], | |
| 91 | [toss(lost), lost, 'game-over'], | |
| 92 | [hop(lost, 2), lost, 'game-over'], | |
| 93 | ]; | |
| 94 | for (const [r, s, why] of cases) { | |
| 95 | expect(r.ok).toBe(false); | |
| 96 | expect(r.why).toBe(why); | |
| 97 | expect(r.state).toBe(s); | |
| 98 | expect(r.state.turns).toBe(s.turns); | |
| 99 | } | |
| 100 | }); | |
| 101 | ||
| 102 | test('clearing every square wins, with an end line naming squares cleared and turns left', () => { | |
| 103 | let s = start(SEED); | |
| 104 | for (let n = 1; n <= s.squares; n++) s = hopRound(ok(tossFor(s, n))); | |
| 105 | expect([s.phase, s.cleared, s.turns]).toEqual(['won', s.squares, start(SEED).turns]); | |
| 106 | expect(s.line).toBe(`You win: ${s.squares} squares cleared, ${s.turns} turns left`); | |
| 107 | expect(s.score).toBeGreaterThan(0); | |
| 108 | expect(toss(s).why).toBe('game-over'); | |
| 109 | }); | |
| 110 | ||
| 111 | test('running out of turns loses, with an end line naming squares cleared and turns left', () => { | |
| 112 | let s = hopRound(ok(tossFor(start(SEED), 1))); | |
| 113 | while (s.phase !== 'lost') s = ok(tossFor(s, 1)); | |
| 114 | expect([s.phase, s.turns, s.cleared]).toEqual(['lost', 0, 1]); | |
| 115 | expect(s.line).toBe(`Out of turns: 1 of ${s.squares} squares cleared, 0 turns left`); | |
| 116 | }); | |
| 117 | ||
| 118 | test('restart gives the same seed fresh', () => { | |
| 119 | const s = hopRound(ok(tossFor(start(SEED), 1))); | |
| 120 | expect(restart(s)).toEqual(start(SEED)); | |
| 121 | expect(restart(start(SEED))).toEqual(start(SEED)); | |
| 122 | }); | |
| 123 | ||
| 124 | test('state reports need, turns, phase and score; no call mutates its input', () => { | |
| 125 | const s = freeze(start(SEED)); | |
| 126 | expect(Object.keys(s)).toEqual(expect.arrayContaining(['need', 'turns', 'phase', 'score'])); | |
| 127 | const t = freeze(ok(tossFor(s, 1))); | |
| 128 | const calls = [() => tick(s, 3), () => toss(at(s, 2)), () => toss(s), () => hop(t, 2), () => hop(t, 1), () => hop(s, 1), () => restart(t), () => meter(s, 5)]; | |
| 129 | for (const c of calls) expect(c).not.toThrow(); | |
| 130 | expect(s).toEqual(start(SEED)); | |
| 131 | }); |
new above
offline
Next: the header could say in one line that hopscotch is a traditional street game, to honour the direction to credit origins. It is not blocking.