Sha256: 57ccc773968a3f36cb9aaa655f7eea58226b54f71136e4362f7e69476b592c42

Contents?: true

Size: 1.62 KB

Versions: 151

Compression:

Stored size: 1.62 KB

Contents

open Core

type field =
  | Mine
  | Empty of int (* Empty spot, number is adjacent mines *)

type board = { fields: field array array; width: int; height: int }

let board_of_strings strings =
  let to_field = function
    | '*' -> Mine
    | _   -> Empty 0 in
  let f s = String.to_list s |> List.map ~f:to_field |> Array.of_list in
  let fields = List.map strings ~f |> Array.of_list in
  let height = Array.length fields in
  let width = if height > 0 then Array.length fields.(0) else 0 in
  { fields; width; height }

let strings_of_board board =
  let to_char = function
    | Mine    -> '*'
    | Empty 0 -> ' '
    | Empty n -> Char.of_int_exn (Char.to_int '0' + n) in
  let f a = Array.to_list a |> List.map ~f:to_char |> String.of_char_list in
  Array.to_list board.fields |> List.map ~f

let adjacent_offsets =
  let open List in
  ([-1; 0; 1] >>= fun x ->
   [-1; 0; 1] >>= fun y ->
   return (x, y))
  |> List.filter ~f:(fun c -> c <> (0, 0))

let mark_mine board c =
  let valid_coord (x, y) = x >= 0 && x < board.width && y >= 0 && y < board.height in
  let add_vec (ax, ay) (bx, by) = (ax + bx, ay + by) in
  let inc_field = function
    | Mine -> Mine
    | Empty n -> Empty (n + 1) in
  List.map adjacent_offsets ~f:(fun o -> add_vec c o)
  |> List.filter ~f:valid_coord
  |> List.iter ~f:(fun (x, y) -> board.fields.(y).(x) <- inc_field board.fields.(y).(x))

let annotate strings =
  let board = board_of_strings strings in
  for x = 0 to board.width - 1 do
    for y = 0 to board.height - 1 do
      match board.fields.(y).(x) with
      | Mine    -> mark_mine board (x, y)
      | Empty _ -> ()
    done
  done;
  strings_of_board board

Version data entries

151 entries across 151 versions & 1 rubygems

Version Path
trackler-2.1.0.53 tracks/ocaml/exercises/minesweeper/example.ml
trackler-2.1.0.52 tracks/ocaml/exercises/minesweeper/example.ml
trackler-2.1.0.51 tracks/ocaml/exercises/minesweeper/example.ml
trackler-2.1.0.50 tracks/ocaml/exercises/minesweeper/example.ml
trackler-2.1.0.49 tracks/ocaml/exercises/minesweeper/example.ml
trackler-2.1.0.48 tracks/ocaml/exercises/minesweeper/example.ml
trackler-2.1.0.47 tracks/ocaml/exercises/minesweeper/example.ml
trackler-2.1.0.46 tracks/ocaml/exercises/minesweeper/example.ml
trackler-2.1.0.45 tracks/ocaml/exercises/minesweeper/example.ml
trackler-2.1.0.44 tracks/ocaml/exercises/minesweeper/example.ml
trackler-2.1.0.43 tracks/ocaml/exercises/minesweeper/example.ml
trackler-2.1.0.42 tracks/ocaml/exercises/minesweeper/example.ml
trackler-2.1.0.41 tracks/ocaml/exercises/minesweeper/example.ml
trackler-2.1.0.40 tracks/ocaml/exercises/minesweeper/example.ml
trackler-2.1.0.39 tracks/ocaml/exercises/minesweeper/example.ml
trackler-2.1.0.38 tracks/ocaml/exercises/minesweeper/example.ml
trackler-2.1.0.37 tracks/ocaml/exercises/minesweeper/example.ml
trackler-2.1.0.36 tracks/ocaml/exercises/minesweeper/example.ml
trackler-2.1.0.34 tracks/ocaml/exercises/minesweeper/example.ml
trackler-2.1.0.33 tracks/ocaml/exercises/minesweeper/example.ml