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
+45
-2
@@ -2,11 +2,14 @@
|
||||
module Snake.App
|
||||
|
||||
open Snake.Model
|
||||
open Snake.View
|
||||
open Snake.IO
|
||||
open Snake.Stream
|
||||
|
||||
let TICK_MS = 150L
|
||||
let FRAME_MS = 25L
|
||||
// m10: the longest step of time the game accepts at once; a longer silence (a background tab) counts as this much.
|
||||
let MAX_GAP_MS = 250L
|
||||
|
||||
// Everything that can happen to the game: a Tick every TICK_MS, and an action for each bound key.
|
||||
let classicActions: Stream<Action> =
|
||||
@@ -22,7 +25,47 @@ let states (seed: uint32) (actions: Stream<Action>) : Stream<GameState> =
|
||||
|
||||
let render (state: GameState) : IO<unit> = IO.drawScene (View.view state)
|
||||
|
||||
// The whole program as a value: get a seed, then turn the stream of states into a stream of drawings.
|
||||
// m10: a session wraps the game with the best score so far. GameState, update and view do not know about it.
|
||||
// `Improved` says whether the action that produced this session raised the best, which is when it must be saved.
|
||||
type Session = { Game: GameState; Best: int; Improved: bool }
|
||||
|
||||
let sessionUpdate (session: Session) (action: Action) : Session =
|
||||
let game = Game.update session.Game action
|
||||
let best = max session.Best game.Score
|
||||
{ Game = game; Best = best; Improved = best > session.Best }
|
||||
|
||||
// The same running fold as `states`, one level up.
|
||||
let sessions (seed: uint32) (best: int) (actions: Stream<Action>) : Stream<Session> =
|
||||
let s0 = { Game = Spawn.newGame seed; Best = best; Improved = false }
|
||||
actions |> Stream.scan sessionUpdate s0 |> Stream.startWith s0
|
||||
|
||||
// The game's picture with the best score at the top right, level with the score on the left.
|
||||
let sessionView (session: Session) : Scene =
|
||||
View.view session.Game
|
||||
@ [ Text(session.Game.Width * CELL - 6, 18, $"Best: {session.Best}", "#ffffff", 16, "right") ]
|
||||
|
||||
// Draw, and save the best only on the action that raised it.
|
||||
let renderSession (session: Session) : IO<unit> =
|
||||
if session.Improved then
|
||||
IO.sequence [ IO.drawScene (sessionView session); IO.saveBest session.Best ]
|
||||
else
|
||||
IO.drawScene (sessionView session)
|
||||
|
||||
// The whole program as a value: get a seed, then the stored best, then turn the stream of sessions into a stream
|
||||
// of drawings. clampTime wraps the whole stream, so everything inside it sees the clamped clock.
|
||||
// Nothing runs here; Runtime interprets it.
|
||||
let main () : IO<Stream<IO<unit>>> =
|
||||
IO.randomSeed () |> IO.map (fun seed -> states seed speedActions |> Stream.map render)
|
||||
IO.randomSeed ()
|
||||
|> IO.bind (fun seed ->
|
||||
IO.loadBest ()
|
||||
|> IO.map (fun best -> sessions seed best speedActions |> Stream.map renderSession |> Stream.clampTime MAX_GAP_MS))
|
||||
|
||||
// m09: the whole program in one equality. For each drawing that `main` emits while fed `events`, the ops it
|
||||
// performs, answered by the pure interpreter (`seed` for RandomSeed, and from m10 `best` for LoadBest).
|
||||
let playWith (seed: uint32) (best: int) (events: Event list) (main: IO<Stream<IO<unit>>>) : Op list list =
|
||||
(IO.traceWith seed best main).Result
|
||||
|> Stream.run events
|
||||
|> List.map (fun io -> (IO.traceWith seed best io).Ops)
|
||||
|
||||
// The Module 9 helper is the "no stored best" case.
|
||||
let play (seed: uint32) (events: Event list) (main: IO<Stream<IO<unit>>>) : Op list list = playWith seed 0 events main
|
||||
Reference in new issue
Block a user