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
|
|
|
|
|
2021-12-16 03:06:35 +03:00
|
|
|
a |> f = f a
|
|
|
|
|
2018-08-17 02:18:45 +03:00
|
|
|
orElse a b =
|
2020-02-07 05:29:43 +03:00
|
|
|
match a with
|
2018-08-17 02:18:45 +03:00
|
|
|
None -> b
|
|
|
|
a -> a
|
|
|
|
|
2018-08-16 18:29:24 +03:00
|
|
|
isWin : Board -> Optional P
|
|
|
|
isWin board =
|
|
|
|
same : P -> P -> P -> Optional P
|
2020-09-28 03:39:26 +03:00
|
|
|
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
|
2020-02-07 05:29:43 +03:00
|
|
|
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
|
2018-08-17 02:18:45 +03:00
|
|
|
d e f
|
|
|
|
g h i ->
|
2021-12-16 02:22:47 +03:00
|
|
|
(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)
|