--- Tic-tac-toe is a paper-and-pencil game for two players who take turns marking the spaces
--- in a three-by-three grid with X or O.
--- The player who succeeds in placing three of their marks in a horizontal, vertical, or
--- diagonal row is the winner. It is a solved game, with a forced draw assuming best play from both players.
module Logic.Game;
open import Stdlib.Data.Nat.Ord;
open import Stdlib.Prelude;
open import Logic.Extra public;
open import Logic.Board public;
open import Logic.GameState public;
--- Checks if we reached the end of the game.
checkState : GameState → GameState;
checkState (state b p e) :=
if
(won (state b p e))
(state
b
p
(terminate ("Player " ++str showSymbol (switch p) ++str " wins!")))
(if
(draw (state b p e))
(state b p (terminate "It's a draw!"))
(state b p e));
--- Given a player attempted move, updates the state accordingly.
playMove : Maybe Nat → GameState → GameState;
playMove nothing (state b p _) :=
state b p (continue "\nInvalid number, try again\n");
playMove (just k) (state (board s) player e) :=
if
(not (elem (==) k (possibleMoves (flatten s))))
(state
(board s)
player
(continue "\nThe square is already occupied, try again\n"))
(checkState
(state (board (map (map (replace player k)) s)) (switch player) noError));
--- Returns ;just; if the given ;Nat; is in range of 1..9
validMove : Nat → Maybe Nat;
validMove n := if (n <= 9 && n >= 1) (just n) nothing;
Last modified on 2023-05-08 11:40 UTC