package knights_tour
Solves the 'Knights Tour' and various 'Poyomino' puzzles
Install
dune-project
Dependency
Authors
Maintainers
Sources
knights_tour-0.0.6.tbz
sha256=770624ae4e35d5a58188a1f25e16730d186df8bc4397827fe9a798ea5ca574e3
sha512=b6c7b2473ddf321afaac6b668f9c6820fb8cc872abc494b69edfdff4101c6b660645837b04801e2917c38dd9f19f79bb0a7b029d59a58ddf1e3a235659bab099
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 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 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
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 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 square_to_char = function | Vacant -> '.' | Occupied p -> Polyomino.name p | Blocked -> '#' let to_string board = 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 (* filling squares *) 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; (* drawing boundaries *) 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"; () open Collections.Persist let persistable (char2poly:(char -> Polyomino.t)) : (module Persistable with type t=t) = let char_to_square = (function | '.' -> Vacant | '#' -> Blocked | ch -> Occupied (char2poly ch) ) in let module PersistableBoard = struct type nonrec t = t let save out board = ( let size = size board in Point.save out size; for x = 0 to size.x-1 do for y = 0 to size.y-1 do let sq = get board {x;y} in output_char out (square_to_char sq) done done ) let load inp = ( let size = Point.load inp in let squares = ref PointMap.empty in for x = 0 to size.x-1 do for y = 0 to size.y-1 do let ch = input_char inp in let sq = char_to_square ch in match sq with | Blocked -> () | Vacant -> squares := PointMap.add {x;y} None !squares | Occupied p -> squares := PointMap.add {x;y} (Some p) !squares done done; {size; squares = !squares} ) end in (module PersistableBoard : Collections.Persist.Persistable with type t = t) let%expect_test "save and load a board" = let pieces = Polyomino.of_order 3 in let board = rectangle 5 4 in let char2poly name = List.find (fun candidate -> Polyomino.name candidate == name) pieces in let module Persistable = (val persistable char2poly) in List.iter (fun poly -> let placement = Polyomino.points poly in let placed = put board placement poly in Printf.printf "%s\n" (to_string placed); let (fname, output) = Filename.open_temp_file "board" "dat" in Persistable.save output placed; close_out output; let input = open_in_bin fname in let loaded = Persistable.load input in close_in input; Printf.printf "%s\n" (to_string loaded); Printf.printf "=====================\n" ) pieces ;[%expect{| AAA.. ..... ..... ..... AAA.. ..... ..... ..... ===================== BB... B.... ..... ..... BB... B.... ..... ..... ===================== |}]
sectionYPositions = computeSectionYPositions($el), 10)"
x-init="setTimeout(() => sectionYPositions = computeSectionYPositions($el), 10)"
>