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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
open Knights_tour
module PointMap = Map.Make(Point)
type square =
| Occupied of Polyomino.t
| Vacant
| Blocked
type t = {
size: Point.t;
squares: Polyomino.t option PointMap.t;
}
let size {size;_} = size
let get {squares;_} pt =
match PointMap.find_opt pt squares with
| None -> Blocked
| Some (Some p) -> Occupied p
| Some None -> Vacant
let vacant {squares;_} =
let vacancies = ref PointSet.empty in
squares |> PointMap.iter (fun pt square ->
if Option.is_none square then
vacancies := PointSet.add pt !vacancies
);
!vacancies
let load_string square_of_char img =
let squares = ref PointMap.empty in
Lines.of_string img
|> Seq.iteri (fun y line ->
line |> String.iteri (fun x ch ->
match square_of_char ch with
| Blocked -> ()
| Vacant -> squares := PointMap.add Point.{x;y} None !squares
| Occupied p -> squares := PointMap.add Point.{x;y} (Some p) !squares
)
);
let points = ref PointSet.empty in
!squares |> PointMap.iter (fun pt _ -> points:= PointSet.add pt !points);
{
size = Point.{x = 1 + PointSet.max_x !points; y = 1 + PointSet.max_y !points};
squares = !squares
}
let of_string img =
let vacancies = PointSet.of_string img |> PointSet.normalize_translation in
{
size = Point.{
x = PointSet.max_x vacancies + 1;
y = PointSet.max_y vacancies + 1;
};
squares = PointSet.fold (fun vacant board -> PointMap.add vacant None board) vacancies PointMap.empty
}
let rectangle w h =
let open Searchspace in
int_range 0 (w - 1) |=> (fun x ->
int_range 0 (h - 1) |-> (fun y -> (Point.{x;y}, None))
)
|> to_seq
|> PointMap.of_seq
|> fun squares -> {
size = Point.{x=w;y=h};
squares
}
let classic = of_string "
########
########
########
###..###
###..###
########
########
########"
let%expect_test "Board of_string |> vacant" =
let open Printf in
let board = classic in
printf "min x: %d y: %d\n" (PointSet.min_x (vacant board)) (PointSet.min_y (vacant board));
printf "max x: %d y: %d\n" (PointSet.max_x (vacant board)) (PointSet.max_y (vacant board));
printf "size = (%d, %d)\n" (size board).x (size board).y;
printf "\n";
printf "%s" (PointSet.to_string (vacant board));
[%expect{|
min x: 0 y: 0
max x: 7 y: 7
size = (8, 8)
########
########
########
###..###
###..###
########
########
######## |}]
let put board points poly =
PointSet.fold (fun pt board ->
match PointMap.find pt board with
| None -> PointMap.add pt (Some poly) board
| _ -> failwith "Invalid placement. Polyomino should only be placed on vacant squares"
) points board.squares
|> (fun squares -> {board with squares})
let to_string board =
let square_to_char = (function
| Vacant -> '.'
| Occupied p -> Polyomino.name p
| Blocked -> '#'
) in
let size = board.size in
let image = Buffer.create ((size.x+1)*(size.y+1)) in
for y = 0 to size.y-1 do
for x = 0 to size.x-1 do
Buffer.add_char image (square_to_char (get board {x;y}))
done;
Buffer.add_char image '\n'
done;
Buffer.contents image
let%expect_test "Place a polyomino" =
let polyos = Polyomino.of_order 5 in
let board = classic in
polyos |> List.iteri (fun i poly ->
Printf.printf "%d:\n" (i+1);
let board = put board (Polyomino.points poly) poly in
Printf.printf "%s\n" (to_string board)
)
; [%expect{|
1:
AAAAA...
........
........
...##...
...##...
........
........
........
2:
BBBB....
B.......
........
...##...
...##...
........
........
........
3:
CCCC....
.C......
........
...##...
...##...
........
........
........
4:
DDD.....
DD......
........
...##...
...##...
........
........
........
5:
EEE.....
E.E.....
........
...##...
...##...
........
........
........
6:
FFF.....
F.......
F.......
...##...
...##...
........
........
........
7:
GGG.....
.G......
.G......
...##...
...##...
........
........
........
8:
HHH.....
..HH....
........
...##...
...##...
........
........
........
9:
II......
.II.....
.I......
...##...
...##...
........
........
........
10:
JJ......
.JJ.....
..J.....
...##...
...##...
........
........
........
11:
KK......
.K......
.KK.....
...##...
...##...
........
........
........
12:
.L......
LLL.....
.L......
...##...
...##...
........
........
........ |}]
let draw_size = 64
let margin = 2
let color_table : Graphics.color array =
let color_levels = [230; 130; 30] in
let ct = Array.make (List.length color_levels |> (fun x-> x*x*x)) Graphics.black in
let idx = ref 0 in
color_levels |> List.iter (fun r ->
color_levels |> List.iter (fun g ->
color_levels |> List.iter (fun b ->
ct.(!idx) <- Graphics.rgb r g b;
idx := !idx + 1
)
)
);
ct
let color_code pento_name =
(Char.code pento_name - Char.code 'A') mod (Array.length color_table)
let color_of = function
| Vacant -> Graphics.white
| Blocked -> Graphics.black
| Occupied p -> color_table.(color_code (Polyomino.name p))
let to_screen xy = (xy*draw_size+margin)
let vert_line x y =
Graphics.moveto (to_screen x) (to_screen y);
Graphics.lineto (to_screen x) (to_screen (y+1))
let hori_line x y =
Graphics.moveto (to_screen x) (to_screen y);
Graphics.lineto (to_screen (x+1)) (to_screen y)
let draw_border board =
Graphics.set_line_width 1;
Graphics.set_color (Graphics.black);
Graphics.draw_rect
((to_screen 0) - 2) ((to_screen 0) - 2)
(draw_size * board.size.x + 3) (draw_size * board.size.y + 3)
let draw ?(black_and_white=false) (board:t) =
Graphics.set_font "12x24";
Graphics.clear_graph ();
let sz = size board in
if not black_and_white then begin
for y = 0 to sz.y-1 do
for x = 0 to sz.x-1 do
Graphics.set_color (get board {x;y} |> color_of);
Graphics.fill_rect (to_screen x) (to_screen y) draw_size draw_size
done
done
end;
Graphics.set_line_width 2;
Graphics.set_color (if black_and_white then Graphics.black else Graphics.white);
for y = 0 to sz.y do
for x = 0 to sz.x do
if get board {x;y} <> get board {x=x-1;y} then
vert_line x y;
if get board {x;y} <> get board {x;y=y-1} then
hori_line x y;
done
done;
if not black_and_white then
draw_border board
let init_graphics board_sz =
let open Point in
let draw_sz = draw_size in
Graphics.open_graph (Printf.sprintf " %dx%d" (board_sz.x * draw_sz + 2 * margin) (board_sz.y * draw_sz + 2 * margin));
Graphics.set_font "12x24";