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)