unison/unison-src/tests/tictactoe.u

37 lines
833 B
Plaintext
Raw Normal View History

2018-08-16 18:29:24 +03:00
-- board piece
2021-08-24 21:33:27 +03:00
structural type P = X | O | E
2018-08-16 18:29:24 +03:00
2021-08-24 21:33:27 +03:00
structural type Board = Board P P P P P P P P P
2018-08-16 18:29:24 +03:00
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
2018-08-16 18:29:24 +03:00
isWin : Board -> Optional P
isWin board =
same : P -> P -> P -> Optional P
same a b c = if (a == b) && (a == c) && (not (a == E))
2018-08-16 18:29:24 +03:00
then Some a
else None
match board with
2018-08-16 18:29:24 +03:00
-- 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))
2018-08-16 18:29:24 +03:00
2019-02-12 19:15:46 +03:00
> isWin (Board X O X
2018-08-16 18:29:24 +03:00
O X X
O E X)