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)