Public: anyone can read this; the crew can change it.
Migrated from github.com/amirmushichge/pixel-plays
Tea Party rules: a tested `tea-party/table.js` where guests call out their orders and you serve each one from memory as the table grows, from plain data, in a two-file pull with a test file that runs in under a second #297
Check · Review · Land · Live done — Landed and live in production.
vesper/pixel-plays:vesper/275-tea-party-rules-a-tested-tea-party-table → main 1 commit fork
2 files
changed +240
−0
tea-party/table.js added +119
| @@ -0,0 +1,119 @@ | ||
| 1 | 'use strict'; | |
| 2 | // The Tea Party rules: guests call out their orders one at a time, then you serve each from memory. Plain data in, | |
| 3 | // plain data out, no DOM, no timers, no Math.random: every order comes from the seed. A rejected call returns the | |
| 4 | // very same state and says why. | |
| 5 | // | |
| 6 | // An order has four parts: drink, milk, sugars, cake. Time is in seconds, summed by tick() while the orders are shown. | |
| 7 | (function (root) { | |
| 8 | const DRINKS = ['tea', 'coffee']; | |
| 9 | const MILKS = ['none', 'milk']; | |
| 10 | const SUGARS = [0, 1, 2, 3]; | |
| 11 | const CAKES = ['scone', 'sponge', 'tart', 'eclair']; | |
| 12 | const PARTS = ['drink', 'milk', 'sugars', 'cake']; | |
| 13 | const START_GUESTS = 2; // guests at the table in round one | |
| 14 | const MAX_GUESTS = 5; // the table never grows past this; clearing a round at this size wins | |
| 15 | const SHOW_SECONDS = 1.5; // s each guest's order is shown | |
| 16 | const PART_POINTS = 10; // score per matching part | |
| 17 | const PERFECT_BONUS = 20; // extra score for a cup with all four parts right | |
| 18 | const MAX_WRONG = 3; // wrong parts in one round that end the game | |
| 19 | const LAST_ROUND = MAX_GUESTS - START_GUESTS + 1; | |
| 20 | ||
| 21 | const OPTIONS = { drink: DRINKS, milk: MILKS, sugars: SUGARS, cake: CAKES }; | |
| 22 | ||
| 23 | // mulberry32 as a pure step: next(seed) -> [value in [0,1), nextSeed] | |
| 24 | function next(r) { | |
| 25 | r = (r + 0x6D2B79F5) >>> 0; | |
| 26 | let t = r; | |
| 27 | t = Math.imul(t ^ (t >>> 15), t | 1); | |
| 28 | t ^= t + Math.imul(t ^ (t >>> 7), t | 61); | |
| 29 | return [((t ^ (t >>> 14)) >>> 0) / 4294967296, r]; | |
| 30 | } | |
| 31 | ||
| 32 | function deal(rng, n) { | |
| 33 | const orders = []; | |
| 34 | for (let i = 0; i < n; i++) { | |
| 35 | const o = {}; | |
| 36 | for (const p of PARTS) { | |
| 37 | let v; [v, rng] = next(rng); | |
| 38 | o[p] = OPTIONS[p][Math.floor(v * OPTIONS[p].length)]; | |
| 39 | } | |
| 40 | orders.push(o); | |
| 41 | } | |
| 42 | return [orders, rng]; | |
| 43 | } | |
| 44 | ||
| 45 | function describe(s) { | |
| 46 | if (s.status === 'won') return `You win: score ${s.score}, ${s.perfect} perfect ${s.perfect === 1 ? 'cup' : 'cups'}`; | |
| 47 | if (s.status === 'lost') return `You lose: ${s.wrong} wrong parts in round ${s.round}, score ${s.score}`; | |
| 48 | if (s.phase === 'order') return `Round ${s.round}: listen to ${s.guests} guests, ${s.shown} of ${s.guests} heard`; | |
| 49 | return `Round ${s.round}: serve ${s.guests - s.served.length} more, score ${s.score}`; | |
| 50 | } | |
| 51 | ||
| 52 | function lay(seed, round, rng, base) { | |
| 53 | const guests = START_GUESTS + round - 1; | |
| 54 | const [orders, r] = deal(rng, guests); | |
| 55 | const s = { | |
| 56 | ...base, status: 'playing', phase: 'order', round, guests, orders, rng: r, | |
| 57 | elapsed: 0, shown: 1, served: [], wrong: 0, last: null, line: '', | |
| 58 | }; | |
| 59 | s.line = describe(s); | |
| 60 | return s; | |
| 61 | } | |
| 62 | ||
| 63 | // start(seed) -> a table for START_GUESTS guests in round one | |
| 64 | function start(seed) { | |
| 65 | const sd = Number.isFinite(seed) ? Math.trunc(seed) >>> 0 : 0; | |
| 66 | return lay(sd, 1, sd, { seed: sd, score: 0, perfect: 0 }); | |
| 67 | } | |
| 68 | ||
| 69 | // tick(state, dt) -> { ok, state, why }: shows one guest's order every SHOW_SECONDS, then switches to serve | |
| 70 | function tick(state, dt) { | |
| 71 | if (typeof dt !== 'number' || !Number.isFinite(dt) || dt <= 0) return { ok: false, state, why: 'bad-dt' }; | |
| 72 | if (state.status !== 'playing') return { ok: false, state, why: 'game-over' }; | |
| 73 | if (state.phase !== 'order') return { ok: false, state, why: 'not-order' }; | |
| 74 | const elapsed = state.elapsed + dt; | |
| 75 | const s = { ...state, elapsed }; | |
| 76 | if (elapsed >= s.guests * SHOW_SECONDS) { s.phase = 'serve'; s.shown = 0; } | |
| 77 | else s.shown = Math.floor(elapsed / SHOW_SECONDS) + 1; | |
| 78 | s.line = describe(s); | |
| 79 | return { ok: true, state: s, why: '' }; | |
| 80 | } | |
| 81 | ||
| 82 | function validOrder(o) { | |
| 83 | if (!o || typeof o !== 'object') return false; | |
| 84 | return PARTS.every(p => Object.prototype.hasOwnProperty.call(o, p) && OPTIONS[p].includes(o[p])); | |
| 85 | } | |
| 86 | ||
| 87 | // serve(state, guest, order) -> { ok, state, why }: scores the cup, then ends or advances the round when all are served | |
| 88 | function serve(state, guest, order) { | |
| 89 | if (state.status !== 'playing') return { ok: false, state, why: 'game-over' }; | |
| 90 | if (state.phase !== 'serve') return { ok: false, state, why: 'still-ordering' }; | |
| 91 | if (!Number.isInteger(guest) || guest < 0 || guest >= state.guests) return { ok: false, state, why: 'no-such-guest' }; | |
| 92 | if (state.served.includes(guest)) return { ok: false, state, why: 'already-served' }; | |
| 93 | if (!validOrder(order)) return { ok: false, state, why: 'bad-order' }; | |
| 94 | const want = state.orders[guest]; | |
| 95 | const matched = PARTS.filter(p => order[p] === want[p]).length; | |
| 96 | const perfect = matched === PARTS.length; | |
| 97 | let s = { | |
| 98 | ...state, | |
| 99 | served: [...state.served, guest], | |
| 100 | score: state.score + matched * PART_POINTS + (perfect ? PERFECT_BONUS : 0), | |
| 101 | perfect: state.perfect + (perfect ? 1 : 0), | |
| 102 | wrong: state.wrong + PARTS.length - matched, | |
| 103 | last: { guest, matched, perfect }, | |
| 104 | }; | |
| 105 | if (s.served.length === s.guests) { | |
| 106 | if (s.wrong >= MAX_WRONG) s.status = 'lost'; | |
| 107 | else if (s.round >= LAST_ROUND) s.status = 'won'; | |
| 108 | else s = lay(s.seed, s.round + 1, s.rng, { seed: s.seed, score: s.score, perfect: s.perfect }); | |
| 109 | } | |
| 110 | s.line = describe(s); | |
| 111 | return { ok: true, state: s, why: '' }; | |
| 112 | } | |
| 113 | ||
| 114 | const api = { | |
| 115 | DRINKS, MILKS, SUGARS, CAKES, PARTS, START_GUESTS, MAX_GUESTS, SHOW_SECONDS, PART_POINTS, PERFECT_BONUS, | |
| 116 | MAX_WRONG, LAST_ROUND, start, tick, serve, describe, | |
| 117 | }; | |
| 118 | if (typeof module !== 'undefined') module.exports = api; else root.TeaParty = api; | |
| 119 | })(typeof window !== 'undefined' ? window : globalThis); |
tea-party/table.test.mjs added +121
| @@ -0,0 +1,121 @@ | ||
| 1 | import { test, expect } from 'bun:test'; | |
| 2 | import { | |
| 3 | DRINKS, MILKS, SUGARS, CAKES, START_GUESTS, MAX_GUESTS, SHOW_SECONDS, PART_POINTS, PERFECT_BONUS, MAX_WRONG, | |
| 4 | LAST_ROUND, start, tick, serve, | |
| 5 | } from './table.js'; | |
| 6 | ||
| 7 | const listen = s => tick(s, s.guests * SHOW_SECONDS).state; | |
| 8 | const exact = (s, g) => ({ ...s.orders[g] }); | |
| 9 | const wrongly = (s, g, n) => { // an order with exactly n parts wrong | |
| 10 | const o = exact(s, g); | |
| 11 | const flips = [ | |
| 12 | ['drink', DRINKS], ['milk', MILKS], ['sugars', SUGARS], ['cake', CAKES], | |
| 13 | ]; | |
| 14 | for (let i = 0; i < n; i++) { const [p, opts] = flips[i]; o[p] = opts[(opts.indexOf(o[p]) + 1) % opts.length]; } | |
| 15 | return o; | |
| 16 | }; | |
| 17 | ||
| 18 | test('start lays the stated guests with orders drawn from the stated options; same seed, same orders', () => { | |
| 19 | const a = start(7), b = start(7), c = start(8); | |
| 20 | expect([a.round, a.guests, a.phase, a.status]).toEqual([1, START_GUESTS, 'order', 'playing']); | |
| 21 | expect(a.orders).toEqual(b.orders); | |
| 22 | expect(JSON.stringify(start(1).orders) === JSON.stringify(start(2).orders) && JSON.stringify(a.orders) === JSON.stringify(c.orders)).toBe(false); | |
| 23 | for (let seed = 0; seed < 30; seed++) for (const o of start(seed).orders) { | |
| 24 | expect(DRINKS).toContain(o.drink); expect(MILKS).toContain(o.milk); | |
| 25 | expect(SUGARS).toContain(o.sugars); expect(CAKES).toContain(o.cake); | |
| 26 | } | |
| 27 | }); | |
| 28 | ||
| 29 | test('tick shows one order at a time at the stated pace, then switches to serve', () => { | |
| 30 | let s = start(3); | |
| 31 | expect(s.shown).toBe(1); | |
| 32 | s = tick(s, SHOW_SECONDS - 0.01).state; | |
| 33 | expect([s.shown, s.phase]).toEqual([1, 'order']); | |
| 34 | s = tick(s, 0.02).state; | |
| 35 | expect([s.shown, s.phase]).toEqual([2, 'order']); | |
| 36 | s = tick(s, SHOW_SECONDS).state; | |
| 37 | expect(s.phase).toBe('serve'); | |
| 38 | expect(tick(s, 1).why).toBe('not-order'); | |
| 39 | }); | |
| 40 | ||
| 41 | test('dt must be a finite number above 0', () => { | |
| 42 | const s = start(1); | |
| 43 | for (const dt of [0, -1, NaN, Infinity, '1', null, undefined]) { | |
| 44 | const r = tick(s, dt); | |
| 45 | expect([r.ok, r.why]).toEqual([false, 'bad-dt']); | |
| 46 | expect(r.state).toBe(s); | |
| 47 | } | |
| 48 | }); | |
| 49 | ||
| 50 | test('serving is refused during order, twice to one guest, and with an unknown option or guest', () => { | |
| 51 | const s0 = start(5); | |
| 52 | const early = serve(s0, 0, exact(s0, 0)); | |
| 53 | expect([early.ok, early.why]).toEqual([false, 'still-ordering']); | |
| 54 | expect(early.state).toBe(s0); | |
| 55 | const s = listen(s0); | |
| 56 | const once = serve(s, 0, exact(s, 0)); | |
| 57 | expect(once.ok).toBe(true); | |
| 58 | const twice = serve(once.state, 0, exact(s, 0)); | |
| 59 | expect([twice.ok, twice.why]).toEqual([false, 'already-served']); | |
| 60 | expect(twice.state).toBe(once.state); | |
| 61 | for (const p of ['drink', 'milk', 'sugars', 'cake']) { | |
| 62 | const r = serve(s, 1, { ...exact(s, 1), [p]: 'nope' }); | |
| 63 | expect([r.ok, r.why]).toEqual([false, 'bad-order']); | |
| 64 | expect(r.state).toBe(s); | |
| 65 | } | |
| 66 | expect(serve(s, 1, { drink: 'tea' }).why).toBe('bad-order'); | |
| 67 | expect(serve(s, 1, null).why).toBe('bad-order'); | |
| 68 | expect(serve(s, 9, exact(s, 1)).why).toBe('no-such-guest'); | |
| 69 | expect(serve(s, 0.5, exact(s, 1)).why).toBe('no-such-guest'); | |
| 70 | }); | |
| 71 | ||
| 72 | test('each matching part scores the stated amount and a perfect cup earns the bonus', () => { | |
| 73 | const s = listen(start(11)); | |
| 74 | const perfect = serve(s, 0, exact(s, 0)).state; | |
| 75 | expect(perfect.score).toBe(4 * PART_POINTS + PERFECT_BONUS); | |
| 76 | expect(perfect.perfect).toBe(1); | |
| 77 | expect(perfect.last).toEqual({ guest: 0, matched: 4, perfect: true }); | |
| 78 | const two = serve(s, 0, wrongly(s, 0, 2)).state; | |
| 79 | expect(two.score).toBe(2 * PART_POINTS); | |
| 80 | expect(two.perfect).toBe(0); | |
| 81 | expect(two.wrong).toBe(2); | |
| 82 | }); | |
| 83 | ||
| 84 | test('a cleared round seats one more guest and begins the next, carrying score', () => { | |
| 85 | let s = listen(start(2)); | |
| 86 | for (let g = 0; g < s.guests; g++) s = serve(s, g, exact(s, g)).state; | |
| 87 | expect([s.round, s.guests, s.phase, s.status]).toEqual([2, START_GUESTS + 1, 'order', 'playing']); | |
| 88 | expect(s.served).toEqual([]); | |
| 89 | expect(s.score).toBe(START_GUESTS * (4 * PART_POINTS + PERFECT_BONUS)); | |
| 90 | expect(s.orders.length).toBe(s.guests); | |
| 91 | expect(MAX_GUESTS).toBe(START_GUESTS + LAST_ROUND - 1); | |
| 92 | }); | |
| 93 | ||
| 94 | test('the stated wrong parts in a round end the game with a loss line', () => { | |
| 95 | let s = listen(start(4)); | |
| 96 | s = serve(s, 0, wrongly(s, 0, MAX_WRONG)).state; | |
| 97 | expect(s.status).toBe('playing'); | |
| 98 | s = serve(s, 1, exact(s, 1)).state; | |
| 99 | expect(s.status).toBe('lost'); | |
| 100 | expect(s.line).toContain('You lose'); | |
| 101 | expect(serve(s, 0, exact(s, 0)).why).toBe('game-over'); | |
| 102 | expect(tick(s, 1).why).toBe('game-over'); | |
| 103 | }); | |
| 104 | ||
| 105 | test('fewer wrong parts than the limit let the round pass; clearing the last round wins with score and perfect cups', () => { | |
| 106 | let s = start(6); | |
| 107 | for (let r = 1; r <= LAST_ROUND; r++) { | |
| 108 | expect(s.round).toBe(r); | |
| 109 | s = listen(s); | |
| 110 | for (let g = 0; g < s.guests; g++) { | |
| 111 | const o = g === 0 && r === 1 ? wrongly(s, g, 1) : exact(s, g); | |
| 112 | s = serve(s, g, o).state; | |
| 113 | } | |
| 114 | } | |
| 115 | const guests = START_GUESTS + LAST_ROUND - 1; | |
| 116 | const total = (START_GUESTS + guests) * (LAST_ROUND) / 2; // arithmetic series of guests per round | |
| 117 | expect(s.status).toBe('won'); | |
| 118 | expect(s.perfect).toBe(total - 1); | |
| 119 | expect(s.score).toBe(total * 4 * PART_POINTS + (total - 1) * PERFECT_BONUS - PART_POINTS); | |
| 120 | expect(s.line).toBe(`You win: score ${s.score}, ${s.perfect} perfect cups`); | |
| 121 | }); |
new above
offline