Public: anyone can read this; the crew can change it.
Migrated from github.com/amirmushichge/pixel-plays
Chestnut Roast rules: a tested `chestnut-roast/pan.js` where you catch roasted chestnuts in a paper cone and dodge the burnt ones, from plain data, in a two-file pull with a test file that runs in under a second #305
Check · Review · Land · Live done — Landed and live in production.
torque/pixel-plays:torque/277-chestnut-roast-rules-a-tested-chestnut-r → main 1 commit fork
2 files
changed +343
−0
chestnut-roast/pan.js added +156
| @@ -0,0 +1,156 @@ | ||
| 1 | 'use strict'; | |
| 2 | // The Chestnut Roast rules: chestnuts pop out of a vendor's pan in arcs; slide a paper cone along the pavement to | |
| 3 | // catch the roasted ones and let the burnt ones fall. Plain data in, plain data out, no DOM, no timers, no | |
| 4 | // Math.random: every pop comes from the seed. A rejected call returns the very same state and says why. | |
| 5 | // | |
| 6 | // Units: metres and seconds. x runs along the pavement from 0 to WIDTH; height is measured from the pavement. | |
| 7 | // A chestnut leaves the pan (x = PAN_X, height PAN_HEIGHT) at launch speed v and angle a from the vertical: | |
| 8 | // x(τ) = PAN_X + v·sin(a)·τ, h(τ) = PAN_HEIGHT + v·cos(a)·τ − g·τ²/2 (projectile motion, τ = time since launch) | |
| 9 | // It lands when h = 0: τ = (vy + sqrt(vy² + 2·g·PAN_HEIGHT)) / g, with vy = v·cos(a). Both are closed-form in τ, so | |
| 10 | // where and when a chestnut lands never depends on how time was sliced into ticks. | |
| 11 | (function (root) { | |
| 12 | const WIDTH = 10; // m, the pavement runs 0..WIDTH | |
| 13 | const PAN_X = 5; // m, where the pan sits | |
| 14 | const PAN_HEIGHT = 1; // m above the pavement | |
| 15 | const GRAVITY = 9.8; // m/s² | |
| 16 | const CONE_WIDTH = 1.6; // m, a chestnut landing within half this of the cone's centre is caught | |
| 17 | const CONE_SPEED = 6; // m/s, the cone's top speed toward its target | |
| 18 | const POPS = 120; // chestnuts in the schedule | |
| 19 | const SPEED_MIN = 3, SPEED_MAX = 5; // m/s launch speed | |
| 20 | const ANGLE_MAX = 0.9; // rad, |angle| from vertical is between 0.1 and this | |
| 21 | const BURNT_SHARE = 0.25; // chance a pop is burnt | |
| 22 | const FIRST_AT = 1; // s, the first pop | |
| 23 | const START_GAP = 1.2; // s between pops at the start | |
| 24 | const QUICKEN_EVERY = 3; // catches per quickening | |
| 25 | const QUICKEN_STEP = 0.1; // s taken off the gap each time | |
| 26 | const MIN_GAP = 0.4; // s, the floor of the gap | |
| 27 | const LIVES = 3; | |
| 28 | const CATCH_POINTS = 10; // score per roasted chestnut caught | |
| 29 | const WIN_CATCHES = 15; // roasted catches that win | |
| 30 | ||
| 31 | // mulberry32 as a pure step: next(seed) -> [value in [0,1), nextSeed] | |
| 32 | function next(r) { | |
| 33 | r = (r + 0x6D2B79F5) >>> 0; | |
| 34 | let t = r; | |
| 35 | t = Math.imul(t ^ (t >>> 15), t | 1); | |
| 36 | t ^= t + Math.imul(t ^ (t >>> 7), t | 61); | |
| 37 | return [((t ^ (t >>> 14)) >>> 0) / 4294967296, r]; | |
| 38 | } | |
| 39 | ||
| 40 | // gap(catches) -> seconds between pops after that many catches, never below MIN_GAP | |
| 41 | function gap(catches) { | |
| 42 | return Math.max(MIN_GAP, START_GAP - QUICKEN_STEP * Math.floor(catches / QUICKEN_EVERY)); | |
| 43 | } | |
| 44 | ||
| 45 | // flight(pop) -> seconds from launch to the pavement | |
| 46 | function flight(pop) { | |
| 47 | const vy = pop.speed * Math.cos(pop.angle); | |
| 48 | return (vy + Math.sqrt(vy * vy + 2 * GRAVITY * PAN_HEIGHT)) / GRAVITY; | |
| 49 | } | |
| 50 | ||
| 51 | // chestnutAt(pop, time) -> { x, h } at an absolute time (before launch it sits in the pan) | |
| 52 | function chestnutAt(pop, time) { | |
| 53 | const t = Math.min(Math.max(time - pop.at, 0), flight(pop)); | |
| 54 | const vy = pop.speed * Math.cos(pop.angle); | |
| 55 | return { x: PAN_X + pop.speed * Math.sin(pop.angle) * t, h: Math.max(0, PAN_HEIGHT + vy * t - GRAVITY * t * t / 2) }; | |
| 56 | } | |
| 57 | ||
| 58 | const landsAt = pop => pop.at + flight(pop); | |
| 59 | const landsX = pop => chestnutAt(pop, landsAt(pop)).x; | |
| 60 | ||
| 61 | // coneAt(cone, dt) -> the cone's centre after travelling toward its target for dt seconds at top speed | |
| 62 | function coneAt(cone, dt) { | |
| 63 | const d = cone.target - cone.x, step = CONE_SPEED * dt; | |
| 64 | return Math.abs(d) <= step ? cone.target : cone.x + Math.sign(d) * step; | |
| 65 | } | |
| 66 | ||
| 67 | function describe(s) { | |
| 68 | if (s.status === 'won') return `You win: score ${s.score}, best streak ${s.best}`; | |
| 69 | if (s.status === 'lost') return `You lose: ${s.lives <= 0 ? 'no lives left' : 'the pan ran dry'}, score ${s.score}`; | |
| 70 | return `${s.lives} ${s.lives === 1 ? 'life' : 'lives'}, score ${s.score}, streak ${s.streak}`; | |
| 71 | } | |
| 72 | ||
| 73 | // start(seed) -> a pan with a cone in the middle and the whole seeded schedule of pops | |
| 74 | function start(seed) { | |
| 75 | const sd = Number.isFinite(seed) ? Math.trunc(seed) >>> 0 : 0; | |
| 76 | let rng = sd; | |
| 77 | const pops = []; | |
| 78 | for (let i = 0; i < POPS; i++) { | |
| 79 | let a, b, c, d; | |
| 80 | [a, rng] = next(rng); [b, rng] = next(rng); [c, rng] = next(rng); [d, rng] = next(rng); | |
| 81 | pops.push({ | |
| 82 | id: i, | |
| 83 | at: FIRST_AT + i * START_GAP, | |
| 84 | speed: SPEED_MIN + a * (SPEED_MAX - SPEED_MIN), | |
| 85 | angle: (b < 0.5 ? -1 : 1) * (0.1 + c * (ANGLE_MAX - 0.1)), | |
| 86 | kind: d < BURNT_SHARE ? 'burnt' : 'roasted', | |
| 87 | result: null, // null while airborne or waiting, then 'caught', 'missed' (roasted) or 'burnt-caught' / 'let-fall' | |
| 88 | }); | |
| 89 | } | |
| 90 | const s = { | |
| 91 | seed: sd, status: 'playing', time: 0, pops, | |
| 92 | cone: { x: WIDTH / 2, target: WIDTH / 2, width: CONE_WIDTH, speed: CONE_SPEED }, | |
| 93 | score: 0, lives: LIVES, catches: 0, streak: 0, best: 0, line: '', | |
| 94 | }; | |
| 95 | s.line = describe(s); | |
| 96 | return s; | |
| 97 | } | |
| 98 | ||
| 99 | // move(state, x) -> { ok, state, why }: sets the cone's target, clamped to the pavement | |
| 100 | function move(state, x) { | |
| 101 | if (typeof x !== 'number' || !Number.isFinite(x)) return { ok: false, state, why: 'bad-x' }; | |
| 102 | if (state.status !== 'playing') return { ok: false, state, why: 'game-over' }; | |
| 103 | return { ok: true, state: { ...state, cone: { ...state.cone, target: Math.min(Math.max(x, 0), WIDTH) } }, why: '' }; | |
| 104 | } | |
| 105 | ||
| 106 | // tick(state, dt) -> { ok, state, why }: advances dt seconds, landing every chestnut that reaches the pavement in order | |
| 107 | function tick(state, dt) { | |
| 108 | if (typeof dt !== 'number' || !Number.isFinite(dt) || dt <= 0) return { ok: false, state, why: 'bad-dt' }; | |
| 109 | if (state.status !== 'playing') return { ok: false, state, why: 'game-over' }; | |
| 110 | const end = state.time + dt; | |
| 111 | const s = { ...state, pops: state.pops.map(p => ({ ...p })) }; | |
| 112 | const from = state.cone; | |
| 113 | for (;;) { | |
| 114 | let hit = null, t = Infinity; | |
| 115 | for (const p of s.pops) if (p.result === null && landsAt(p) < t) { hit = p; t = landsAt(p); } | |
| 116 | if (!hit || t > end) break; | |
| 117 | s.time = t; | |
| 118 | const inCone = Math.abs(landsX(hit) - coneAt(from, t - state.time)) <= CONE_WIDTH / 2; | |
| 119 | if (hit.kind === 'roasted') { | |
| 120 | if (inCone) { | |
| 121 | hit.result = 'caught'; | |
| 122 | s.score += CATCH_POINTS; s.catches += 1; s.streak += 1; s.best = Math.max(s.best, s.streak); | |
| 123 | requicken(s, t); | |
| 124 | } else { hit.result = 'missed'; s.streak = 0; } | |
| 125 | } else if (inCone) { hit.result = 'burnt-caught'; s.lives -= 1; } | |
| 126 | else hit.result = 'let-fall'; | |
| 127 | if (s.lives <= 0) s.status = 'lost'; | |
| 128 | else if (s.catches >= WIN_CATCHES) s.status = 'won'; | |
| 129 | else if (s.pops.every(p => p.result !== null)) s.status = 'lost'; | |
| 130 | if (s.status !== 'playing') break; | |
| 131 | } | |
| 132 | if (s.status === 'playing') s.time = end; | |
| 133 | s.cone = { ...from, x: coneAt(from, s.time - state.time) }; | |
| 134 | s.line = describe(s); | |
| 135 | return { ok: true, state: s, why: '' }; | |
| 136 | } | |
| 137 | ||
| 138 | // requicken(s, now): after a catch, pops not yet launched are respaced by the gap for the new catch count | |
| 139 | function requicken(s, now) { | |
| 140 | const g = gap(s.catches); | |
| 141 | let prev = null; | |
| 142 | for (const p of s.pops) { | |
| 143 | if (p.result === null && p.at > now) { | |
| 144 | p.at = Math.max(now, (prev ? prev.at : now) + (prev ? g : 0)); | |
| 145 | prev = p; | |
| 146 | } else prev = p; | |
| 147 | } | |
| 148 | } | |
| 149 | ||
| 150 | const api = { | |
| 151 | WIDTH, PAN_X, PAN_HEIGHT, GRAVITY, CONE_WIDTH, CONE_SPEED, POPS, SPEED_MIN, SPEED_MAX, ANGLE_MAX, FIRST_AT, | |
| 152 | START_GAP, QUICKEN_EVERY, QUICKEN_STEP, MIN_GAP, LIVES, CATCH_POINTS, WIN_CATCHES, | |
| 153 | start, tick, move, gap, flight, chestnutAt, landsAt, landsX, describe, | |
| 154 | }; | |
| 155 | if (typeof module !== 'undefined') module.exports = api; else root.ChestnutRoast = api; | |
| 156 | })(typeof window !== 'undefined' ? window : globalThis); |
chestnut-roast/pan.test.mjs added +187
| @@ -0,0 +1,187 @@ | ||
| 1 | import { test, expect } from 'bun:test'; | |
| 2 | import { | |
| 3 | WIDTH, PAN_X, PAN_HEIGHT, GRAVITY, CONE_WIDTH, CONE_SPEED, POPS, SPEED_MIN, SPEED_MAX, ANGLE_MAX, START_GAP, | |
| 4 | QUICKEN_EVERY, QUICKEN_STEP, MIN_GAP, LIVES, CATCH_POINTS, WIN_CATCHES, | |
| 5 | start, tick, move, gap, flight, chestnutAt, landsAt, landsX, | |
| 6 | } from './pan.js'; | |
| 7 | ||
| 8 | // a game whose schedule is exactly the given pops, straight up (angle 0) unless stated, launched at the given times | |
| 9 | const pop = (id, at, kind = 'roasted', angle = 0.5, speed = 4) => ({ id, at, speed, angle, kind, result: null }); | |
| 10 | const game = (pops, cone = {}) => { | |
| 11 | const s = start(1); | |
| 12 | return { ...s, pops, cone: { ...s.cone, ...cone } }; | |
| 13 | }; | |
| 14 | const land = p => landsAt(p) + 0.001; | |
| 15 | ||
| 16 | test('start gives a pan, a cone of stated position and width, and a seeded schedule; same seed, same pops', () => { | |
| 17 | const a = start(7), b = start(7), c = start(8); | |
| 18 | expect([a.status, a.lives, a.score, a.cone.x, a.cone.width, a.cone.speed]).toEqual(['playing', LIVES, 0, WIDTH / 2, CONE_WIDTH, CONE_SPEED]); | |
| 19 | expect(a.pops.length).toBe(POPS); | |
| 20 | expect(a.pops).toEqual(b.pops); | |
| 21 | expect(a.pops).not.toEqual(c.pops); | |
| 22 | for (let seed = 0; seed < 20; seed++) for (const p of start(seed).pops) { | |
| 23 | expect(['roasted', 'burnt']).toContain(p.kind); | |
| 24 | expect(p.speed).toBeGreaterThanOrEqual(SPEED_MIN); expect(p.speed).toBeLessThanOrEqual(SPEED_MAX); | |
| 25 | expect(Math.abs(p.angle)).toBeLessThanOrEqual(ANGLE_MAX); | |
| 26 | const x = landsX(p); | |
| 27 | expect(x).toBeGreaterThan(0); expect(x).toBeLessThan(WIDTH); // every chestnut lands on the pavement | |
| 28 | } | |
| 29 | const kinds = new Set(start(3).pops.map(p => p.kind)); | |
| 30 | expect(kinds.size).toBe(2); | |
| 31 | expect(start(NaN).seed).toBe(0); | |
| 32 | }); | |
| 33 | ||
| 34 | test('a chestnut follows the closed-form arc and lands at h = 0 by hand calculation', () => { | |
| 35 | const p = pop(0, 2, 'roasted', 0, 4); // straight up at 4 m/s from 1 m | |
| 36 | const T = (4 + Math.sqrt(16 + 2 * GRAVITY * PAN_HEIGHT)) / GRAVITY; // = (4 + 5.9666) / 9.8 | |
| 37 | expect(flight(p)).toBeCloseTo(T, 12); | |
| 38 | expect(T).toBeCloseTo(1.0170, 4); | |
| 39 | expect(chestnutAt(p, 2 + T).h).toBeCloseTo(0, 9); | |
| 40 | expect(chestnutAt(p, 2).h).toBe(PAN_HEIGHT); | |
| 41 | expect(chestnutAt(p, 0).x).toBe(PAN_X); | |
| 42 | const q = pop(1, 0, 'roasted', Math.PI / 6, 4); // 30° right of vertical: vx = 2, vy = 4cos30° | |
| 43 | expect(landsX(q)).toBeCloseTo(PAN_X + 2 * flight(q), 12); | |
| 44 | }); | |
| 45 | ||
| 46 | test('the landing point is the same however time is sliced', () => { | |
| 47 | const ps = start(11).pops.slice(0, 8).map(p => ({ ...p, kind: 'burnt' })); | |
| 48 | const run = dts => { | |
| 49 | let s = game(ps, { x: 3, target: 3 }); | |
| 50 | s = { ...s, lives: 99 }; | |
| 51 | for (const dt of dts) s = tick(s, dt).state; | |
| 52 | return s.pops.map(p => p.result); | |
| 53 | }; | |
| 54 | const total = 14; | |
| 55 | const whole = run([total]); | |
| 56 | const sliced = run(Array(1400).fill(total / 1400)); | |
| 57 | const odd = run([0.37, 5.01, 0.003, total - 5.383]); | |
| 58 | expect(sliced).toEqual(whole); | |
| 59 | expect(odd).toEqual(whole); | |
| 60 | expect(whole.every(r => r !== null)).toBe(true); | |
| 61 | }); | |
| 62 | ||
| 63 | test('the same inputs give the same game, sliced or not, with moves in between', () => { | |
| 64 | const play = dt => { | |
| 65 | let s = start(5); | |
| 66 | for (let t = 0; t < 40 && s.status === 'playing'; t += dt) { | |
| 67 | s = move(s, 5 + 4 * Math.sin(t)).state; | |
| 68 | s = tick(s, dt).state; | |
| 69 | } | |
| 70 | return s; | |
| 71 | }; | |
| 72 | expect(play(0.25).score).toBe(play(0.25).score); | |
| 73 | expect(play(0.25).pops).toEqual(play(0.25).pops); | |
| 74 | }); | |
| 75 | ||
| 76 | test('dt must be a finite number above 0', () => { | |
| 77 | const s = start(1); | |
| 78 | for (const dt of [0, -1, NaN, Infinity, '1', null, undefined]) { | |
| 79 | const r = tick(s, dt); | |
| 80 | expect([r.ok, r.why]).toEqual([false, 'bad-dt']); | |
| 81 | expect(r.state).toBe(s); | |
| 82 | } | |
| 83 | }); | |
| 84 | ||
| 85 | test('move clamps the target to the pavement and rejects non-numbers', () => { | |
| 86 | const s = start(1); | |
| 87 | expect(move(s, -5).state.cone.target).toBe(0); | |
| 88 | expect(move(s, 99).state.cone.target).toBe(WIDTH); | |
| 89 | expect(move(s, 2.5).state.cone.target).toBe(2.5); | |
| 90 | for (const x of [NaN, Infinity, '3', null]) { const r = move(s, x); expect([r.ok, r.why, r.state]).toEqual([false, 'bad-x', s]); } | |
| 91 | expect(s.cone.target).toBe(WIDTH / 2); | |
| 92 | }); | |
| 93 | ||
| 94 | test('the cone travels toward its target at the stated top speed and stops there', () => { | |
| 95 | let s = move(start(1), 10).state; // 5 m away, nothing airborne before t = 1 | |
| 96 | s = tick(s, 0.5).state; | |
| 97 | expect(s.cone.x).toBeCloseTo(5 + CONE_SPEED * 0.5, 12); | |
| 98 | s = tick(s, 0.1).state; | |
| 99 | expect(s.cone.x).toBeCloseTo(5 + CONE_SPEED * 0.6 > 10 ? 10 : 5 + CONE_SPEED * 0.6, 12); | |
| 100 | s = tick(s, 0.3).state; | |
| 101 | expect(s.cone.x).toBe(10); | |
| 102 | s = tick(move(s, 0).state, 0.5).state; // leftward, 3 m in 0.5 s | |
| 103 | expect(s.cone.x).toBeCloseTo(10 - CONE_SPEED * 0.5, 12); | |
| 104 | }); | |
| 105 | ||
| 106 | test('a roasted chestnut landing in the cone scores; the cone needs time to get there', () => { | |
| 107 | const p = pop(0, 0); | |
| 108 | const x = landsX(p); | |
| 109 | let s = tick(game([p], { x, target: x }), land(p)).state; | |
| 110 | expect([s.score, s.catches, s.streak, s.pops[0].result]).toEqual([CATCH_POINTS, 1, 1, 'caught']); | |
| 111 | // edge of the cone still catches, just outside does not | |
| 112 | s = tick(game([p], { x: x + CONE_WIDTH / 2 - 0.01, target: x + CONE_WIDTH / 2 - 0.01 }), land(p)).state; | |
| 113 | expect(s.pops[0].result).toBe('caught'); | |
| 114 | s = tick(game([p], { x: x + CONE_WIDTH / 2 + 0.01, target: x + CONE_WIDTH / 2 + 0.01 }), land(p)).state; | |
| 115 | expect(s.pops[0].result).toBe('missed'); | |
| 116 | // a far cone starts moving only as time passes: too slow to arrive | |
| 117 | const far = game([p], { x: 0, target: x }); | |
| 118 | expect(x / CONE_SPEED).toBeGreaterThan(landsAt(p)); | |
| 119 | expect(tick(far, land(p)).state.pops[0].result).toBe('missed'); | |
| 120 | }); | |
| 121 | ||
| 122 | test('a burnt chestnut in the cone costs a life; burnt let fall costs nothing', () => { | |
| 123 | const p = pop(0, 0, 'burnt'); | |
| 124 | const x = landsX(p); | |
| 125 | const hit = tick(game([p, pop(1, 50)], { x, target: x }), land(p)).state; | |
| 126 | expect([hit.lives, hit.score, hit.pops[0].result, hit.status]).toEqual([LIVES - 1, 0, 'burnt-caught', 'playing']); | |
| 127 | const far = tick(game([p, pop(1, 50)], { x: 0.5, target: 0.5 }), land(p)).state; | |
| 128 | expect([far.lives, far.pops[0].result]).toEqual([LIVES, 'let-fall']); | |
| 129 | }); | |
| 130 | ||
| 131 | test('a missed roasted chestnut breaks the streak but keeps the best', () => { | |
| 132 | const a = pop(0, 0), b = pop(1, 1, 'roasted', -0.5); | |
| 133 | const xa = landsX(a); | |
| 134 | let s = game([a, b, pop(2, 90)], { x: xa, target: xa }); | |
| 135 | s = tick(s, land(a)).state; | |
| 136 | expect([s.streak, s.best]).toEqual([1, 1]); | |
| 137 | const nb = s.pops[1]; // the catch respaced b, so read it from the state | |
| 138 | expect(Math.abs(landsX(nb) - xa)).toBeGreaterThan(CONE_WIDTH); | |
| 139 | s = tick(s, land(nb) - s.time).state; | |
| 140 | expect([s.streak, s.best, s.catches, s.pops[1].result]).toEqual([0, 1, 1, 'missed']); | |
| 141 | }); | |
| 142 | ||
| 143 | test('every stated number of catches the pops come faster by the stated step, down to the floor', () => { | |
| 144 | expect(gap(0)).toBe(START_GAP); | |
| 145 | expect(gap(QUICKEN_EVERY - 1)).toBe(START_GAP); | |
| 146 | expect(gap(QUICKEN_EVERY)).toBeCloseTo(START_GAP - QUICKEN_STEP, 12); | |
| 147 | expect(gap(QUICKEN_EVERY * 2)).toBeCloseTo(START_GAP - 2 * QUICKEN_STEP, 12); | |
| 148 | expect(gap(10000)).toBe(MIN_GAP); | |
| 149 | expect(gap(10000)).toBeGreaterThan(0); | |
| 150 | // in play: the third catch respaces the pops still waiting | |
| 151 | const caught = [pop(0, 0), pop(1, 0), pop(2, 0)]; | |
| 152 | const rest = [pop(3, 50), pop(4, 60), pop(5, 70)]; | |
| 153 | const x = landsX(caught[0]); | |
| 154 | const s = tick(game([...caught, ...rest], { x, target: x }), land(caught[2])).state; | |
| 155 | expect(s.catches).toBe(QUICKEN_EVERY); | |
| 156 | const ats = s.pops.slice(3).map(p => p.at); | |
| 157 | expect(ats[1] - ats[0]).toBeCloseTo(gap(QUICKEN_EVERY), 12); | |
| 158 | expect(ats[2] - ats[1]).toBeCloseTo(gap(QUICKEN_EVERY), 12); | |
| 159 | expect(ats[0]).toBeGreaterThanOrEqual(s.time); | |
| 160 | // the untouched schedule has the base gap | |
| 161 | const base = start(2).pops; | |
| 162 | expect(base[1].at - base[0].at).toBeCloseTo(START_GAP, 12); | |
| 163 | }); | |
| 164 | ||
| 165 | test('losing every life ends the game with a loss line', () => { | |
| 166 | const ps = [0, 1, 2, 3].map(i => pop(i, 0, 'burnt')); | |
| 167 | const x = landsX(ps[0]); | |
| 168 | const s = tick(game(ps, { x, target: x }), 5).state; | |
| 169 | expect([s.status, s.lives]).toEqual(['lost', 0]); | |
| 170 | expect(s.line).toBe('You lose: no lives left, score 0'); | |
| 171 | expect(s.pops[3].result).toBe(null); // the game stopped at the last life | |
| 172 | for (const r of [tick(s, 1), move(s, 3)]) expect([r.ok, r.why, r.state]).toEqual([false, 'game-over', s]); | |
| 173 | }); | |
| 174 | ||
| 175 | test('catching the stated total wins, reporting score and best streak', () => { | |
| 176 | const ps = Array.from({ length: WIN_CATCHES + 2 }, (_, i) => pop(i, 0)); | |
| 177 | const x = landsX(ps[0]); | |
| 178 | const s = tick(game(ps, { x, target: x }), 5).state; | |
| 179 | expect([s.status, s.catches, s.score, s.best]).toEqual(['won', WIN_CATCHES, WIN_CATCHES * CATCH_POINTS, WIN_CATCHES]); | |
| 180 | expect(s.line).toBe(`You win: score ${WIN_CATCHES * CATCH_POINTS}, best streak ${WIN_CATCHES}`); | |
| 181 | expect(tick(s, 1).why).toBe('game-over'); | |
| 182 | }); | |
| 183 | ||
| 184 | test('a pan that runs dry before the total ends the game as a loss', () => { | |
| 185 | const s = tick(game([pop(0, 0)], { x: 0, target: 0 }), 5).state; | |
| 186 | expect([s.status, s.line]).toEqual(['lost', 'You lose: the pan ran dry, score 0']); | |
| 187 | }); |
new above
offline