feat(course): audit fixes and twelve extensions (Modules 7b, 12, 13)
Audit pass: - Frame rule counts frames since the last move, so every step lasts exactly movesEvery(score) frames, also right after a speed-up - clampTime + MAX_GAP_MS: a hidden tab no longer replays the whole gap - Pong Restart keeps held keys - Fact corrections (mulberry32 output coverage, Haskell IO, uniform) - New fixture cases from a mutation analysis Extensions (all three tracks, shared fixtures, course text): - Turn queue (Exercise 10.6), persisted high score (10.7), replay viewer (10.8), play() end-to-end helper (9.5) - Optional State (4.7) and optional Module 7b, stack-safe IO - Module 12, property-based testing with generators, shrinking, invariants and a model-based oracle - Module 13, Conway's Life with a Click event - JS type checking via jsconfig, JSDoc and types.d.ts - Maintainer tools: scripts/mutate.mjs, scripts/typecheck-js.sh, Playwright end-to-end suite (e2e/, scripts/e2e.sh) Docs: docs/contract.md is the canonical contract; specs and build log under docs/superpowers. Verified: JS 728, Scala 689, F# 691 unit tests; stubs and progression M01-M13 and M07b; 176 end-to-end tests on Chromium and WebKit. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
1 parent
876f5aabb1
commit
edd57f0148
253 files changed
+21753
-503
No files matched your search
+37
-9
@@ -1,16 +1,41 @@
|
||||
// m05 game: one `update` function turns every action into a new state; replay is a fold over actions.
|
||||
// m10 adds pause, restart and a frame clock that speeds up as the score grows.
|
||||
// m10 adds pause, restart, a frame counter that makes the snake speed up as the score grows, and a turn queue.
|
||||
import { turn } from './rules.js'
|
||||
import { opposite } from './model.js'
|
||||
import { tick, newGame } from './spawn.js'
|
||||
/** @import { Direction, Action, GameState } from './types' */
|
||||
|
||||
/** How many frames pass between game steps: 8 at first, one fewer every 3 points, never fewer than 2. */
|
||||
/**
|
||||
* What `update` does with `Turn(dir)` from Module 10 on. With no turn pending (`nextDir == dir`) it is the Module 3
|
||||
* `turn`. With a turn pending, a key that is neither `nextDir` nor its opposite is remembered in `queued`
|
||||
* (replacing an earlier one) and taken on the step after; any other key changes nothing.
|
||||
* @type {(state: GameState, dir: Direction) => GameState}
|
||||
*/
|
||||
export const steer = (state, dir) => {
|
||||
if (state.status !== 'Playing') return state
|
||||
if (state.nextDir === state.dir) return turn(state, dir)
|
||||
return dir !== state.nextDir && dir !== opposite(state.nextDir) ? { ...state, queued: dir } : state
|
||||
}
|
||||
|
||||
// After a step: a game still in play takes the queued turn, if there is one. It is always a legal turn, because the
|
||||
// step just made `dir` equal to the old `nextDir`, and `queued` is neither that nor its opposite.
|
||||
const takeQueued = (state) =>
|
||||
state.status === 'Playing' && state.queued !== null ? { ...state, nextDir: state.queued, queued: null } : state
|
||||
|
||||
// One game step as `update` takes it: `tick`, then the queued turn.
|
||||
const step = (state) => takeQueued(tick(state))
|
||||
|
||||
/** How many frames pass between game steps: 8 at first, one fewer every 3 points, never fewer than 2.
|
||||
* @type {(score: number) => number} */
|
||||
export const movesEvery = (score) => Math.max(2, 8 - Math.floor(score / 3))
|
||||
|
||||
// One animation frame: count it, and every `movesEvery(score)`-th frame run a game step.
|
||||
// One animation frame. `frame` counts the frames since the snake last moved; when it reaches
|
||||
// `movesEvery(score)` the count starts again and the game takes a step. Counting from the last move
|
||||
// (not from the start of the game) keeps every step exactly `movesEvery` frames long, also right after a speed-up.
|
||||
const frame = (state) => {
|
||||
if (state.status !== 'Playing') return state
|
||||
const counted = { ...state, frame: state.frame + 1 }
|
||||
return counted.frame % movesEvery(counted.score) === 0 ? tick(counted) : counted
|
||||
const n = state.frame + 1
|
||||
return n >= movesEvery(state.score) ? step({ ...state, frame: 0 }) : { ...state, frame: n }
|
||||
}
|
||||
|
||||
// Pause and resume only make sense in those two statuses; a finished game stays finished.
|
||||
@@ -19,10 +44,11 @@ const togglePause = (state) =>
|
||||
: state.status === 'Paused' ? { ...state, status: 'Playing' }
|
||||
: state
|
||||
|
||||
/** @type {(state: GameState, action: Action) => GameState} */
|
||||
export const update = (state, action) => {
|
||||
switch (action.tag) {
|
||||
case 'Tick': return tick(state)
|
||||
case 'Turn': return turn(state, action.dir)
|
||||
case 'Tick': return step(state)
|
||||
case 'Turn': return steer(state, action.dir)
|
||||
case 'TogglePause': return togglePause(state)
|
||||
case 'Restart': return newGame(state.seed)
|
||||
case 'Frame': return frame(state)
|
||||
@@ -30,10 +56,12 @@ export const update = (state, action) => {
|
||||
}
|
||||
}
|
||||
|
||||
/** Fold the actions over the state, left to right. */
|
||||
/** Fold the actions over the state, left to right.
|
||||
* @type {(state: GameState, actions: Action[]) => GameState} */
|
||||
export const replay = (state, actions) => actions.reduce(update, state)
|
||||
|
||||
/** Like replay, but keeps each intermediate state. Recursion instead of mutation: no array is ever pushed to. */
|
||||
/** Like replay, but keeps each intermediate state. Recursion instead of mutation: no array is ever pushed to.
|
||||
* @type {(state: GameState, actions: Action[]) => GameState[]} */
|
||||
export const history = (state, actions) =>
|
||||
actions.length === 0
|
||||
? [state]
|
||||
|
||||
Reference in new issue
Block a user