-
Notifications
You must be signed in to change notification settings - Fork 0
/
Grid.ml
100 lines (86 loc) · 2.89 KB
/
Grid.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
type box = Default | Player of string | Resolved of string
type t = box list
let create () = [ Default; Default; Default; Default; Default; Default; Default; Default; Default]
let x_wins () = [ Resolved "\\"; Resolved " "; Resolved "/"; Resolved " "; Resolved "X"; Resolved " "; Resolved "/"; Resolved " "; Resolved "\\"]
let o_wins () = [ Resolved "/"; Resolved "-"; Resolved "\\"; Resolved "|"; Resolved "O"; Resolved "|"; Resolved "\\"; Resolved "-"; Resolved "/"]
let get_box box =
match box with
| Default -> "-"
| Player box -> box
| Resolved box -> box
let get_index (y, x) = ((y mod 3) * 3) + (x mod 3)
let get_player (grid : t) =
match (List.nth grid 4) with
| Resolved player -> Resolved player
| _ -> Default
let display grid y =
let index = y * 3 in
let rec loop lst n =
match lst with
| [] -> ()
| elem :: tail when n = (index + 2) -> print_string (get_box elem)
| elem :: tail when n >= index ->
begin
print_string (get_box elem);
print_char ' ';
loop tail (n + 1)
end
| elem :: tail -> loop tail (n + 1)
in
loop grid 0
let check grid coord =
let rec loop lst n =
match lst with
| [] -> false
| elem::tail when n = 0 -> (elem = Default)
| elem::tail -> loop tail (n - 1)
in
loop grid (get_index coord)
let place (grid: t) coord player : t =
let rec loop lst n =
match lst with
| [] -> []
| elem :: tail when n = 0 -> Player player :: tail
| elem :: tail -> elem :: loop tail (n - 1)
in
loop grid (get_index coord)
let resolve_x (grid: t) =
let rec in_check lst =
match lst with
| Player a :: Player b :: Player c :: _ when a = b && b = c -> true
| _ :: _ :: _ :: next -> in_check next
| _ -> false
in
in_check grid
let resolve_y (grid: t) =
let rec in_check = function
| Player a :: _ :: _ :: Player b :: _ :: _ :: Player c :: _ when a = b && b = c -> true
| _ :: next -> in_check next
| _ -> false
in
in_check grid
let resolve_z (grid: t) =
let rec in_left = function
| Player a :: _ :: _ :: _ :: Player b :: tail when a = b && in_left (Player b :: tail) -> true
| [c] -> true
| _ -> false
in
let in_right lst =
let in_check = function
| Player a :: _ :: Player b :: _ :: Player c :: _ when a = b && b = c -> true
| _ -> false
in
match lst with
| _ :: _ :: xs -> in_check xs
| _ -> false
in
(in_left grid) || (in_right grid)
let grid_is_full (grid : t) =
let rec loop = function
| [] -> true
| Player _ :: xs -> loop xs
| _ -> false
in
loop grid
let resolve (grid: t) =
(grid_is_full grid || resolve_x grid || resolve_y grid || resolve_z grid)