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

Migrated from github.com/amirmushichge/pixel-plays

Flag Hoist rules: a tested `flag-hoist/hoist.js` that plays a growing signal-flag memory sequence from plain data #161

Closed opened this
Closed without merging

torque/pixel-plays:torque/145-flag-hoist-rules-a-tested-flag-hoist-hoi → main 2 commits fork 1 thread · 1 unresolved

  • Closed without merging. Nothing was applied to main.

Review

Sign in to review this change.
2 files changed +202 −0
flag-hoist/hoist.js added +85
@@ -0,0 +1,85 @@
1 'use strict';
2 // The Flag Hoist rules: the harbour master hoists a sequence of signal flags and lowers them; you hoist the same flags in
3 // the same order. Each round adds one flag and keeps the earlier ones. Plain data in, plain data out, no DOM, no timers,
4 // no Math.random: the sequence comes from the seed, and the clock is a number `t` the page passes in. A rejected
5 // hoist returns the very same state. The page draws what this decides.
6 //
7 // Times are in milliseconds. Round r (from 1) shows r flags.
8 (function (root) {
9 // Six flags, each named with a stated colour pattern (colours are CSS names; the page paints them).
10 const FLAGS = [
11 { id: 'alpha', name: 'Alpha', pattern: 'split vertically: white at the hoist, blue at the fly', colours: ['white', 'blue'] },
12 { id: 'bravo', name: 'Bravo', pattern: 'solid red swallowtail', colours: ['red'] },
13 { id: 'charlie', name: 'Charlie', pattern: 'five horizontal bands: blue, white, red, white, blue', colours: ['blue', 'white', 'red', 'white', 'blue'] },
14 { id: 'delta', name: 'Delta', pattern: 'three horizontal bands: yellow, blue, yellow', colours: ['yellow', 'blue', 'yellow'] },
15 { id: 'echo', name: 'Echo', pattern: 'two horizontal bands: blue over red', colours: ['blue', 'red'] },
16 { id: 'foxtrot', name: 'Foxtrot', pattern: 'white diamond on a red field', colours: ['white', 'red'] },
17 ];
18 const IDS = FLAGS.map(f => f.id);
19 const TARGET = 6; // longest sequence repeated that wins; below it loses
20 const SHOW_START = 900, SHOW_STEP = 60, SHOW_FLOOR = 360; // ms per flag in round 1, shaved per round, never below the floor
21 const GAP = 120; // ms of bare mast after each flag, so a repeated flag shows twice
22
23 const finite = v => (Number.isFinite(v) ? v : 0);
24 // a repeatable number in [0, 1) for this seed and slot
25 function noise(seed, n) {
26 let h = (Math.imul(finite(seed) | 0, 374761393) + Math.imul(n | 0, 668265263)) | 0;
27 h = Math.imul(h ^ (h >>> 13), 1274126177);
28 return ((h ^ (h >>> 16)) >>> 0) / 4294967296;
29 }
30 // The first n flag ids for a seed. Flag i depends on (seed, i) alone, so a longer sequence keeps every earlier flag.
31 function sequence(seed, n) {
32 seed = finite(seed) >>> 0;
33 n = Math.max(0, Math.floor(finite(n)));
34 return Array.from({ length: n }, (_, i) => IDS[Math.floor(noise(seed, i) * IDS.length)]);
35 }
36 // ms each flag is shown in a round: SHOW_START less SHOW_STEP per earlier round, floored at SHOW_FLOOR
37 const showMs = round => Math.max(SHOW_FLOOR, SHOW_START - SHOW_STEP * (Math.max(1, Math.floor(finite(round))) - 1));
38
39 function refresh(s) {
40 s.line = s.status === 'playing' ? `Round ${s.round}: ${s.pos} of ${s.sequence.length} hoisted`
41 : s.longest >= TARGET ? `Well hoisted: ${s.longest} flags repeated (target ${TARGET}), best ${s.best}`
42 : `Struck the colours: ${s.longest} flags repeated, short of ${TARGET}, best ${s.best}`;
43 return s;
44 }
45 // best: the longest sequence repeated in earlier games, carried by the caller (the page keeps it in storage)
46 function newGame(seed = 1, best = 0) {
47 seed = finite(seed) >>> 0;
48 best = Math.max(0, Math.floor(finite(best)));
49 return refresh({ seed, round: 1, pos: 0, sequence: sequence(seed, 1), status: 'playing', longest: 0, best, target: TARGET, line: '' });
50 }
51
52 // Hoist a flag. The next flag in order advances; finishing the round starts the next with one more flag; any other
53 // flag ends the game. A value that is not a flag is refused with why 'unknown-flag' and the state is unchanged. Returns { ok, why, state }; a finished game refuses with why 'over'.
54 function hoist(state, flag) {
55 if (state.status !== 'playing') return { ok: false, why: 'over', state };
56 if (!IDS.includes(flag)) return { ok: false, why: 'unknown-flag', state };
57 const s = Object.assign({}, state, { sequence: state.sequence.slice() });
58 if (flag !== s.sequence[s.pos]) {
59 s.status = 'over';
60 return { ok: true, why: '', state: refresh(s) };
61 }
62 s.pos++;
63 if (s.pos === s.sequence.length) { // round repeated in full
64 s.longest = s.sequence.length;
65 s.best = Math.max(s.best, s.longest);
66 s.round++; s.pos = 0;
67 s.sequence = sequence(s.seed, s.round);
68 }
69 return { ok: true, why: '', state: refresh(s) };
70 }
71
72 // Which flag is on the mast at time t into the showing of this round: its id, or null in a gap, before the start, or
73 // after the last flag. Each flag takes showMs(round) then GAP of bare mast.
74 function shown(state, t) {
75 t = finite(t);
76 if (t < 0) return null;
77 const ms = showMs(state.round), i = Math.floor(t / (ms + GAP));
78 return i < state.sequence.length && t - i * (ms + GAP) < ms ? state.sequence[i] : null;
79 }
80 // when the showing of this round is over, in ms
81 const showLength = state => state.sequence.length * (showMs(state.round) + GAP);
82
83 const api = { FLAGS, IDS, TARGET, SHOW_START, SHOW_STEP, SHOW_FLOOR, GAP, sequence, showMs, newGame, hoist, shown, showLength };
84 if (typeof module !== 'undefined') module.exports = api; else root.FlagHoist = api;
85 })(typeof window !== 'undefined' ? window : globalThis);
flag-hoist/hoist.test.mjs added +117
@@ -0,0 +1,117 @@
1 import { test, expect } from 'bun:test';
2 import { FLAGS, IDS, TARGET, SHOW_START, SHOW_FLOOR, GAP, sequence, showMs, newGame, hoist, shown, showLength } from './hoist.js';
3
4 // hoist a list of flags, expecting each to be accepted
5 const play = (s, flags) => flags.reduce((st, f) => { const r = hoist(st, f); expect(r.ok).toBe(true); return r.state; }, s);
6 // play the first n rounds through correctly
7 const through = (s, n) => { for (let r = 1; r <= n; r++) s = play(s, sequence(s.seed, r)); return s; };
8
9 test('there are six flags, each named with a colour pattern', () => {
10 expect(FLAGS).toHaveLength(6);
11 expect(new Set(IDS).size).toBe(6);
12 for (const f of FLAGS) { expect(f.name).toBeTruthy(); expect(f.pattern).toBeTruthy(); expect(f.colours.length).toBeGreaterThan(0); }
13 });
14
15 test('the same seed gives the same sequence, and other seeds differ', () => {
16 expect(sequence(7, 10)).toEqual(sequence(7, 10));
17 expect(sequence(7, 10).every(f => IDS.includes(f))).toBe(true);
18 expect(sequence(7, 12)).not.toEqual(sequence(8, 12));
19 });
20
21 test('a sequence grows by one flag each round and keeps its prefix', () => {
22 for (let r = 1; r < 15; r++) {
23 const a = sequence(3, r), b = sequence(3, r + 1);
24 expect(b).toHaveLength(a.length + 1);
25 expect(b.slice(0, r)).toEqual(a);
26 }
27 expect(newGame(3).sequence).toHaveLength(1);
28 });
29
30 test('a right hoist advances; finishing the round starts the next with one more flag', () => {
31 let s = newGame(5);
32 s = through(s, 1);
33 expect(s.round).toBe(2);
34 expect(s.pos).toBe(0);
35 expect(s.sequence).toEqual(sequence(5, 2));
36 const first = hoist(s, s.sequence[0]);
37 expect(first.state.pos).toBe(1);
38 expect(first.state.status).toBe('playing');
39 expect(s.pos).toBe(0); // the old state is untouched
40 });
41
42 test('a wrong hoist ends the game and a finished game refuses more', () => {
43 const s = through(newGame(5), 2);
44 const wrong = IDS.find(f => f !== s.sequence[0]);
45 const r = hoist(s, wrong);
46 expect(r.ok).toBe(true);
47 expect(r.state.status).toBe('over');
48 expect(r.state.longest).toBe(2);
49 const again = hoist(r.state, s.sequence[0]);
50 expect(again.ok).toBe(false);
51 expect(again.state).toBe(r.state);
52 expect(hoist(s, 'zulu').why).toBe('unknown-flag');
53 });
54
55 test('the showing time per flag shortens each round down to the floor', () => {
56 expect(showMs(1)).toBe(SHOW_START);
57 expect(showMs(2)).toBeLessThan(showMs(1));
58 expect(showMs(5)).toBeLessThan(showMs(4));
59 expect(showMs(100)).toBe(SHOW_FLOOR);
60 for (let r = 1; r < 30; r++) expect(showMs(r)).toBeGreaterThanOrEqual(SHOW_FLOOR);
61 });
62
63 test('the state says which flag is shown at time t', () => {
64 const s = through(newGame(9), 2); // round 3: three flags
65 const ms = showMs(3), seq = s.sequence;
66 expect(shown(s, -1)).toBeNull();
67 expect(shown(s, 0)).toBe(seq[0]);
68 expect(shown(s, ms - 1)).toBe(seq[0]);
69 expect(shown(s, ms)).toBeNull(); // bare mast in the gap
70 expect(shown(s, ms + GAP)).toBe(seq[1]);
71 expect(shown(s, 2 * (ms + GAP) + 1)).toBe(seq[2]);
72 expect(shown(s, showLength(s))).toBeNull();
73 });
74
75 test('the end reports the longest repeated and the best, with the win line at the target', () => {
76 const wrongFor = s => IDS.find(f => f !== s.sequence[s.pos]);
77 let s = through(newGame(2, 4), TARGET);
78 s = hoist(s, wrongFor(s)).state;
79 expect(s.longest).toBe(TARGET);
80 expect(s.best).toBe(TARGET);
81 expect(s.line).toContain('Well hoisted');
82 expect(s.line).toContain(`${TARGET} flags`);
83 });
84
85 test('below the target the loss line shows, and best keeps an earlier longer game', () => {
86 const wrongFor = s => IDS.find(f => f !== s.sequence[s.pos]);
87 let s = through(newGame(2, 8), TARGET - 1);
88 s = hoist(s, wrongFor(s)).state;
89 expect(s.longest).toBe(TARGET - 1);
90 expect(s.best).toBe(8);
91 expect(s.line).toContain('Struck the colours');
92 const none = hoist(newGame(2), wrongFor(newGame(2))).state;
93 expect(none.longest).toBe(0);
94 expect(none.line).toContain('Struck the colours');
95 });
96
97 test('newGame treats NaN, negative and fractional seed and best as plain whole numbers', () => {
98 expect(newGame(NaN, NaN)).toEqual(newGame(0, 0));
99 expect(newGame(NaN).sequence).toEqual(sequence(0, 1));
100 expect(newGame(1, -5).best).toBe(0);
101 expect(newGame(1, 3.9).best).toBe(3);
102 expect(newGame(2.7).seed).toBe(2);
103 expect(Number.isInteger(newGame(-3).seed) && newGame(-3).seed >= 0).toBe(true);
104 });
105
106 test('shown treats NaN as time zero', () => {
107 const s = newGame(4);
108 expect(shown(s, NaN)).toBe(shown(s, 0));
109 expect(shown(s, NaN)).toBe(s.sequence[0]);
110 });
111
112 test('hoist on a finished game says why over, even for a non-flag', () => {
113 const s = newGame(5);
114 const over = hoist(s, IDS.find(f => f !== s.sequence[0])).state;
115 expect(hoist(over, s.sequence[0])).toEqual({ ok: false, why: 'over', state: over });
116 expect(hoist(over, 'zulu').why).toBe('over');
117 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs