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>
64 lines
3.4 KiB
Scala
64 lines
3.4 KiB
Scala
package snake
|
|
|
|
/**
|
|
* 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.
|
|
*/
|
|
final case class Session(game: GameState, best: Int, improved: Boolean)
|
|
|
|
/** m09 app: the whole game wired together as values: streams of actions in, a stream of drawings out. */
|
|
object App:
|
|
|
|
val TICK_MS: Long = 150
|
|
val FRAME_MS: Long = 25
|
|
// m10: the longest step of time the game accepts at once; a longer silence (a background tab) counts as this much.
|
|
val MAX_GAP_MS: Long = 250
|
|
|
|
// Everything that can happen to the game: a Tick every TICK_MS, and an action for each bound key.
|
|
lazy val classicActions: Stream[Action] =
|
|
Stream.interval(TICK_MS).map(_ => Action.Tick).merge(Stream.keyDowns.filterMap(Input.keyToAction))
|
|
|
|
// m10: a Frame every FRAME_MS instead of a Tick; the game itself decides (movesEvery) which frames take a step.
|
|
lazy val speedActions: Stream[Action] =
|
|
Stream.interval(FRAME_MS).map(_ => Action.Frame).merge(Stream.keyDowns.filterMap(Input.keyToAction))
|
|
|
|
// Actions folded into game states. startWith puts the initial state first so it is drawn right away.
|
|
def states(seed: Long, actions: Stream[Action]): Stream[GameState] =
|
|
actions.scan(Spawn.newGame(seed))(Game.update).startWith(Spawn.newGame(seed))
|
|
|
|
def render(state: GameState): IO[Unit] = IO.drawScene(View.view(state))
|
|
|
|
// m10: one action for the session: step the game, then compare the best before with the best after.
|
|
def sessionUpdate(session: Session, action: Action): Session =
|
|
val game = Game.update(session.game, action)
|
|
val best = math.max(session.best, game.score)
|
|
Session(game, best, improved = best > session.best)
|
|
|
|
// The same running fold as `states`, one level up.
|
|
def sessions(seed: Long, best: Int, actions: Stream[Action]): Stream[Session] =
|
|
val s0 = Session(Spawn.newGame(seed), best, improved = false)
|
|
actions.scan(s0)(sessionUpdate).startWith(s0)
|
|
|
|
// The game's picture with the best score at the top right, level with the score on the left.
|
|
def sessionView(session: Session): Scene =
|
|
View.view(session.game) :+
|
|
Shape.Text(session.game.width * View.CELL - 6, 18, s"Best: ${session.best}", "#ffffff", 16, "right")
|
|
|
|
// Draw, and save the best only on the action that raised it.
|
|
def renderSession(session: Session): IO[Unit] =
|
|
if session.improved then IO.sequence(List(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.
|
|
lazy val main: IO[Stream[IO[Unit]]] =
|
|
IO.randomSeed.flatMap(seed =>
|
|
IO.loadBest.map(best => sessions(seed, best, speedActions).map(renderSession).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).
|
|
def play(main: IO[Stream[IO[Unit]]], seed: Long, events: List[Event], best: Int = 0): List[List[Op]] =
|
|
IO.trace(main, seed, best).result.run(events).map(io => IO.trace(io, seed, best).ops)
|