/// m02 model: the data the whole game is built from. Records and unions are immutable. module Snake.Model /// A grid cell. Records compare structurally: two points with the same X and Y are `=`. type Point = { X: int; Y: int } type Direction = | Up | Down | Left | Right type Status = | Playing | Paused | GameOver | Won /// Everything that can happen to the game, as data. type Action = | Tick | Turn of Direction | TogglePause | Restart | Frame /// The whole game. `Snake` is head first; `Seed` is the unsigned 32-bit random seed. type GameState = { Width: int Height: int Snake: Point list Dir: Direction NextDir: Direction Apple: Point option Seed: uint32 Score: int Status: Status Frame: int } let delta (dir: Direction) : Point = match dir with | Up -> { X = 0; Y = -1 } | Down -> { X = 0; Y = 1 } | Left -> { X = -1; Y = 0 } | Right -> { X = 1; Y = 0 } let opposite (dir: Direction) : Direction = match dir with | Up -> Down | Down -> Up | Left -> Right | Right -> Left let initialState (seed: uint32) : GameState = { Width = 20 Height = 20 Snake = [ { X = 10; Y = 10 }; { X = 9; Y = 10 }; { X = 8; Y = 10 } ] Dir = Right NextDir = Right Apple = None Seed = seed Score = 0 Status = Playing Frame = 0 }