--- 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.
--- The module Logic.Game contains the game logic.
module CLI.TicTacToe;
open import Stdlib.Data.Nat.Ord;
open import Stdlib.Prelude;
open import Logic.Game;
--- A ;String; that prompts the user for their input
prompt : GameState → String;
prompt x :=
"\n"
++str showGameState x
++str "\nPlayer "
++str showSymbol (player x)
++str ": ";
nextMove : GameState → String → GameState;
nextMove s := flip playMove s ∘ validMove ∘ stringToNat;
--- Main loop
terminating
run : GameState → IO;
run (state b p (terminate msg)) :=
printStringLn
("\n" ++str showGameState (state b p noError) ++str "\n" ++str msg);
run (state b p (continue msg)) :=
printString (msg ++str prompt (state b p noError))
>> readLn (run ∘ nextMove (state b p noError));
run x := printString (prompt x) >> readLn (run ∘ nextMove x);
--- The welcome message
welcome : String;
welcome :=
"MiniTicTacToe\n-------------\n\nType a number then ENTER to make a move";
--- The entry point of the program
main : IO;
main := printStringLn welcome >> run beginState;
Last modified on 2023-05-08 11:40 UTC