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>
59 lines
3.2 KiB
Scala
59 lines
3.2 KiB
Scala
package snake
|
|
|
|
/**
|
|
* m05 game: one `update` turns every action into a new state; replaying a game is a fold over its actions.
|
|
* m10 adds pause, restart, a frame counter that makes the snake speed up as the score grows, and a turn queue.
|
|
*/
|
|
object Game:
|
|
|
|
/**
|
|
* 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.
|
|
*/
|
|
def steer(state: GameState, dir: Direction): GameState =
|
|
if state.status != Status.Playing then state
|
|
else if state.nextDir == state.dir then Rules.turn(state, dir)
|
|
else if dir != state.nextDir && dir != Model.opposite(state.nextDir) then state.copy(queued = Some(dir))
|
|
else 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.
|
|
private def takeQueued(state: GameState): GameState = state.queued match
|
|
case Some(dir) if state.status == Status.Playing => state.copy(nextDir = dir, queued = None)
|
|
case _ => state
|
|
|
|
// One game step as `update` takes it: `tick`, then the queued turn.
|
|
private def step(state: GameState): GameState = takeQueued(Spawn.tick(state))
|
|
|
|
/** How many frames pass between game steps: 8 at first, one fewer every 3 points, never fewer than 2. */
|
|
def movesEvery(score: Int): Int = math.max(2, 8 - score / 3)
|
|
|
|
// 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.
|
|
private def frame(state: GameState): GameState =
|
|
if state.status != Status.Playing then state
|
|
else
|
|
val n = state.frame + 1
|
|
if n >= movesEvery(state.score) then step(state.copy(frame = 0)) else state.copy(frame = n)
|
|
|
|
// Pause and resume only make sense in those two statuses; a finished game stays finished.
|
|
private def togglePause(state: GameState): GameState = state.status match
|
|
case Status.Playing => state.copy(status = Status.Paused)
|
|
case Status.Paused => state.copy(status = Status.Playing)
|
|
case Status.GameOver | Status.Won => state
|
|
|
|
// Every case is listed (no `case _`): adding an Action later makes the compiler point here.
|
|
def update(state: GameState, action: Action): GameState = action match
|
|
case Action.Tick => step(state)
|
|
case Action.Turn(dir) => steer(state, dir)
|
|
case Action.TogglePause => togglePause(state)
|
|
case Action.Restart => Spawn.newGame(state.seed)
|
|
case Action.Frame => frame(state)
|
|
|
|
def replay(state: GameState, actions: List[Action]): GameState = actions.foldLeft(state)(update)
|
|
|
|
// scanLeft is foldLeft that keeps every intermediate result.
|
|
def history(state: GameState, actions: List[Action]): List[GameState] = actions.scanLeft(state)(update)
|