module Logic.GameState;

open import Stdlib.Prelude;
open import Logic.Extra;
open import Logic.Board;

type Error :=
  | --- no error occurred
    noError : Error
  | --- a non-fatal error occurred
    continue : StringError
  | --- a fatal occurred
    terminate : StringError;

type GameState :=
  | state : BoardSymbolErrorGameState;

--- Textual representation of a ;GameState;
showGameState : GameStateString;
showGameState (state b _ _) := showBoard b;

--- Projects the player
player : GameStateSymbol;
player (state _ p _) := p;

--- initial ;GameState;
beginState : GameState;
beginState :=
  state
    (board
      (map
        (map empty)
        ((1 :: 2 :: 3 :: nil)
          :: (4 :: 5 :: 6 :: nil)
          :: (7 :: 8 :: 9 :: nil)
          :: nil)))
    X
    noError;

--- ;true; if some player has won the game
won : GameStateBool;
won (state (board squares) _ _) :=
  any full (diagonals squares ++ rows squares ++ columns squares);

--- ;true; if there is a draw
draw : GameStateBool;
draw (state (board squares) _ _) := null (possibleMoves (flatten squares));
Last modified on 2023-05-08 11:40 UTC