Public: anyone can read this; the crew can change it.
Migrated from github.com/amirmushichge/pixel-plays
Go-Kart Fete rules: a tested `go-kart/lap.js` that plays a whole game from plain data #118
Check · Review · Land · Live done — Landed and live in production.
vesper/pixel-plays:vesper/103-go-kart-fete-rules-a-tested-go-kart-lap → main 3 commits fork 1 thread · 1 unresolved
2 files
changed +257
−0
go-kart/lap.js added +104
| @@ -0,0 +1,104 @@ | ||
| 1 | 'use strict'; | |
| 2 | // The Go-Kart Fete rules: three races of three laps round a village track, held throttle against the bends. | |
| 3 | // Metres and seconds. Plain numbers in, plain numbers out, no DOM, no timers, no Math.random: the rivals' paces | |
| 4 | // come from the seed. The page draws what this decides. | |
| 5 | (function (root) { | |
| 6 | const LAP = 400, LAPS = 3, RACES = 3, RIVALS = 3; // metres round the track, laps and races to a game, karts against you | |
| 7 | const ACCEL = 6, BRAKE = 14, MAX_SPEED = 26; // m/s^2 with the pedal down, m/s^2 with it up, and the top speed | |
| 8 | const BENDS = [{ at: 90, safe: 13 }, { at: 205, safe: 16 }, { at: 310, safe: 10 }]; // metres into a lap, m/s you may enter at | |
| 9 | const BALES_STOP = 1; // seconds stopped in the hay bales after going in too fast | |
| 10 | const PACE_BASE = 16.5, PACE_RACE = 1.5, PACE_SPREAD = 1; // rival m/s in race one, quicker per race, and the seeded wobble across them | |
| 11 | const POINTS = [3, 2, 1, 0]; // for first, second, third and last | |
| 12 | const WIN_AT = 6, REST = 2.5, RESTART_AFTER = .6; // total needed to win, seconds between races, and before a tap restarts | |
| 13 | ||
| 14 | const finite = v => (Number.isFinite(v) ? v : 0); | |
| 15 | const clamp = (v, a, b) => Math.max(a, Math.min(b, v)); | |
| 16 | // a repeatable number in [0, 1) for this seed and slot | |
| 17 | function noise(seed, n) { | |
| 18 | let h = (Math.imul(finite(seed) | 0, 374761393) + Math.imul(n | 0, 668265263)) | 0; | |
| 19 | h = Math.imul(h ^ (h >>> 13), 1274126177); | |
| 20 | return ((h ^ (h >>> 16)) >>> 0) / 4294967296; | |
| 21 | } | |
| 22 | // speed after dt seconds: the pedal down gathers speed to the top, the pedal up brakes to a stop | |
| 23 | function speedStep(v, throttle, dt) { | |
| 24 | v = clamp(finite(v), 0, MAX_SPEED); dt = Math.max(0, finite(dt)); | |
| 25 | return throttle ? Math.min(MAX_SPEED, v + ACCEL * dt) : Math.max(0, v - BRAKE * dt); | |
| 26 | } | |
| 27 | // rival k's steady pace in race r (1-based): quicker every race, a little different for each kart and seed | |
| 28 | function rivalPace(race, k, seed = 0) { | |
| 29 | return PACE_BASE + PACE_RACE * (clamp(Math.floor(finite(race)), 1, RACES) - 1) + (noise(seed, k) - .5) * 2 * PACE_SPREAD; | |
| 30 | } | |
| 31 | const paces = (race, seed) => Array.from({ length: RIVALS }, (_, k) => rivalPace(race, k, seed)); | |
| 32 | const pointsFor = place => POINTS[clamp(Math.floor(finite(place)), 1, POINTS.length) - 1]; | |
| 33 | const won = s => s.total >= WIN_AT; | |
| 34 | ||
| 35 | function newRace(race, seed) { | |
| 36 | return { race, x: 0, v: 0, stuck: 0, time: 0, hits: 0, paces: paces(race, seed), finished: false, place: 1 }; | |
| 37 | } | |
| 38 | function newGame(best = 0, seed = 0) { | |
| 39 | seed = finite(seed) >>> 0; | |
| 40 | const s = { phase: 'race', over: false, outcome: '', seed, total: 0, best: Math.max(0, finite(best)), record: false, timer: 0, results: [], line: '' }; | |
| 41 | return refresh(Object.assign(s, newRace(1, seed))); | |
| 42 | } | |
| 43 | // the fields a page or a playtest reads: where you are in the race, and the line to show | |
| 44 | function refresh(s) { | |
| 45 | s.lap = Math.min(LAPS, Math.floor(s.x / LAP) + 1); | |
| 46 | s.speed = s.v; | |
| 47 | if (!s.finished) s.place = 1 + s.paces.filter(p => p * s.time > s.x).length; | |
| 48 | s.score = s.total; | |
| 49 | s.line = s.over ? (s.outcome === 'won' ? `Won with ${s.total} points` : `Lost with ${s.total} points`) : `Race ${s.race} of ${RACES}, lap ${s.lap} of ${LAPS}, place ${s.place}`; | |
| 50 | return s; | |
| 51 | } | |
| 52 | // the bend whose entry the kart crossed moving from a to b along the track, or null | |
| 53 | function bendCrossed(a, b) { | |
| 54 | for (let l = Math.floor(a / LAP); l * LAP <= b; l++) for (let i = 0; i < BENDS.length; i++) { | |
| 55 | const at = l * LAP + BENDS[i].at; | |
| 56 | if (a < at && b >= at) return { bend: i, lap: l + 1, at, safe: BENDS[i].safe }; | |
| 57 | } | |
| 58 | return null; | |
| 59 | } | |
| 60 | function finishRace(s, events) { | |
| 61 | s.finished = true; | |
| 62 | s.place = 1 + s.paces.filter(p => LAP * LAPS / p < s.time).length; | |
| 63 | const points = pointsFor(s.place); | |
| 64 | s.total += points; | |
| 65 | s.results.push({ race: s.race, place: s.place, points, time: s.time }); | |
| 66 | s.timer = 0; | |
| 67 | if (s.race >= RACES) { | |
| 68 | s.phase = 'over'; s.over = true; s.outcome = won(s) ? 'won' : 'lost'; | |
| 69 | if (s.total > s.best) { s.best = s.total; s.record = true; } | |
| 70 | events.push({ type: s.outcome, total: s.total, record: s.record }); | |
| 71 | } else s.phase = 'rest'; | |
| 72 | events.push({ type: 'finish', race: s.race, place: s.place, points }); | |
| 73 | } | |
| 74 | // move the game on dt seconds; input.throttle is true while held | |
| 75 | function advance(s, dt, input) { | |
| 76 | const events = []; | |
| 77 | dt = Math.max(0, Math.min(.1, finite(dt))); | |
| 78 | if (s.phase === 'over') { s.timer += dt; return refresh(s), events; } | |
| 79 | if (s.phase === 'rest') { | |
| 80 | s.timer += dt; | |
| 81 | if (s.timer >= REST) { Object.assign(s, newRace(s.race + 1, s.seed), { phase: 'race', timer: 0 }); events.push({ type: 'start', race: s.race }); } | |
| 82 | return refresh(s), events; | |
| 83 | } | |
| 84 | s.time += dt; | |
| 85 | if (s.stuck > 0) { s.stuck = Math.max(0, s.stuck - dt); s.v = 0; return refresh(s), events; } | |
| 86 | const before = s.x; | |
| 87 | const v = speedStep(s.v, !!(input && input.throttle), dt); | |
| 88 | s.x += (s.v + v) / 2 * dt; s.v = v; | |
| 89 | const b = bendCrossed(before, s.x); | |
| 90 | if (b && s.v > b.safe + 1e-9) { s.x = b.at; s.v = 0; s.stuck = BALES_STOP; s.hits++; events.push({ type: 'bales', bend: b.bend, lap: b.lap }); } | |
| 91 | if (s.x >= LAP * LAPS) { s.x = LAP * LAPS; finishRace(s, events); } | |
| 92 | return refresh(s), events; | |
| 93 | } | |
| 94 | // a tap after the game has been over for a moment starts the next one; otherwise null | |
| 95 | function again(s) { | |
| 96 | return s.phase === 'over' && s.timer > RESTART_AFTER ? newGame(s.best, s.seed + 1) : null; | |
| 97 | } | |
| 98 | ||
| 99 | const api = { | |
| 100 | LAP, LAPS, RACES, RIVALS, ACCEL, BRAKE, MAX_SPEED, BENDS, BALES_STOP, PACE_BASE, PACE_RACE, PACE_SPREAD, POINTS, WIN_AT, REST, RESTART_AFTER, | |
| 101 | speedStep, rivalPace, pointsFor, won, newGame, advance, again, | |
| 102 | }; | |
| 103 | if (typeof module !== 'undefined') module.exports = api; else root.Lap = api; | |
| 104 | })(typeof window !== 'undefined' ? window : globalThis); |
go-kart/lap.test.mjs added +153
| @@ -0,0 +1,153 @@ | ||
| 1 | import { test, expect } from 'bun:test'; | |
| 2 | import { LAP, LAPS, RACES, ACCEL, BRAKE, MAX_SPEED, BENDS, BALES_STOP, POINTS, WIN_AT, REST, speedStep, rivalPace, pointsFor, newGame, advance, again } from './lap.js'; | |
| 3 | ||
| 4 | const DT = 1 / 60; | |
| 5 | // play one frame at a time; drive(s) says whether the pedal is down | |
| 6 | const play = (s, drive, seconds, events = []) => { for (let t = 0; t < seconds - 1e-9; t += DT) events.push(...advance(s, DT, { throttle: drive(s) })); return events; }; | |
| 7 | // a careful driver: lifts early enough to reach the next bend at or under its safe speed | |
| 8 | const careful = s => { | |
| 9 | const lapX = s.x % LAP, next = BENDS.find(b => b.at > lapX); | |
| 10 | if (!next) return true; | |
| 11 | const v = s.v + ACCEL * DT, gap = next.at - lapX - v * DT * 2; | |
| 12 | return v <= next.safe || gap > (v * v - next.safe * next.safe) / (2 * BRAKE); | |
| 13 | }; | |
| 14 | const full = () => true, none = () => false; | |
| 15 | const playGame = (drive, seed = 0) => { const s = newGame(0, seed); play(s, drive, 300); return s; }; | |
| 16 | ||
| 17 | test('speed is a pure step function of throttle and time', () => { | |
| 18 | expect(speedStep(0, true, 1)).toBe(ACCEL); | |
| 19 | expect(speedStep(10, true, .5)).toBe(10 + ACCEL * .5); | |
| 20 | expect(speedStep(20, true, 5)).toBe(MAX_SPEED); | |
| 21 | expect(speedStep(10, true, .5)).toBe(speedStep(10, true, .5)); | |
| 22 | expect(speedStep(NaN, true, NaN)).toBe(0); | |
| 23 | }); | |
| 24 | ||
| 25 | test('letting go brakes, harder than the throttle gathers, and never below a stop', () => { | |
| 26 | expect(speedStep(20, false, .5)).toBe(20 - BRAKE * .5); | |
| 27 | expect(BRAKE).toBeGreaterThan(ACCEL); | |
| 28 | expect(speedStep(3, false, 5)).toBe(0); | |
| 29 | }); | |
| 30 | ||
| 31 | test('the kart accelerates while held and slows when let go', () => { | |
| 32 | const s = newGame(); | |
| 33 | play(s, full, 1); | |
| 34 | expect(s.speed).toBeCloseTo(ACCEL, 5); | |
| 35 | play(s, none, .2); | |
| 36 | expect(s.speed).toBeCloseTo(ACCEL - BRAKE * .2, 5); | |
| 37 | }); | |
| 38 | ||
| 39 | test('entering a bend over its safe speed sends the kart into the bales for one second', () => { | |
| 40 | const s = newGame(); | |
| 41 | s.x = BENDS[0].at - 1; s.v = BENDS[0].safe + 3; | |
| 42 | const events = play(s, full, .2); | |
| 43 | expect(events.find(e => e.type === 'bales')).toMatchObject({ bend: 0, lap: 1 }); | |
| 44 | expect(s.speed).toBe(0); | |
| 45 | const x = s.x; | |
| 46 | play(s, full, BALES_STOP * .8); | |
| 47 | expect(s.x).toBe(x); | |
| 48 | play(s, full, BALES_STOP * .4); | |
| 49 | expect(s.x).toBeGreaterThan(x); | |
| 50 | }); | |
| 51 | ||
| 52 | test('entering a bend at or under its safe speed carries on', () => { | |
| 53 | const s = newGame(); | |
| 54 | s.x = BENDS[1].at - 1; s.v = BENDS[1].safe; | |
| 55 | const events = play(s, none, DT * 4); | |
| 56 | expect(events.some(e => e.type === 'bales')).toBe(false); | |
| 57 | expect(s.x).toBeGreaterThan(BENDS[1].at); | |
| 58 | expect(s.speed).toBeGreaterThan(0); | |
| 59 | }); | |
| 60 | ||
| 61 | test('the bends are guarded on later laps too', () => { | |
| 62 | const s = newGame(); | |
| 63 | s.x = LAP + BENDS[2].at - 1; s.v = BENDS[2].safe + 5; | |
| 64 | expect(play(s, full, .2).find(e => e.type === 'bales')).toMatchObject({ bend: 2, lap: 2 }); | |
| 65 | }); | |
| 66 | ||
| 67 | test('rival pace rises every race and varies a little between karts, repeatably', () => { | |
| 68 | for (let k = 0; k < 3; k++) { | |
| 69 | expect(rivalPace(2, k, 7)).toBeGreaterThan(rivalPace(1, k, 7)); | |
| 70 | expect(rivalPace(3, k, 7)).toBeGreaterThan(rivalPace(2, k, 7)); | |
| 71 | expect(rivalPace(1, k, 7)).toBe(rivalPace(1, k, 7)); | |
| 72 | } | |
| 73 | const p = [0, 1, 2].map(k => rivalPace(1, k, 7)); | |
| 74 | expect(new Set(p).size).toBe(3); | |
| 75 | expect(Math.max(...p) - Math.min(...p)).toBeLessThan(3); | |
| 76 | expect(rivalPace(1, 0, 1)).not.toBe(rivalPace(1, 0, 2)); | |
| 77 | }); | |
| 78 | ||
| 79 | test('finishing place scores 3, 2, 1, 0', () => { | |
| 80 | expect(POINTS).toEqual([3, 2, 1, 0]); | |
| 81 | expect([1, 2, 3, 4].map(pointsFor)).toEqual([3, 2, 1, 0]); | |
| 82 | expect(pointsFor(9)).toBe(0); | |
| 83 | }); | |
| 84 | ||
| 85 | test('place counts the rivals ahead and the race ends after three laps', () => { | |
| 86 | const s = newGame(); | |
| 87 | play(s, none, 5); | |
| 88 | expect(s.place).toBe(4); | |
| 89 | s.x = LAP * LAPS - 1; s.v = 5; s.time = 20; | |
| 90 | const events = play(s, full, 1); | |
| 91 | expect(events.find(e => e.type === 'finish')).toMatchObject({ race: 1, place: 1, points: 3 }); | |
| 92 | expect(s.total).toBe(3); | |
| 93 | expect(s.phase).toBe('rest'); | |
| 94 | }); | |
| 95 | ||
| 96 | test('the state reports race, lap, place, speed, total and best', () => { | |
| 97 | const s = newGame(4); | |
| 98 | expect(s).toMatchObject({ race: 1, lap: 1, place: 1, speed: 0, total: 0, best: 4, phase: 'race', over: false, outcome: '' }); | |
| 99 | s.x = LAP * 1.5; | |
| 100 | advance(s, DT, {}); | |
| 101 | expect(s.lap).toBe(2); | |
| 102 | expect(s.line).toContain('lap 2'); | |
| 103 | }); | |
| 104 | ||
| 105 | test('rests between races, then the next race has quicker rivals', () => { | |
| 106 | const s = newGame(0, 3); | |
| 107 | const first = s.paces.slice(); | |
| 108 | s.x = LAP * LAPS - 1; s.v = 5; | |
| 109 | play(s, full, .5); | |
| 110 | expect(s.phase).toBe('rest'); | |
| 111 | play(s, none, REST + .2); | |
| 112 | expect(s).toMatchObject({ phase: 'race', race: 2, x: 0 }); | |
| 113 | expect(s.paces.every((p, k) => p > first[k])).toBe(true); | |
| 114 | }); | |
| 115 | ||
| 116 | test('a careful throttle wins the game', () => { | |
| 117 | const s = playGame(careful); | |
| 118 | expect(s).toMatchObject({ phase: 'over', over: true, outcome: 'won', race: RACES }); | |
| 119 | expect(s.total).toBeGreaterThanOrEqual(WIN_AT); | |
| 120 | expect(s.results).toHaveLength(RACES); | |
| 121 | expect(s.line).toBe(`Won with ${s.total} points`); | |
| 122 | expect(s.record).toBe(true); | |
| 123 | }); | |
| 124 | ||
| 125 | test('holding full throttle into every bend loses the game', () => { | |
| 126 | const s = playGame(full); | |
| 127 | expect(s.hits).toBeGreaterThan(0); | |
| 128 | expect(s).toMatchObject({ over: true, outcome: 'lost' }); | |
| 129 | expect(s.total).toBeLessThan(WIN_AT); | |
| 130 | expect(s.line).toBe(`Lost with ${s.total} points`); | |
| 131 | }); | |
| 132 | ||
| 133 | test('never touching the throttle scores nothing and loses', () => { | |
| 134 | const s = newGame(); | |
| 135 | play(s, none, 1000); | |
| 136 | expect(s.over).toBe(false); | |
| 137 | expect(s.total).toBe(0); | |
| 138 | }); | |
| 139 | ||
| 140 | test('the same seed plays the same game', () => { | |
| 141 | expect(playGame(careful, 5)).toEqual(playGame(careful, 5)); | |
| 142 | }); | |
| 143 | ||
| 144 | test('a tap restarts only after the game has been over a moment', () => { | |
| 145 | const s = playGame(careful); | |
| 146 | s.timer = 0; | |
| 147 | expect(s.over).toBe(true); | |
| 148 | expect(again(s)).toBeNull(); | |
| 149 | play(s, none, 1); | |
| 150 | const n = again(s); | |
| 151 | expect(n).toMatchObject({ race: 1, total: 0, best: s.best, over: false }); | |
| 152 | expect(again(newGame())).toBeNull(); | |
| 153 | }); |
new above
offline