package knights_tour

  1. Overview
  2. Docs
Solves the Knight's tour puzzle; and others

Install

dune-project
 Dependency

Authors

Maintainers

Sources

knights_tour-0.0.2.tbz
sha256=8362f846492183e83e1901f73933d772c193c08b8a375480c28d0f23f0d29e11
sha512=5fe79ac95a3e5a2e01d429665fa7a8bfef422d9843758b35db2c8efc299c762c1d22974266f5e8802ee6f81e09223aa90476824d1fd93540c473cc8a357d38a3

doc/src/knights_tour.pentominos/board.ml.html

Source file board.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
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
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; (* map of squares on the board excluding blocked squares *)
}

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 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 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 = 32

module CharMap = Map.Make(Char)
let color_table : Graphics.color CharMap.t = 
  let ct = ref CharMap.empty in
  for i = 1 to 14 do
    let ch = (Char.code 'A') + i - 1 |> Char.chr in
    let r = i mod 2 in
    let i = i / 2 in
    let g = i mod 2 in
    let i = i / 2 in
    let b = i mod 2 in
    let i = i / 2 in
    let dark = (i mod 2) * 100 + 100 in
    ct := CharMap.add ch (Graphics.rgb (200-dark*r) (200-dark*g) (200-dark*b)) !ct
  done;
  !ct

let%expect_test "color table" =
  color_table |> CharMap.iter (fun k v ->
    Printf.printf "'%c' -> %d\n" k v
  )
  ; [%expect{|
    'A' -> 6605000
    'B' -> 13133000
    'C' -> 6579400
    'D' -> 13158500
    'E' -> 6604900
    'F' -> 13132900
    'G' -> 6579300
    'H' -> 13158600
    'I' -> 51400
    'J' -> 13107400
    'K' -> 200
    'L' -> 13158400
    'M' -> 51200
    'N' -> 13107200 |}]

let color_of = function 
| Vacant -> Graphics.white
| Blocked -> Graphics.black
| Occupied p -> CharMap.find (Polyomino.name p) color_table
let draw (board:t) = 
  Graphics.set_font "12x24";
  Graphics.clear_graph ();
  let sz = size board in
  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 (x*draw_size) (y*draw_size) draw_size draw_size
    done
  done
OCaml

Innovation. Community. Security.