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

wiserfirst's solution

to Minesweeper in the Elixir Track

Published at Aug 02 2020 · 0 comments
Test suite

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:

| 2*2 |
| 111 |

Running tests

Execute the tests with:

$ mix test

Pending tests

In the test suites, all but the first test have been skipped.

Once you get a test passing, you can unskip the next one by commenting out the relevant @tag :pending with a # symbol.

For example:

# @tag :pending
test "shouting" do
  assert Bob.hey("WATCH OUT!") == "Whoa, chill out!"

Or, you can enable all the tests by commenting out the ExUnit.configure line in the test suite.

# ExUnit.configure exclude: :pending, trace: true

If you're stuck on something, it may help to look at some of the available resources out there where answers might be found.

Submitting Incomplete Solutions

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


defmodule MinesweeperTest do
  use ExUnit.Case

  defp clean(b), do: Enum.map(b, &String.replace(&1, ~r/[^*]/, " "))

  # @tag :pending
  test "zero size board" do
    b = []
    assert Minesweeper.annotate(clean(b)) == b

  @tag :pending
  test "empty board" do
    b = ["   ", "   ", "   "]
    assert Minesweeper.annotate(clean(b)) == b

  @tag :pending
  test "board full of mines" do
    b = ["***", "***", "***"]
    assert Minesweeper.annotate(clean(b)) == b

  @tag :pending
  test "surrounded" do
    b = ["***", "*8*", "***"]
    assert Minesweeper.annotate(clean(b)) == b

  @tag :pending
  test "horizontal line" do
    b = ["1*2*1"]
    assert Minesweeper.annotate(clean(b)) == b

  @tag :pending
  test "vertical line" do
    b = ["1", "*", "2", "*", "1"]
    assert Minesweeper.annotate(clean(b)) == b

  @tag :pending
  test "cross" do
    b = [" 2*2 ", "25*52", "*****", "25*52", " 2*2 "]
    assert Minesweeper.annotate(clean(b)) == b


ExUnit.configure(exclude: :pending, trace: true)
defmodule Minesweeper do
  @doc """
  Annotate empty spots next to mines with the number of mines next to them.
  @mine "*"

  @spec annotate([String.t()]) :: [String.t()]
  def annotate([]), do: []

  def annotate(board) do
    row_count = length(board)
    col_count = board |> hd() |> String.length()
    board_sizes = {row_count, col_count}

    Enum.map(0..(row_count - 1), fn j ->
      0..(col_count - 1)
      |> Enum.map(fn i ->
        {i, j}
        |> mine?(board_sizes, board)
        |> count_mines({i, j}, board_sizes, board)
      |> Enum.join()

  defp count_mines(true, _, _, _board), do: @mine

  defp count_mines(false, {i, j}, board_sizes, board) do
    {i, j}
    |> neighbouring()
    |> Enum.count(fn index -> mine?(index, board_sizes, board) end)
    |> zero_to_space()

  defp mine?({x, y}, {row_count, col_count}, board)
       when x >= 0 and y >= 0 and x < col_count and y < row_count,
       do: board |> Enum.at(y) |> String.at(x) === @mine

  defp mine?(_, _, _board), do: false

  defp neighbouring({x, y}) do
    # only doing this when not a mine, so no need to remove {x, y} itself
    for m <- -1..1, n <- -1..1, do: {x + m, y + n}

  defp zero_to_space(0), do: " "
  defp zero_to_space(n), do: n

Community comments

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

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?