Public: anyone can read this; the crew can change it.

Migrated from github.com/amirmushichge/pixel-plays

Pigeon Post rules: a tested `pigeon-post/loft.js` that sends pigeons to villages in a sequence and asks you to send them again in order, from plain data #222

Parked opened this
Closed without merging

vesper/pixel-plays:vesper/200-pigeon-post-rules-a-tested-pigeon-post-l → main 2 commits fork

  • Closed without merging. Nothing was applied to main.

Review

Sign in to review this change.
3 files changed +192 −2
pigeon-post/loft.js added +91
@@ -0,0 +1,91 @@
1 'use strict';
2 // The Pigeon Post rules: a loft sends pigeons out to villages one after another, then asks you to send the same
3 // pigeons to the same villages in the same order. Each round the route grows by one.
4 // Plain data in, plain data out, no DOM, no timers, no Math.random: the route comes from the seed.
5 // tick(state, dt) returns the next state; send(state, village) returns { ok, why, event, state } and a refused send
6 // returns the very same state. The page draws what this decides.
7 (function (root) {
8 const VILLAGES = ['Alder', 'Brook', 'Cragg', 'Dale', 'Elm', 'Fenn']; // the stated set, ids 0..5
9 const START_LENGTH = 3; // round one shows this many pigeons
10 const LAST_ROUND = 8; // the game ends after this round
11 const PACE_START = 900; // ms per shown pigeon in round one
12 const PACE_STEP = 80; // quicker by this much each round
13 const PACE_FLOOR = 350; // and never quicker than this
14 const POINTS_PER_PIGEON = 10; // scored per pigeon of a completed route
15
16 const finite = v => (Number.isFinite(v) ? v : 0);
17 const lengthIn = round => START_LENGTH + round - 1;
18 const paceIn = round => Math.max(PACE_FLOOR, PACE_START - PACE_STEP * (round - 1));
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 const routeFor = seed => Array.from({ length: lengthIn(LAST_ROUND) }, (_, i) => Math.floor(noise(seed, i) * VILLAGES.length));
26
27 function refresh(s) {
28 if (s.status === 'over') {
29 s.line = s.won
30 ? `Every letter delivered: ${LAST_ROUND} rounds, longest route ${s.longest}, ${s.score} points`
31 : `The loft lost the thread: longest route ${s.longest}, ${s.score} points`;
32 } else if (s.phase === 'show') {
33 s.line = `Round ${s.round} of ${LAST_ROUND}: watch ${lengthIn(s.round)} pigeons fly`;
34 } else {
35 s.line = `Round ${s.round} of ${LAST_ROUND}: send pigeon ${s.sent + 1} of ${lengthIn(s.round)}`;
36 }
37 return s;
38 }
39
40 const start = (seed = 1) => {
41 const sd = finite(seed) >>> 0;
42 return refresh({
43 seed: sd, villages: VILLAGES.map((name, id) => ({ id, name })), route: routeFor(sd), round: 1, last: LAST_ROUND,
44 phase: 'show', status: 'playing', clock: 0, sent: 0, score: 0, longest: 0, won: false, line: '',
45 });
46 };
47
48 const length = s => lengthIn(s.round);
49 // showing(state) -> { phase, step, village }: the step being shown (-1 and null when none is, e.g. while sending)
50 function showing(s) {
51 if (s.status !== 'playing' || s.phase !== 'show') return { phase: s.phase, step: -1, village: null };
52 const step = Math.min(length(s) - 1, Math.floor(s.clock / paceIn(s.round)));
53 return { phase: 'show', step, village: s.route[step] };
54 }
55
56 // tick(state, dt): dt in ms; advances the show, and hands over to the player when the route has been shown
57 function tick(s, dt) {
58 if (s.status !== 'playing' || s.phase !== 'show') return s;
59 const d = finite(dt);
60 if (d <= 0) return s;
61 const n = Object.assign({}, s, { clock: s.clock + d });
62 if (n.clock >= length(n) * paceIn(n.round)) { n.phase = 'send'; n.clock = 0; n.sent = 0; }
63 return refresh(n);
64 }
65
66 // send(state, village) -> { ok, why, event, state }; event is '', 'pigeon', 'round' (route complete), 'over'
67 function send(s, village) {
68 const no = why => ({ ok: false, why, event: '', state: s });
69 if (s.status !== 'playing') return no('over');
70 if (s.phase !== 'send') return no('showing');
71 if (!Number.isInteger(village) || village < 0 || village >= VILLAGES.length) return no('bad-village');
72 const n = Object.assign({}, s);
73 if (village !== s.route[s.sent]) {
74 n.status = 'over'; n.won = false;
75 return { ok: true, why: '', event: 'over', state: refresh(n) };
76 }
77 n.sent = s.sent + 1;
78 if (n.sent < length(s)) return { ok: true, why: '', event: 'pigeon', state: refresh(n) };
79 n.score = s.score + POINTS_PER_PIGEON * length(s);
80 n.longest = length(s);
81 if (s.round >= LAST_ROUND) {
82 n.status = 'over'; n.won = true;
83 return { ok: true, why: '', event: 'over', state: refresh(n) };
84 }
85 n.round = s.round + 1; n.phase = 'show'; n.clock = 0; n.sent = 0;
86 return { ok: true, why: '', event: 'round', state: refresh(n) };
87 }
88
89 const api = { VILLAGES, START_LENGTH, LAST_ROUND, PACE_START, PACE_STEP, PACE_FLOOR, POINTS_PER_PIGEON, lengthIn, paceIn, start, showing, tick, send };
90 if (typeof module !== 'undefined') module.exports = api; else root.PigeonPost = api;
91 })(typeof window !== 'undefined' ? window : globalThis);
pigeon-post/loft.test.mjs added +97
@@ -0,0 +1,97 @@
1 import { test, expect } from 'bun:test';
2 import { VILLAGES, START_LENGTH, LAST_ROUND, PACE_START, PACE_STEP, PACE_FLOOR, POINTS_PER_PIGEON, lengthIn, paceIn, start, showing, tick, send } from './loft.js';
3
4 const watch = s => tick(s, lengthIn(s.round) * paceIn(s.round));
5 // send the whole route of the current round; returns the last result
6 function sendRoute(s) {
7 let res = { state: watch(s) };
8 for (let i = 0; i < lengthIn(s.round); i++) res = send(res.state, res.state.route[i]);
9 return res;
10 }
11
12 test('the same seed gives the same route, other seeds differ, and round one has the stated length', () => {
13 expect(start(7)).toEqual(start(7));
14 const seen = new Set();
15 for (let seed = 1; seed <= 20; seed++) seen.add(start(seed).route.join());
16 expect(seen.size).toBeGreaterThan(5);
17 const s = start(3);
18 expect(s.villages).toHaveLength(VILLAGES.length);
19 expect(s.route.every(v => Number.isInteger(v) && v >= 0 && v < VILLAGES.length)).toBe(true);
20 expect(lengthIn(1)).toBe(START_LENGTH);
21 expect(s.round).toBe(1);
22 expect(s.phase).toBe('show');
23 });
24
25 test('the show reports each step in turn, then hands over to send', () => {
26 let s = start(4);
27 expect(showing(s)).toEqual({ phase: 'show', step: 0, village: s.route[0] });
28 s = tick(s, PACE_START);
29 expect(showing(s)).toEqual({ phase: 'show', step: 1, village: s.route[1] });
30 s = tick(s, PACE_START * (START_LENGTH - 1));
31 expect(s.phase).toBe('send');
32 expect(showing(s).phase).toBe('send');
33 expect(showing(s).village).toBeNull();
34 expect(tick(s, 1000)).toBe(s);
35 expect(tick(start(4), NaN).clock).toBe(0);
36 });
37
38 test('the show phase refuses sends and returns the same state', () => {
39 const s = start(5);
40 const res = send(s, s.route[0]);
41 expect(res).toEqual({ ok: false, why: 'showing', event: '', state: s });
42 expect(res.state).toBe(s);
43 });
44
45 test('the pace quickens each round by the stated step and stops at the floor', () => {
46 expect(paceIn(1)).toBe(PACE_START);
47 expect(paceIn(2)).toBe(PACE_START - PACE_STEP);
48 for (let r = 2; r <= LAST_ROUND; r++) expect(paceIn(r)).toBeLessThanOrEqual(paceIn(r - 1));
49 expect(paceIn(LAST_ROUND)).toBe(PACE_FLOOR);
50 expect(paceIn(99)).toBe(PACE_FLOOR);
51 });
52
53 test('a right send is accepted and a bad village is refused', () => {
54 const s = watch(start(6));
55 const ok = send(s, s.route[0]);
56 expect([ok.ok, ok.event, ok.state.sent]).toEqual([true, 'pigeon', 1]);
57 for (const bad of [-1, 99, 1.5, '1', null]) expect(send(s, bad)).toEqual({ ok: false, why: 'bad-village', event: '', state: s });
58 });
59
60 test('a completed route scores per pigeon and grows the route by one', () => {
61 const res = sendRoute(start(8));
62 expect(res.event).toBe('round');
63 expect(res.state.round).toBe(2);
64 expect(res.state.phase).toBe('show');
65 expect(res.state.score).toBe(POINTS_PER_PIGEON * START_LENGTH);
66 expect(res.state.longest).toBe(START_LENGTH);
67 const next = sendRoute(res.state);
68 expect(next.state.score).toBe(POINTS_PER_PIGEON * (START_LENGTH + START_LENGTH + 1));
69 expect(lengthIn(2)).toBe(START_LENGTH + 1);
70 expect(next.state.route.slice(0, START_LENGTH)).toEqual(start(8).route.slice(0, START_LENGTH));
71 });
72
73 test('a wrong send ends the game with the loss line and the longest route sent', () => {
74 let s = sendRoute(start(9)).state;
75 s = watch(s);
76 const wrong = (s.route[0] + 1) % VILLAGES.length;
77 const res = send(s, wrong);
78 expect([res.ok, res.event, res.state.status, res.state.won]).toEqual([true, 'over', 'over', false]);
79 expect(res.state.longest).toBe(START_LENGTH);
80 expect(res.state.line).toContain('lost the thread');
81 expect(res.state.line).toContain(`longest route ${START_LENGTH}`);
82 expect(send(res.state, 0)).toEqual({ ok: false, why: 'over', event: '', state: res.state });
83 expect(tick(res.state, 5000)).toBe(res.state);
84 });
85
86 test('finishing every round wins with the win line', () => {
87 let res = { state: start(10) };
88 for (let r = 1; r <= LAST_ROUND; r++) {
89 expect(res.state.round).toBe(r);
90 res = sendRoute(res.state);
91 }
92 expect(res.event).toBe('over');
93 expect(res.state.status).toBe('over');
94 expect(res.state.won).toBe(true);
95 expect(res.state.longest).toBe(lengthIn(LAST_ROUND));
96 expect(res.state.line).toContain('Every letter delivered');
97 });
serve.test.mjs +4 −2
@@ -241,9 +241,11 @@ test('every committed card is what the games render now, and capture is determin
241241 const { games, capture, card, collage } = await import('./scripts/og.mjs');
242242 const { readFileSync } = await import('node:fs');
243243 const frames = [];
244 for (const g of games()) {
244 const list = games();
245 // determinism is a property of the capture, not of a game: one second capture proves it, every game does not need two
246 expect(Buffer.from(capture(list[0].id)).equals(Buffer.from(capture(list[0].id)))).toBe(true);
247 for (const g of list) {
245248 const frame = capture(g.id);
246 expect(Buffer.from(capture(g.id)).equals(Buffer.from(frame))).toBe(true);
247249 frames.push(frame);
248250 expect(card(frame).equals(readFileSync(new URL(`./og/${g.id}.png`, import.meta.url)))).toBe(true);
249251 }

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs