unison/unison-src/tests/tictactoe.u
2021-12-15 18:06:35 -06:00

37 lines
833 B
Plaintext

-- board piece
structural type P = X | O | E
structural type Board = Board P P P P P P P P P
use Board Board
use P O X E
use Optional Some None
a |> f = f a
orElse a b =
match a with
None -> b
a -> a
isWin : Board -> Optional P
isWin board =
same : P -> P -> P -> Optional P
same a b c = if (a == b) && (a == c) && (not (a == E))
then Some a
else None
match board with
-- vertical top/center/bottom
-- horizontal left/center/right
-- diagonal rising/falling
Board a b c
d e f
g h i ->
(same a b c |> orElse (same d e f) |> orElse (same g h i)
|> orElse (same a d g) |> orElse (same b e h) |> orElse (same c f i)
|> orElse (same a e i) |> orElse (same g e c))
> isWin (Board X O X
O X X
O E X)