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

Migrated from github.com/amirmushichge/pixel-plays

Cloakroom rules: a tested `cloakroom/cloak.js` that plays a whole game from plain data #120

Merged opened this

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

Merged

vesper/pixel-plays:vesper/107-cloakroom-rules-a-tested-cloakroom-cloak → main 2 commits fork 2 threads · 2 unresolved

  • Merged. vesper/107-cloakroom-rules-a-tested-cloakroom-cloak was merged into main as 7d12e0d.

Review

Sign in to review this change.
2 files changed +276 −0
cloakroom/cloak.js added +92
@@ -0,0 +1,92 @@
1 'use strict';
2 // The cloakroom rules: a rail of coat tags, face down, turned two at a time to find the pairs. Three rounds
3 // (6, 8, 10 pairs), 90 seconds each, 12 mismatches allowed in all. Plain data in, plain data out, no DOM, no timers;
4 // randomness comes from the seed passed in. The page draws what this decides.
5 (function (root) {
6 const PAIRS = [6, 8, 10], ROUNDS = PAIRS.length;
7 const CLOCK = 90, MAX_MISMATCHES = 12, PAUSE = .8; // seconds per round; the most mismatches a win allows; seconds a mismatch stays shown
8 const RESTART_AFTER = .6;
9
10 const finite = v => (Number.isFinite(v) ? v : 0);
11 // a repeatable number in [0, 1) for this seed and slot
12 function noise(seed, n) {
13 let h = (Math.imul(finite(seed) | 0, 374761393) + Math.imul(n | 0, 668265263)) | 0;
14 h = Math.imul(h ^ (h >>> 13), 1274126177);
15 return ((h ^ (h >>> 16)) >>> 0) / 4294967296;
16 }
17 // a rail for this round: each face 0..pairs-1 twice, shuffled by the seed. A tag is { id, face, state: 'down' | 'up' | 'gone' }
18 function deal(round, seed = 0) {
19 const pairs = PAIRS[round - 1];
20 if (!pairs) return [];
21 const faces = [];
22 for (let f = 0; f < pairs; f++) faces.push(f, f);
23 for (let i = faces.length - 1; i > 0; i--) {
24 const j = Math.floor(noise(seed, round * 1000 + i) * (i + 1));
25 [faces[i], faces[j]] = [faces[j], faces[i]];
26 }
27 return faces.map((face, id) => ({ id, face, state: 'down' }));
28 }
29 function startRound(s, round) {
30 s.round = round; s.rail = deal(round, s.seed + round * 7919); s.pairsLeft = PAIRS[round - 1]; s.clock = CLOCK; s.up = []; s.pause = 0;
31 s.phase = 'play';
32 }
33 function newGame(best = null, seed = 0) {
34 const s = { phase: 'play', over: false, won: false, outcome: '', round: 1, rail: [], pairsLeft: 0, mismatches: 0, clock: CLOCK, up: [], pause: 0, timer: 0, best: typeof best === 'number' && Number.isFinite(best) && best >= 0 ? best : null, record: false, seed: finite(seed) >>> 0 };
35 startRound(s, 1);
36 return s;
37 }
38 function finish(s, won, outcome, events) {
39 s.phase = 'over'; s.over = true; s.won = won; s.outcome = outcome; s.timer = 0; s.pause = 0;
40 for (const i of s.up) s.rail[i].state = 'down'; // tags still shown turn back, so the rail and `up` agree
41 s.up = [];
outdated

!s.best treats best 0 (a perfect game) as unset, so a later worse win replaces it. Represent 'no best' as null and compare with s.best === null || ...; newGame's Math.max(0, finite(best)) needs the same treatment.

outdated

On a loss by mismatch the tags in s.up keep state 'up' while s.up is emptied; reset their state here.

42 if (won && (s.best === null || s.mismatches < s.best)) { s.best = s.mismatches; s.record = true; }
43 events.push({ type: won ? 'won' : 'lost', outcome });
44 }
45 // turn the tag with this id; returns the events it caused. Ignored when over, during a pause, or for a tag not face down.
46 function turn(s, id) {
47 const events = [];
48 if (s.over || s.phase !== 'play' || !Number.isInteger(id)) return events;
49 const tag = s.rail[id];
50 if (!tag || tag.state !== 'down') return events;
51 tag.state = 'up'; s.up.push(id);
52 events.push({ type: 'turn', id });
53 if (s.up.length < 2) return events;
54 const [a, b] = s.up.map(i => s.rail[i]);
55 if (a.face === b.face) {
56 a.state = b.state = 'gone'; s.up = []; s.pairsLeft--;
57 events.push({ type: 'match', ids: [a.id, b.id], face: a.face });
58 if (s.pairsLeft === 0) {
59 if (s.round >= ROUNDS) finish(s, true, 'won', events);
60 else { startRound(s, s.round + 1); events.push({ type: 'round', round: s.round }); }
61 }
62 } else {
63 s.mismatches++; s.phase = 'pause'; s.pause = PAUSE;
64 events.push({ type: 'mismatch', ids: [a.id, b.id], mismatches: s.mismatches });
65 if (s.mismatches > MAX_MISMATCHES) finish(s, false, 'lost: too many mismatches', events);
66 }
67 return events;
68 }
69 function advance(s, dt) {
70 const events = [];
71 dt = Math.max(0, Math.min(finite(dt), .25));
72 if (s.over) { s.timer += dt; return events; }
73 s.clock = Math.max(0, s.clock - dt);
74 if (s.phase === 'pause') {
75 s.pause = Math.max(0, s.pause - dt);
76 if (s.pause <= 0) {
77 for (const i of s.up) s.rail[i].state = 'down';
78 s.up = []; s.phase = 'play';
79 events.push({ type: 'turnback' });
80 }
81 }
82 if (s.clock <= 0) finish(s, false, 'lost: out of time', events);
83 return events;
84 }
85 // a tap after the game has been over for a moment starts the next one; otherwise null
86 function again(s) {
87 return s.over && s.timer > RESTART_AFTER ? newGame(s.best, s.seed + 1) : null;
88 }
89
90 const api = { PAIRS, ROUNDS, CLOCK, MAX_MISMATCHES, PAUSE, RESTART_AFTER, deal, newGame, turn, advance, again };
91 if (typeof module !== 'undefined') module.exports = api; else root.Cloak = api;
92 })(typeof window !== 'undefined' ? window : globalThis);
cloakroom/cloak.test.mjs added +184
@@ -0,0 +1,184 @@
1 import { test, expect } from 'bun:test';
2 import { PAIRS, CLOCK, MAX_MISMATCHES, PAUSE, deal, newGame, turn, advance, again } from './cloak.js';
3
4 const run = (s, seconds) => { for (let t = 0; t < seconds - 1e-9; t += 1 / 60) advance(s, 1 / 60); };
5 const partner = (s, id) => s.rail.find(t => t.id !== id && t.face === s.rail[id].face && t.state === 'down').id;
6 const stranger = (s, id) => s.rail.find(t => t.face !== s.rail[id].face && t.state === 'down').id;
7 const match = s => { const id = s.rail.find(t => t.state === 'down').id; turn(s, id); turn(s, partner(s, id)); };
8 const miss = s => { const id = s.rail.find(t => t.state === 'down').id; turn(s, id); turn(s, stranger(s, id)); run(s, PAUSE + .1); };
9 const clearRound = s => { const r = s.round; while (!s.over && s.round === r) match(s); };
10
11 test('the deal holds exactly the stated pairs, and the same seed deals the same rail', () => {
12 expect(PAIRS).toEqual([6, 8, 10]);
13 PAIRS.forEach((n, i) => {
14 const rail = deal(i + 1, 5);
15 expect(rail.length).toBe(n * 2);
16 for (let f = 0; f < n; f++) expect(rail.filter(t => t.face === f).length).toBe(2);
17 expect(rail.every(t => t.state === 'down')).toBe(true);
18 });
19 expect(deal(1, 5)).toEqual(deal(1, 5));
20 expect(deal(1, 5).map(t => t.face)).not.toEqual(deal(1, 6).map(t => t.face));
21 expect(newGame(null, 3).pairsLeft).toBe(6);
22 });
23
24 test('one tag then a second reveals both', () => {
25 const s = newGame(null, 1);
26 turn(s, 0);
27 expect(s.rail[0].state).toBe('up');
28 turn(s, partner(s, 0) === 1 ? 2 : 1);
29 expect(s.up.length).toBe(2);
30 });
31
32 test('a match leaves the rail', () => {
33 const s = newGame(null, 1);
34 turn(s, 0); turn(s, partner(s, 0));
35 expect(s.pairsLeft).toBe(5);
36 expect(s.rail.filter(t => t.state === 'gone').length).toBe(2);
37 expect(s.mismatches).toBe(0);
38 expect(s.phase).toBe('play');
39 });
40
41 test('a mismatch is counted and both tags turn back after the pause', () => {
42 const s = newGame(null, 1);
43 turn(s, 0); turn(s, stranger(s, 0));
44 expect(s.mismatches).toBe(1);
45 expect(s.phase).toBe('pause');
46 expect(s.pairsLeft).toBe(6);
47 run(s, PAUSE / 2);
48 expect(s.up.length).toBe(2);
49 run(s, PAUSE);
50 expect(s.phase).toBe('play');
51 expect(s.rail.every(t => t.state === 'down')).toBe(true);
52 });
53
54 test('a third turn during the pause is ignored', () => {
55 const s = newGame(null, 1);
56 turn(s, 0); const b = stranger(s, 0); turn(s, b);
57 const third = s.rail.find(t => t.state === 'down').id;
58 expect(turn(s, third)).toEqual([]);
59 expect(s.rail[third].state).toBe('down');
60 expect(s.up.length).toBe(2);
61 run(s, PAUSE + .1);
62 turn(s, third);
63 expect(s.rail[third].state).toBe('up');
64 });
65
66 test('turning a tag already up, gone, or not on the rail does nothing', () => {
67 const s = newGame(null, 1);
68 turn(s, 0);
69 expect(turn(s, 0)).toEqual([]);
70 expect(turn(s, 99)).toEqual([]);
71 expect(turn(s, 'x')).toEqual([]);
72 expect(s.up).toEqual([0]);
73 });
74
75 test('clearing a round deals the next, bigger rail with a fresh clock', () => {
76 const s = newGame(null, 2);
77 run(s, 10);
78 clearRound(s);
79 expect(s.round).toBe(2);
80 expect(s.pairsLeft).toBe(8);
81 expect(s.rail.length).toBe(16);
82 expect(s.clock).toBe(CLOCK);
83 clearRound(s);
84 expect(s.round).toBe(3);
85 expect(s.pairsLeft).toBe(10);
86 });
87
88 test('the clock counts down and running out loses', () => {
89 const s = newGame(null, 1);
90 run(s, 30);
91 expect(s.clock).toBeCloseTo(CLOCK - 30, 1);
92 expect(s.over).toBe(false);
93 run(s, CLOCK);
94 expect(s.over).toBe(true);
95 expect(s.won).toBe(false);
96 expect(s.phase).toBe('over');
97 expect(s.outcome).toContain('time');
98 });
99
100 test('clearing all three rounds with 12 mismatches wins', () => {
101 const s = newGame(null, 4);
102 for (let i = 0; i < MAX_MISMATCHES; i++) miss(s);
103 expect(s.over).toBe(false);
104 clearRound(s); clearRound(s); clearRound(s);
105 expect(s.over).toBe(true);
106 expect(s.won).toBe(true);
107 expect(s.outcome).toBe('won');
108 expect(s.mismatches).toBe(12);
109 expect(s.best).toBe(12);
110 expect(s.record).toBe(true);
111 });
112
113 test('a 13th mismatch loses on the spot', () => {
114 const s = newGame(null, 4);
115 for (let i = 0; i < MAX_MISMATCHES; i++) miss(s);
116 turn(s, 0); turn(s, stranger(s, 0));
117 expect(s.mismatches).toBe(13);
118 expect(s.over).toBe(true);
119 expect(s.won).toBe(false);
120 expect(s.outcome).toContain('mismatches');
121 });
122
123 test('a better game replaces best and a worse one does not; nothing is played after the end', () => {
124 const t = newGame(5, 1);
125 for (let i = 0; i < 7; i++) miss(t);
126 clearRound(t); clearRound(t); clearRound(t);
127 expect(t.best).toBe(5);
128 expect(t.record).toBe(false);
outdated

Add the case: a perfect win, then again, then a 7-mismatch win must leave best at 0 and record false. As written this block passes with the bug.

129 expect(turn(t, 0)).toEqual([]);
130 const u = newGame(5, 1);
131 for (let i = 0; i < 3; i++) miss(u);
132 clearRound(u); clearRound(u); clearRound(u);
133 expect(u.best).toBe(3);
134 expect(u.record).toBe(true);
135 });
136
137 test('a perfect game stays the best through a worse one', () => {
138 const s = newGame(null, 1);
139 expect(s.best).toBeNull();
140 clearRound(s); clearRound(s); clearRound(s);
141 expect(s.best).toBe(0);
142 expect(s.record).toBe(true);
143 run(s, 1);
144 const n = again(s);
145 expect(n.best).toBe(0);
146 for (let i = 0; i < 7; i++) miss(n);
147 clearRound(n); clearRound(n); clearRound(n);
148 expect(n.won).toBe(true);
149 expect(n.best).toBe(0);
150 expect(n.record).toBe(false);
151 });
152
153 test('the losing mismatch leaves no tag face up on the rail', () => {
154 const s = newGame(null, 4);
155 for (let i = 0; i < MAX_MISMATCHES; i++) miss(s);
156 turn(s, 0); turn(s, stranger(s, 0));
157 expect(s.over).toBe(true);
158 expect(s.up).toEqual([]);
159 expect(s.rail.some(t => t.state === 'up')).toBe(false);
160 });
161
162 test('the clock keeps running during a pause, and running out mid-pause loses', () => {
163 const s = newGame(null, 1);
164 run(s, CLOCK - .3);
165 turn(s, 0); turn(s, stranger(s, 0));
166 expect(s.phase).toBe('pause');
167 run(s, .4);
168 expect(s.over).toBe(true);
169 expect(s.won).toBe(false);
170 expect(s.outcome).toContain('time');
171 expect(s.rail.some(t => t.state === 'up')).toBe(false);
172 });
173
174 test('again starts a new game only after a moment', () => {
175 const s = newGame(null, 1);
176 expect(again(s)).toBeNull();
177 run(s, CLOCK + .3);
178 expect(again(s)).toBeNull();
179 run(s, .5);
180 const n = again(s);
181 expect(n.round).toBe(1);
182 expect(n.over).toBe(false);
183 expect(n.seed).toBe(2);
184 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs