module Logic.GameState;

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

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

type GameState := state : Board  Symbol  Error  GameState;

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

--- Projects the player
player : GameState  Symbol
  | (state _ p _) := p;

--- initial ;GameState;
beginState : GameState :=
  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 : GameState  Bool
  | (state (board squares) _ _) :=
    any full (diagonals squares ++ rows squares ++ columns squares);

--- ;true; if there is a draw
draw : GameState  Bool
  | (state (board squares) _ _) := null (possibleMoves (flatten squares));
Last modified on 2024-07-11 16:35 UTC