unison/unison-src/tests/tictactoe2.u
2023-02-27 11:13:11 -05:00

50 lines
1014 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
foldLeft : (b -> a -> b) -> b -> [a] -> b
foldLeft f =
go z xs = match xs with
[] -> z
a +: as -> go (f z a) as
go
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
->
foldLeft orElse None
[ same a b c
, same d e f
, same g h i
, same a d g
, same b e h
, same c f i
, same a e i
, same g e c
]
x = isWin (Board X O X
O X X
O E X)