🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of hyphenrf

hyphenrf's solution

to Minesweeper in the OCaml Track

Published at Jul 23 2020 · 1 comment
Instructions
Test suite
Solution

Add the numbers to a minesweeper board.

Minesweeper is a popular game where the user has to find the mines using numeric hints that indicate how many mines are directly adjacent (horizontally, vertically, diagonally) to a square.

In this exercise you have to create some code that counts the number of mines adjacent to a square and transforms boards like this (where * indicates a mine):

+-----+
| * * |
|  *  |
|  *  |
|     |
+-----+

into this:

+-----+
|1*3*1|
|13*31|
| 2*2 |
| 111 |
+-----+

Getting Started

  1. Install the Exercism CLI.

  2. Install OCaml.

  3. For library documentation, follow Useful OCaml resources.

Running Tests

A Makefile is provided with a default target to compile your solution and run the tests. At the command line, type:

make

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

Feedback, Issues, Pull Requests

The exercism/ocaml repository on GitHub is the home for all of the Ocaml exercises.

If you have feedback about an exercise, or want to help implementing a new one, head over there and create an issue or submit a PR. We welcome new contributors!

test.ml

(* minesweeper - 1.1.0 *)
open Base
open OUnit2
open Minesweeper

let format_board strings =
  let width = match strings with
    | [] -> 0
    | (s::_) -> String.length s in
  let border_line = "+" ^ String.make width '-' ^ "+\n" in
  let line s = "|" ^ s ^ "|\n" in
  "\n" ^ border_line ^ String.concat (List.map strings ~f:line) ^ border_line

(* Assert Equals *)
let ae exp got =
  assert_equal exp got ~printer:format_board

let tests = [
  "no rows" >:: (fun _ ->
      let b = [] in
      let expected = [] in
      ae expected (annotate b)
    );
  "no columns" >:: (fun _ ->
      let b = [""] in
      let expected = [""] in
      ae expected (annotate b)
    );
  "no mines" >:: (fun _ ->
      let b = [
        "   ";
        "   ";
        "   ";
      ] in
      let expected = [
        "   ";
        "   ";
        "   ";
      ] in
      ae expected (annotate b)
    );
  "minefield with only mines" >:: (fun _ ->
      let b = [
        "***";
        "***";
        "***";
      ] in
      let expected = [
        "***";
        "***";
        "***";
      ] in
      ae expected (annotate b)
    );
  "mine surrounded by spaces" >:: (fun _ ->
      let b = [
        "   ";
        " * ";
        "   ";
      ] in
      let expected = [
        "111";
        "1*1";
        "111";
      ] in
      ae expected (annotate b)
    );
  "space surrounded by mines" >:: (fun _ ->
      let b = [
        "***";
        "* *";
        "***";
      ] in
      let expected = [
        "***";
        "*8*";
        "***";
      ] in
      ae expected (annotate b)
    );
  "horizontal line" >:: (fun _ ->
      let b = [" * * "] in
      let expected = ["1*2*1"] in
      ae expected (annotate b)
    );
  "horizontal line, mines at edges" >:: (fun _ ->
      let b = ["*   *"] in
      let expected = ["*1 1*"] in
      ae expected (annotate b)
    );
  "vertical line" >:: (fun _ ->
      let b = [
        " ";
        "*";
        " ";
        "*";
        " ";
      ] in
      let expected = [
        "1";
        "*";
        "2";
        "*";
        "1";
      ] in
      ae expected (annotate b)
    );
  "vertical line, mines at edges" >:: (fun _ ->
      let b = [
        "*";
        " ";
        " ";
        " ";
        "*";
      ] in
      let expected = [
        "*";
        "1";
        " ";
        "1";
        "*";
      ] in
      ae expected (annotate b)
    );
  "cross" >:: (fun _ ->
      let b = [
        "  *  ";
        "  *  ";
        "*****";
        "  *  ";
        "  *  ";
      ] in
      let expected = [
        " 2*2 ";
        "25*52";
        "*****";
        "25*52";
        " 2*2 ";
      ] in
      ae expected (annotate b)
    );
  "large minefield" >:: (fun _ ->
      let b = [
        " *  * ";
        "  *   ";
        "    * ";
        "   * *";
        " *  * ";
        "      ";
      ] in
      let expected = [
        "1*22*1";
        "12*322";
        " 123*2";
        "112*4*";
        "1*22*2";
        "111111";
      ] in
      ae expected (annotate b)
    );
]

let () =
  run_test_tt_main ("minesweeper tests" >::: tests)
let getitem (row, col) board =
    try       Some Bytes.(get board.(row) col)
    with _ -> None

let cells = [ (-1,-1); (-1, 0); (-1, 1)
            ; (0, -1); (*0, 0*) (0,  1)
            ; (1, -1); (1,  0); (1,  1) ]

let bump = function
    | '1'..'7' as c -> Char.(chr (code c + 1))
    | ' ' -> '1'
    | c -> c

let annotate ls =
    let board = ls |> Array.of_list
                   |> Array.map Bytes.of_string
    in
    let flag rmine cmine =
        let surround (r, c) =
            match getitem (r+rmine, c+cmine) board with
            | Some a ->
                board.(r+rmine).[c+cmine] <- bump a
            | None -> ()
        in
        List.iter surround cells
    in
    board |> Array.iteri
             (fun r -> Bytes.iteri
             (fun c chr -> if chr = '*' then flag r c));

             board |> Array.map Bytes.to_string
                   |> Array.to_list

Community comments

Find this solution interesting? Ask the author a question to learn more.
Avatar of hyphenrf

Why were the tests and interface specified with immutable structures (List, String) when the problem seems to be of a mutable nature?

I found myself solving more naturally with Array and Bytes

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?