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, with a test file that runs in under a second #254

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

torque/pixel-plays:torque/238-pigeon-post-rules-a-tested-pigeon-post-l → main 1 commit fork

  • Merged. torque/238-pigeon-post-rules-a-tested-pigeon-post-l was merged into main as 77b9861.

Review

Sign in to review this change.
2 files changed +232 −0
pigeon-post/loft.js added +115
@@ -0,0 +1,115 @@
1 'use strict';
2 // The Pigeon Post rules: pigeons fly out of the loft to villages one after another, then you 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 // A rejected call returns the very same state and says why. The page (not yet written) draws what this decides.
6 //
7 // Time is in seconds. Villages are numbered 0 to VILLAGES - 1. Round n shows a route of START_LENGTH + n - 1
8 // villages, each for pace(n) seconds; the route of a round is the first that many villages of one seeded route,
9 // so a longer round repeats the shorter one and adds a village at the end.
10 (function (root) {
11 const VILLAGES = 6; // villages a pigeon can fly to
12 const START_LENGTH = 3; // pigeons in the route of round one
13 const LAST_ROUND = 6; // finishing this round wins the game
14 const START_PACE = 1; // seconds each step is shown in round one
15 const PACE_STEP = 0.1; // seconds quicker per round
16 const PACE_FLOOR = 0.6; // seconds, the quickest a step is ever shown
17 const PIGEON_SCORE = 10; // points for each pigeon of a completed route
18 const MAX_LENGTH = START_LENGTH + LAST_ROUND - 1; // pigeons in the route of the last round
19
20 const finite = v => (Number.isFinite(v) ? v : 0);
21 // a repeatable number in [0, 1) for this seed and slot
22 function noise(seed, n) {
23 let h = (Math.imul(finite(seed) | 0, 374761393) + Math.imul(n | 0, 668265263)) | 0;
24 h = Math.imul(h ^ (h >>> 13), 1274126177);
25 return ((h ^ (h >>> 16)) >>> 0) / 4294967296;
26 }
27 const round3 = v => Math.round(v * 1000) / 1000;
28
29 // route(seed) -> the village ids of the longest route; round n uses its first lengthOf(n) ids
30 function route(seed) {
31 const out = [];
32 for (let i = 0; i < MAX_LENGTH; i++) out.push(Math.floor(noise(seed, i) * VILLAGES));
33 return out;
34 }
35 const lengthOf = round => START_LENGTH + round - 1;
36 // seconds each step is shown in a round: START_PACE, PACE_STEP quicker each round, never under PACE_FLOOR
37 const pace = round => round3(Math.max(PACE_FLOOR, START_PACE - PACE_STEP * (round - 1)));
38
39 function refresh(s) {
40 const len = lengthOf(s.round);
41 s.length = len;
42 s.pace = pace(s.round);
43 if (s.status === 'over') {
44 s.line = s.won
45 ? `All ${s.lastRound} rounds delivered, you win: longest route ${s.longest}, ${s.score} points`
46 : `Wrong village, you lose: longest route ${s.longest}, ${s.score} points`;
47 } else if (s.phase === 'show') {
48 s.line = `Round ${s.round} of ${s.lastRound}: watch ${len} pigeons`;
49 } else {
50 s.line = `Round ${s.round} of ${s.lastRound}: send pigeon ${s.sent + 1} of ${len}`;
51 }
52 return s;
53 }
54
55 const start = (seed = 1) => {
56 const sd = finite(seed) >>> 0;
57 return refresh({
58 seed: sd, villages: VILLAGES, lastRound: LAST_ROUND, pigeonScore: PIGEON_SCORE, route: route(sd),
59 round: 1, phase: 'show', time: 0, sent: 0, score: 0, longest: 0,
60 status: 'playing', won: false, length: 0, pace: 0, line: '',
61 });
62 };
63
64 // showing(state) -> { phase, step, village }: during show, the step being shown (0-based) and its village;
65 // during send or over, step is -1 and village is -1 (nothing is shown)
66 function showing(s) {
67 if (s.status !== 'playing' || s.phase !== 'show') return { phase: s.phase, step: -1, village: -1 };
68 const step = Math.min(lengthOf(s.round) - 1, Math.floor(s.time / pace(s.round) + 1e-9));
69 return { phase: 'show', step, village: s.route[step] };
70 }
71
72 const refuse = (s, why) => ({ ok: false, why, event: '', state: s });
73
74 // tick(state, dt) -> { ok, why, event, state }: dt seconds of the show pass. event is '' or 'send' (the show ended
75 // and it is your turn). Refused outside the show phase. dt must be a finite number above 0.
76 function tick(s, dt) {
77 if (s.status !== 'playing') return refuse(s, 'over');
78 if (s.phase !== 'show') return refuse(s, 'not-show');
79 if (typeof dt !== 'number' || !Number.isFinite(dt) || dt <= 0) return refuse(s, 'bad-dt');
80 const n = Object.assign({}, s);
81 n.time = round3(n.time + dt);
82 let event = '';
83 if (n.time >= lengthOf(n.round) * pace(n.round) - 1e-9) { n.phase = 'send'; n.time = 0; n.sent = 0; event = 'send'; }
84 return { ok: true, why: '', event, state: refresh(n) };
85 }
86
87 // send(state, village) -> { ok, why, event, state }: refused during the show. A right village gives event 'right';
88 // the last of a route gives 'round' (scores PIGEON_SCORE per pigeon and grows the route) or 'won' after the last
89 // round; a wrong village gives 'lost' and ends the game. longest is the longest route sent in full.
90 function send(s, village) {
91 if (s.status !== 'playing') return refuse(s, 'over');
92 if (s.phase !== 'send') return refuse(s, 'show');
93 if (!Number.isInteger(village) || village < 0 || village >= s.villages) return refuse(s, 'bad-village');
94 const n = Object.assign({}, s);
95 if (village !== n.route[n.sent]) {
96 n.status = 'over'; n.won = false;
97 return { ok: true, why: '', event: 'lost', state: refresh(n) };
98 }
99 n.sent += 1;
100 let event = 'right';
101 if (n.sent >= lengthOf(n.round)) {
102 n.score += lengthOf(n.round) * PIGEON_SCORE;
103 n.longest = lengthOf(n.round);
104 if (n.round >= n.lastRound) { n.status = 'over'; n.won = true; event = 'won'; }
105 else { n.round += 1; n.phase = 'show'; n.time = 0; n.sent = 0; event = 'round'; }
106 }
107 return { ok: true, why: '', event, state: refresh(n) };
108 }
109
110 const api = {
111 VILLAGES, START_LENGTH, LAST_ROUND, START_PACE, PACE_STEP, PACE_FLOOR, PIGEON_SCORE, MAX_LENGTH,
112 route, lengthOf, pace, start, showing, tick, send,
113 };
114 if (typeof module !== 'undefined') module.exports = api; else root.PigeonPost = api;
115 })(typeof window !== 'undefined' ? window : globalThis);
pigeon-post/loft.test.mjs added +117
@@ -0,0 +1,117 @@
1 import { test, expect } from 'bun:test';
2 import {
3 VILLAGES, START_LENGTH, LAST_ROUND, START_PACE, PACE_STEP, PACE_FLOOR, PIGEON_SCORE, MAX_LENGTH,
4 route, lengthOf, pace, start, showing, tick, send,
5 } from './loft.js';
6
7 // watch the whole show of the current round
8 const watch = s => tick(s, s.length * s.pace).state;
9 // send the whole route of the current round
10 const sendAll = s => {
11 let r;
12 const len = s.length;
13 for (let i = 0; i < len; i++) { r = send(s, s.route[i]); s = r.state; }
14 return r;
15 };
16
17 test('the same seed gives the same route, other seeds differ, and the stated sizes hold', () => {
18 expect(start(7)).toEqual(start(7));
19 expect(route(7)).toEqual(route(7));
20 const seen = new Set();
21 for (let seed = 1; seed <= 20; seed++) seen.add(route(seed).join());
22 expect(seen.size).toBeGreaterThan(10);
23 const s = start(3);
24 expect(s.route).toHaveLength(MAX_LENGTH);
25 expect(MAX_LENGTH).toBe(START_LENGTH + LAST_ROUND - 1);
26 for (const v of s.route) { expect(Number.isInteger(v)).toBe(true); expect(v).toBeGreaterThanOrEqual(0); expect(v).toBeLessThan(VILLAGES); }
27 expect([s.round, s.length, s.phase, s.villages, s.lastRound]).toEqual([1, START_LENGTH, 'show', VILLAGES, LAST_ROUND]);
28 });
29
30 test('showing reports the step and village being shown, then nothing once the send phase starts', () => {
31 let s = start(5);
32 expect(showing(s)).toEqual({ phase: 'show', step: 0, village: s.route[0] });
33 s = tick(s, START_PACE).state;
34 expect(showing(s)).toEqual({ phase: 'show', step: 1, village: s.route[1] });
35 s = tick(s, START_PACE * 0.5).state;
36 expect(showing(s).step).toBe(1);
37 const r = tick(s, START_PACE * 1.5);
38 expect(r.event).toBe('send');
39 expect(showing(r.state)).toEqual({ phase: 'send', step: -1, village: -1 });
40 });
41
42 test('the pace quickens by a step each round and stops at the floor', () => {
43 expect(pace(1)).toBe(START_PACE);
44 expect(pace(2)).toBeCloseTo(START_PACE - PACE_STEP, 9);
45 expect(pace(3)).toBeLessThan(pace(2));
46 expect(pace(LAST_ROUND)).toBe(PACE_FLOOR);
47 expect(pace(50)).toBe(PACE_FLOOR);
48 for (let r = 1; r < 20; r++) expect(pace(r + 1)).toBeLessThanOrEqual(pace(r));
49 // a show lasts length * pace seconds: just short of that it is still showing
50 const s = start(1);
51 expect(tick(s, 3 * START_PACE - 0.01).state.phase).toBe('show');
52 expect(tick(s, 3 * START_PACE).state.phase).toBe('send');
53 });
54
55 test('send is refused during the show and tick is refused during the send', () => {
56 const s = start(2);
57 const r = send(s, s.route[0]);
58 expect([r.ok, r.why, r.state]).toEqual([false, 'show', s]);
59 const w = watch(s);
60 const t = tick(w, 1);
61 expect([t.ok, t.why, t.state]).toEqual([false, 'not-show', w]);
62 expect(tick(s, 0).why).toBe('bad-dt');
63 expect(send(w, 99).why).toBe('bad-village');
64 expect(send(w, 1.5).why).toBe('bad-village');
65 });
66
67 test('a right send is accepted and the next village is then expected', () => {
68 const w = watch(start(4));
69 const r = send(w, w.route[0]);
70 expect([r.ok, r.event, r.state.sent, r.state.status]).toEqual([true, 'right', 1, 'playing']);
71 expect(r.state.line).toContain('pigeon 2 of 3');
72 });
73
74 test('a wrong send ends the game with a loss line naming the longest route sent', () => {
75 const w = watch(start(4));
76 const wrong = (w.route[0] + 1) % VILLAGES;
77 const r = send(w, wrong);
78 expect([r.ok, r.event, r.state.status, r.state.won, r.state.longest]).toEqual([true, 'lost', 'over', false, 0]);
79 expect(r.state.line).toBe('Wrong village, you lose: longest route 0, 0 points');
80 expect(send(r.state, 0).why).toBe('over');
81 });
82
83 test('a completed route scores per pigeon and grows the route by one for the next round', () => {
84 const r = sendAll(watch(start(9)));
85 expect(r.event).toBe('round');
86 const n = r.state;
87 expect([n.round, n.length, n.phase, n.score, n.longest, n.sent]).toEqual([2, lengthOf(2), 'show', START_LENGTH * PIGEON_SCORE, START_LENGTH, 0]);
88 expect(n.length).toBe(START_LENGTH + 1);
89 expect(n.pace).toBe(pace(2));
90 expect(n.route.slice(0, 3)).toEqual(start(9).route.slice(0, 3)); // the longer route repeats the shorter
91 // scoring is 3 * 10 then 4 * 10
92 const r2 = sendAll(watch(n));
93 expect(r2.state.score).toBe(30 + 40);
94 expect(r2.state.longest).toBe(4);
95 });
96
97 test('a wrong send after finished rounds reports the longest route sent', () => {
98 let s = sendAll(watch(start(6))).state;
99 s = sendAll(watch(s)).state;
100 const w = watch(s);
101 const r = send(w, (w.route[0] + 1) % VILLAGES);
102 expect(r.state.longest).toBe(START_LENGTH + 1);
103 expect(r.state.score).toBe(70);
104 expect(r.state.line).toBe('Wrong village, you lose: longest route 4, 70 points');
105 });
106
107 test('finishing every round wins, with a win line and the longest route', () => {
108 let s = start(11), r;
109 for (let round = 1; round <= LAST_ROUND; round++) { r = sendAll(watch(s)); s = r.state; }
110 expect([r.event, s.status, s.won, s.longest]).toEqual(['won', 'over', true, MAX_LENGTH]);
111 let total = 0;
112 for (let n = 1; n <= LAST_ROUND; n++) total += lengthOf(n) * PIGEON_SCORE;
113 expect(s.score).toBe(total);
114 expect(s.line).toBe(`All ${LAST_ROUND} rounds delivered, you win: longest route ${MAX_LENGTH}, ${total} points`);
115 expect(send(s, 0).why).toBe('over');
116 expect(tick(s, 1).why).toBe('over');
117 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs