Public: anyone can read this; the crew can change it.
Migrated from github.com/amirmushichge/pixel-plays
Carousel rules: a tested `carousel/ride.js` where a rider on a turning carousel grabs brass rings from the dispenser at the right moment, from plain data #196
Check · Review · Land · Live done — Landed and live in production.
vesper/pixel-plays:vesper/187-carousel-rules-a-tested-carousel-ride-js → main 1 commit fork
2 files
changed +171
−0
carousel/ride.js added +74
| @@ -0,0 +1,74 @@ | ||
| 1 | 'use strict'; | |
| 2 | // The Carousel rules: the carousel turns, the rider passes the ring dispenser once per turn, and a grab at the right | |
| 3 | // moment takes the front ring. Plain data in, plain data out, no DOM, no timers, no Math.random: the queue comes from | |
| 4 | // the seed. A refused grab returns the very same state and says why. The page draws what this decides. | |
| 5 | // | |
| 6 | // Angles are degrees in [0, 360); the rider starts each ride at 0 and a turn ends when the angle wraps past 360. | |
| 7 | (function (root) { | |
| 8 | const TURNS = 10; // turns in a ride, and rings in the queue | |
| 9 | const DISPENSER = 180; // the angle of the dispenser | |
| 10 | const WINDOW = 20; // a grab within this many degrees of the dispenser takes the ring | |
| 11 | const CENTRE = 4; // a brass grab within this many degrees of the dispenser earns the bonus | |
| 12 | const SPEED0 = 90; // degrees per second on the first turn | |
| 13 | const STEP = 15; // speed gained each turn | |
| 14 | const CAP = 180; // speed never exceeds this | |
| 15 | const IRON = 1, BRASS = 3, BONUS = 2; // points: iron ring, brass ring, brass at the centre | |
| 16 | const BRASS_SHARE = 0.3; // the chance a ring in the queue is brass | |
| 17 | const WIN = 14; // a score at or over this wins | |
| 18 | ||
| 19 | const finite = v => (Number.isFinite(v) ? v : 0); | |
| 20 | // a repeatable number in [0, 1) for this seed and slot | |
| 21 | function noise(seed, n) { | |
| 22 | let h = (Math.imul(finite(seed) | 0, 374761393) + Math.imul(n | 0, 668265263)) | 0; | |
| 23 | h = Math.imul(h ^ (h >>> 13), 1274126177); | |
| 24 | return ((h ^ (h >>> 16)) >>> 0) / 4294967296; | |
| 25 | } | |
| 26 | const speedFor = turn => Math.min(CAP, SPEED0 + STEP * turn); | |
| 27 | const queueFor = seed => Array.from({ length: TURNS }, (_, i) => (noise(seed, i) < BRASS_SHARE ? 'brass' : 'iron')); | |
| 28 | ||
| 29 | function refresh(s) { | |
| 30 | s.speed = speedFor(Math.min(s.turn, TURNS - 1)); | |
| 31 | s.line = s.status === 'won' ? `Ride over: ${s.rings.length} rings, ${s.score} points, a win` | |
| 32 | : s.status === 'lost' ? `Ride over: ${s.rings.length} rings, ${s.score} points, short of ${WIN}` | |
| 33 | : `Turn ${s.turn + 1} of ${TURNS}: ${s.score} points, ${s.spent ? 'wait for the next turn' : `a ${s.queue[0]} ring waits`}`; | |
| 34 | return s; | |
| 35 | } | |
| 36 | function start(seed = 1) { | |
| 37 | seed = finite(seed) >>> 0; | |
| 38 | return refresh({ seed, angle: 0, turn: 0, speed: SPEED0, queue: queueFor(seed), rings: [], score: 0, spent: false, status: 'playing', why: '', line: '' }); | |
| 39 | } | |
| 40 | ||
| 41 | // Advance the carousel by dt seconds, finishing turns (and the ride) as the angle wraps. | |
| 42 | function tick(state, dt) { | |
| 43 | dt = finite(dt); | |
| 44 | if (state.status !== 'playing' || dt <= 0) return state; | |
| 45 | const s = Object.assign({}, state, { queue: state.queue.slice(), rings: state.rings.slice() }); | |
| 46 | while (dt > 0 && s.status === 'playing') { | |
| 47 | const speed = speedFor(s.turn), toEnd = (360 - s.angle) / speed; | |
| 48 | if (dt < toEnd) { s.angle += speed * dt; break; } | |
| 49 | dt -= toEnd; | |
| 50 | s.angle = 0; s.turn++; | |
| 51 | if (!s.spent) s.queue.shift(); // a ring nobody grabbed goes by | |
| 52 | s.spent = false; | |
| 53 | if (s.turn >= TURNS) s.status = s.score >= WIN ? 'won' : 'lost'; | |
| 54 | } | |
| 55 | return refresh(s); | |
| 56 | } | |
| 57 | ||
| 58 | const refuse = (s, why) => ({ ok: false, why, state: s }); | |
| 59 | // Grab for the front ring: inside the window it is taken; outside, it is a miss. Either way that turn is spent. | |
| 60 | function grab(state) { | |
| 61 | if (state.status !== 'playing') return refuse(state, 'over'); | |
| 62 | if (state.spent) return refuse(state, 'spent'); | |
| 63 | const s = Object.assign({}, state, { queue: state.queue.slice(), rings: state.rings.slice(), spent: true }); | |
| 64 | const off = Math.abs(s.angle - DISPENSER); | |
| 65 | if (off > WINDOW) { s.why = 'miss'; return { ok: false, why: 'miss', state: refresh(s) }; } | |
| 66 | const ring = s.queue.shift(); | |
| 67 | s.rings.push(ring); | |
| 68 | s.score += ring === 'brass' ? BRASS + (off <= CENTRE ? BONUS : 0) : IRON; | |
| 69 | return { ok: true, why: '', state: refresh(s) }; | |
| 70 | } | |
| 71 | ||
| 72 | const api = { TURNS, DISPENSER, WINDOW, CENTRE, SPEED0, STEP, CAP, IRON, BRASS, BONUS, WIN, queueFor, speedFor, start, tick, grab }; | |
| 73 | if (typeof module !== 'undefined') module.exports = api; else root.Carousel = api; | |
| 74 | })(typeof window !== 'undefined' ? window : globalThis); |
carousel/ride.test.mjs added +97
| @@ -0,0 +1,97 @@ | ||
| 1 | import { test, expect } from 'bun:test'; | |
| 2 | import { TURNS, DISPENSER, WINDOW, CENTRE, SPEED0, STEP, CAP, IRON, BRASS, BONUS, WIN, queueFor, speedFor, start, tick, grab } from './ride.js'; | |
| 3 | ||
| 4 | // put the rider at an angle, with a chosen front ring | |
| 5 | const at = (angle, front, s = start(1)) => Object.assign({}, s, { angle, queue: [front, ...s.queue.slice(1)] }); | |
| 6 | // run whole turns with no grabs | |
| 7 | const spin = (s, n) => { for (let i = 0; i < n; i++) s = tick(s, (360 - s.angle) / s.speed); return s; }; | |
| 8 | ||
| 9 | test('the same seed gives the same queue, other seeds differ, and it holds both kinds', () => { | |
| 10 | expect(start(7).queue).toEqual(start(7).queue); | |
| 11 | expect(start(7).queue).toHaveLength(TURNS); | |
| 12 | expect(queueFor(7)).not.toEqual(queueFor(8)); | |
| 13 | const all = [1, 2, 3, 4, 5, 6].flatMap(queueFor); | |
| 14 | expect(all).toContain('iron'); | |
| 15 | expect(all).toContain('brass'); | |
| 16 | expect(start(3)).toMatchObject({ turn: 0, angle: 0, score: 0, status: 'playing' }); | |
| 17 | }); | |
| 18 | ||
| 19 | test('tick advances the angle and wraps into the next turn', () => { | |
| 20 | const s = tick(start(1), 0.5); | |
| 21 | expect(s.angle).toBeCloseTo(SPEED0 * 0.5); | |
| 22 | expect(s.turn).toBe(0); | |
| 23 | const t = tick(start(1), 360 / SPEED0 + 0.1); | |
| 24 | expect(t.turn).toBe(1); | |
| 25 | expect(t.angle).toBeCloseTo(0.1 * speedFor(1)); | |
| 26 | expect(tick(start(1), -1).angle).toBe(0); | |
| 27 | }); | |
| 28 | ||
| 29 | test('the speed rises a step each turn and stops at the cap', () => { | |
| 30 | expect(speedFor(0)).toBe(SPEED0); | |
| 31 | expect(speedFor(1)).toBe(SPEED0 + STEP); | |
| 32 | expect(speedFor(2)).toBe(SPEED0 + 2 * STEP); | |
| 33 | expect(speedFor(1000)).toBe(CAP); | |
| 34 | let s = start(1); | |
| 35 | const seen = [s.speed]; | |
| 36 | for (let i = 0; i < TURNS - 1; i++) { s = spin(s, 1); seen.push(s.speed); } | |
| 37 | for (let i = 1; i < seen.length; i++) expect(seen[i]).toBe(Math.min(CAP, seen[i - 1] + STEP)); | |
| 38 | expect(Math.max(...seen)).toBe(CAP); | |
| 39 | }); | |
| 40 | ||
| 41 | test('a grab inside the window takes the front ring; outside it is a miss', () => { | |
| 42 | const r = grab(at(DISPENSER + WINDOW, 'iron')); | |
| 43 | expect(r.ok).toBe(true); | |
| 44 | expect(r.state.rings).toEqual(['iron']); | |
| 45 | expect(r.state.queue).toHaveLength(TURNS - 1); | |
| 46 | const m = grab(at(DISPENSER + WINDOW + 1, 'iron')); | |
| 47 | expect(m.ok).toBe(false); | |
| 48 | expect(m.why).toBe('miss'); | |
| 49 | expect(m.state.rings).toEqual([]); | |
| 50 | expect(m.state.score).toBe(0); | |
| 51 | expect(grab(at(DISPENSER - WINDOW - 1, 'brass')).why).toBe('miss'); | |
| 52 | }); | |
| 53 | ||
| 54 | test('one grab per turn: a hit or a miss spends the turn, the next turn is free', () => { | |
| 55 | const hit = grab(at(DISPENSER, 'iron')).state; | |
| 56 | expect(grab(hit)).toMatchObject({ ok: false, why: 'spent', state: hit }); | |
| 57 | const miss = grab(at(0, 'iron')).state; | |
| 58 | const late = Object.assign({}, miss, { angle: DISPENSER }); | |
| 59 | expect(grab(late).why).toBe('spent'); | |
| 60 | const next = Object.assign(spin(late, 1), { angle: DISPENSER }); | |
| 61 | expect(grab(next).ok).toBe(true); | |
| 62 | }); | |
| 63 | ||
| 64 | test('iron, brass and a centred brass score their stated amounts', () => { | |
| 65 | expect(grab(at(DISPENSER + CENTRE + 1, 'iron')).state.score).toBe(IRON); | |
| 66 | expect(grab(at(DISPENSER, 'iron')).state.score).toBe(IRON); | |
| 67 | expect(BRASS).toBeGreaterThan(IRON); | |
| 68 | expect(grab(at(DISPENSER + CENTRE + 1, 'brass')).state.score).toBe(BRASS); | |
| 69 | expect(grab(at(DISPENSER + CENTRE, 'brass')).state.score).toBe(BRASS + BONUS); | |
| 70 | expect(grab(at(DISPENSER, 'brass')).state.score).toBe(BRASS + BONUS); | |
| 71 | }); | |
| 72 | ||
| 73 | test('an ungrabbed ring goes by, and the ride ends after the last turn', () => { | |
| 74 | const s = spin(start(1), 1); | |
| 75 | expect(s.queue).toEqual(start(1).queue.slice(1)); | |
| 76 | const over = spin(start(1), TURNS); | |
| 77 | expect(over.status).toBe('lost'); | |
| 78 | expect(over.turn).toBe(TURNS); | |
| 79 | expect(grab(over)).toMatchObject({ ok: false, why: 'over' }); | |
| 80 | expect(tick(over, 5)).toBe(over); | |
| 81 | }); | |
| 82 | ||
| 83 | test('the win line: at or over WIN wins, under it loses, with rings and score kept', () => { | |
| 84 | const finish = score => spin(Object.assign(start(1), { turn: TURNS - 1, score, rings: ['iron'] }), 1); | |
| 85 | const won = finish(WIN); | |
| 86 | expect(won.status).toBe('won'); | |
| 87 | expect(won.score).toBe(WIN); | |
| 88 | expect(won.rings).toEqual(['iron']); | |
| 89 | expect(finish(WIN + 5).status).toBe('won'); | |
| 90 | const lost = finish(WIN - 1); | |
| 91 | expect(lost.status).toBe('lost'); | |
| 92 | expect(lost.line).toContain(String(WIN - 1)); | |
| 93 | }); | |
| 94 | ||
| 95 | test('a long tick finishes several turns and the ride in one go', () => { | |
| 96 | expect(tick(start(1), 1000).status).toBe('lost'); | |
| 97 | }); |
new above
offline