forked from exercism/futhark
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.fut
More file actions
21 lines (18 loc) · 925 Bytes
/
example.fut
File metadata and controls
21 lines (18 loc) · 925 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
type state = #win | #draw | #ongoing | #invalid
local let lines: [8]i32 = [0b000000111, 0b000111000, 0b111000000, 0b001001001, 0b010010010, 0b100100100, 0b001010100, 0b100010001]
local def is_win(bitset: i32): bool =
any (\line -> ((bitset & line) == line)) lines
def game_state (board: [3][3]u8): state =
let (count_x, count_o, bitset_x, bitset_o) = loop (count_x, count_o, bitset_x, bitset_o) = (0, 0, 0, 0) for i in 0..<9 do
match board[i / 3][i % 3]
case 'X' -> (count_x + 1, count_o, bitset_x | (1 << i), bitset_o)
case 'O' -> (count_x, count_o + 1, bitset_x, bitset_o | (1 << i))
case _ -> (count_x, count_o, bitset_x, bitset_o)
in
let win_x = is_win bitset_x
let win_o = is_win bitset_o
in
if count_x > count_o + 1 || count_o > count_x || (win_x && win_o) then #invalid else
if win_x || win_o then #win else
if count_x + count_o == 9 then #draw else
#ongoing