-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrid.ml
206 lines (174 loc) · 5.16 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
open Core.Std
open Utils
type grid = int option list list
type t = {grid: grid; score: float}
type content =
| Empty
| Free of int
| Locked of int
let grid_size = 4
let empty =
[ [ None; None; None; None ];
[ None; None; None; None ];
[ None; None; None; None ];
[ None; None; None; None ]; ]
let map grid ~f =
List.map grid ~f:(fun row -> List.map row ~f)
let fold grid ~init ~f =
List.fold grid ~init ~f:(fun acc row -> List.fold row ~init:acc ~f)
let to_content grid =
map grid ~f:(function
| Some x -> Free x
| None -> Empty
)
let from_content grid =
map grid ~f:(function
| Empty -> None
| Free x -> Some x
| Locked x -> Some x
)
let sample_new () =
if Random.float 1. <. 0.9 then Some 2 else Some 4
let empty_locations {grid} =
fold grid ~init:(0, []) ~f:(fun (id, locs) elem ->
match elem with
| None -> (id + 1, id::locs)
| _ -> (id + 1, locs)
) |> snd |> List.rev
let add_tile {grid; score} ~loc ~tile =
let loc = 15 - loc in (* fold_right inverts the order *)
let grid' =
List.fold_right grid ~init:(0, []) ~f:(fun row (count, res) ->
let (count, row') =
List.fold_right row ~init:(count, []) ~f:(fun elem (c, r) ->
if c = loc then (c + 1, tile :: r) else ( c + 1, elem :: r)
)
in
(count, row' :: res)
) |> snd
in {grid = grid'; score}
let add_random grid =
let empty_locs = empty_locations {grid; score = 0.} in
let nb_none = List.length empty_locs in
(* FIXME can this be used for end of game detection? *)
let () = assert (nb_none > 0) in
let new_id = Random.int nb_none in
let loc = List.nth_exn empty_locs new_id in
let tile = sample_new () in
(* let () = (
printf "emtpy locs:";
List.iter empty_locs ~f:(printf " %d");
printf "\n";
printf "loc index: %d\n" new_id;
printf "chosen loc: %d\n" loc;
) in *)
let {grid} = add_tile {grid; score = 0.} ~loc ~tile in
(* FIXME something is wrong, overwriting non-empty tile... *)
grid
let new_game () =
let grid = empty |> add_random |> add_random in
{grid; score = 0.0 }
type move =
| Left
| Right
| Up
| Down
type move_res =
| Good of t
| Useless of t
| Game_over of t
let rev = List.map ~f:List.rev
let rec move_atomic = function
| [] | [_] as l -> l
| Empty :: tail -> (move_atomic tail) @ [Empty]
| Free x :: Free y :: tail ->
if x = y then Locked (x + y) :: move_atomic (Empty :: tail)
else Free x :: move_atomic (Free y :: tail)
| Free x :: tail -> Free x :: move_atomic tail
| Locked x :: tail -> Locked x :: move_atomic tail
let rec move_list l =
let l' = move_atomic l in
if l = l'
then l
else move_list l'
let move_left = List.map ~f:move_list
let move_right grid = rev grid |> move_left |> rev
let move_pure grid m =
let grid = to_content grid in
let grid = match m with
| Left -> move_left grid
| Right -> move_right grid
| Up -> List.transpose_exn grid |> move_left |> List.transpose_exn
| Down -> List.transpose_exn grid |> move_right |> List.transpose_exn
in
let merged_scores = fold grid ~init:0 ~f:(fun acc value ->
match value with
| Locked x -> acc + x
| _ -> acc
) in
from_content grid, (Float.of_int merged_scores)
let game_over grid =
move_pure grid Left |> fst = grid &&
move_pure grid Right |> fst = grid &&
move_pure grid Up |> fst = grid &&
move_pure grid Down |> fst = grid
let move {grid; score} m =
let moved, merged_scores = move_pure grid m in
if moved = grid then
Useless {grid = moved; score}
else
let res = add_random moved in
if game_over res then
Game_over {grid = res; score = score +. merged_scores}
else
Good {grid = res; score = score +. merged_scores}
let to_llist {grid; score} = grid
let to_string {grid; score} =
let strings = List.map grid ~f:(
List.map ~f:(function
| None -> ""
| Some x -> Int.to_string x
)) in
let max_len =
List.max_elt ~cmp:Int.ascending (List.map strings ~f:(fun l ->
List.max_elt ~cmp:Int.ascending (List.map l ~f:String.length) |> unwrap
)) |> unwrap in
let pad x = x ^ String.make (max_len - String.length x) ' ' in
let strings = List.map strings ~f:(List.map ~f:pad) in
let strings = List.map strings ~f:(List.intersperse ~sep:" | ") in
let nb_rows = List.length grid in
strings
|> List.intersperse ~sep:([String.make (nb_rows * (max_len + 3)) '-'])
|> List.intersperse ~sep:(["\n"])
|> List.concat
|> String.concat
let nb_full grid =
fold grid ~init:0 ~f:(fun acc x ->
match x with
| Some _ -> acc + 1
| None -> acc
)
let nb_empty {grid; score} =
let grid_size = List.length grid in
(grid_size * grid_size) - (nb_full grid)
let heuristic_eval_pos {grid; score} =
let sum_cases = fold grid ~init:0 ~f:(fun acc x ->
match x with
| Some y -> acc + y
| None -> acc
) in
let nb_cases = nb_full grid in
Float.of_int sum_cases /. Float.of_int nb_cases
let eval_pos {score; } =
score
let move_to_string = function
| Left -> "Left"
| Right -> "Right"
| Down -> "Down"
| Up -> "Up"
let highest {grid; score} =
fold grid ~init:0 ~f:(fun max x ->
match x with
| Some y -> Int.max max y
| None -> max
)