🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉 # Eggman's solution

## to Minesweeper in the Erlang Track

Published at Oct 07 2020 · 0 comments
Instructions
Test suite
Solution

#### Note:

This exercise has changed since this solution was written.

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 |
+-----+
``````

## Running tests

In order to run the tests, issue the following command from the exercise directory:

For running the tests provided, `rebar3` is used as it is the official build and dependency management tool for erlang now. Please refer to the tracks installation instructions on how to do that.

In order to run the tests, you can issue the following command from the exercise directory.

``````\$ rebar3 eunit
``````

## Questions?

For detailed information about the Erlang track, please refer to the help page on the Exercism site. This covers the basic information on setting up the development environment expected by the exercises.

## Submitting Incomplete Solutions

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

### minesweeper_tests.erl

``````%% Based on canonical data version 1.1.0
%% https://github.com/exercism/problem-specifications/raw/master/exercises/minesweeper/canonical-data.json
%% This file is automatically generated from the exercises canonical data.

-module(minesweeper_tests).

-include_lib("erl_exercism/include/exercism.hrl").
-include_lib("eunit/include/eunit.hrl").

'1_no_rows_test'() ->
Input=[

],
Expected=[

],
?assertMatch(Expected, minesweeper:annotate(Input)).

'2_no_columns_test'() ->
Input=[
""
],
Expected=[
""
],
?assertMatch(Expected, minesweeper:annotate(Input)).

'3_no_mines_test'() ->
Input=[
"   ",
"   ",
"   "
],
Expected=[
"   ",
"   ",
"   "
],
?assertMatch(Expected, minesweeper:annotate(Input)).

'4_minefield_with_only_mines_test'() ->
Input=[
"***",
"***",
"***"
],
Expected=[
"***",
"***",
"***"
],
?assertMatch(Expected, minesweeper:annotate(Input)).

'5_mine_surrounded_by_spaces_test'() ->
Input=[
"   ",
" * ",
"   "
],
Expected=[
"111",
"1*1",
"111"
],
?assertMatch(Expected, minesweeper:annotate(Input)).

'6_space_surrounded_by_mines_test'() ->
Input=[
"***",
"* *",
"***"
],
Expected=[
"***",
"*8*",
"***"
],
?assertMatch(Expected, minesweeper:annotate(Input)).

'7_horizontal_line_test'() ->
Input=[
" * * "
],
Expected=[
"1*2*1"
],
?assertMatch(Expected, minesweeper:annotate(Input)).

'8_horizontal_line_mines_at_edges_test'() ->
Input=[
"*   *"
],
Expected=[
"*1 1*"
],
?assertMatch(Expected, minesweeper:annotate(Input)).

'9_vertical_line_test'() ->
Input=[
" ",
"*",
" ",
"*",
" "
],
Expected=[
"1",
"*",
"2",
"*",
"1"
],
?assertMatch(Expected, minesweeper:annotate(Input)).

'10_vertical_line_mines_at_edges_test'() ->
Input=[
"*",
" ",
" ",
" ",
"*"
],
Expected=[
"*",
"1",
" ",
"1",
"*"
],
?assertMatch(Expected, minesweeper:annotate(Input)).

'11_cross_test'() ->
Input=[
"  *  ",
"  *  ",
"*****",
"  *  ",
"  *  "
],
Expected=[
" 2*2 ",
"25*52",
"*****",
"25*52",
" 2*2 "
],
?assertMatch(Expected, minesweeper:annotate(Input)).

'12_large_minefield_test'() ->
Input=[
" *  * ",
"  *   ",
"    * ",
"   * *",
" *  * ",
"      "
],
Expected=[
"1*22*1",
"12*322",
" 123*2",
"112*4*",
"1*22*2",
"111111"
],
?assertMatch(Expected, minesweeper:annotate(Input)).``````
``````-module(minesweeper).

-export([annotate/1]).

annotate([]) ->
[];
annotate(["" | _] = Minefield) ->
Minefield;
annotate(Minefield) ->
{Rows, Cols} = minefield_size(Minefield),
Indexes = [{Row, Col} || Row <- lists:seq(1, Rows), Col <- lists:seq(1, Cols)],
FilledIn = [mine_count(Row, Col, Minefield) || {Row, Col} <- Indexes],
[lists:sublist(FilledIn, Row * Cols + 1, Cols) || Row <- lists:seq(0, Rows - 1)].

minefield_size([FirstRow|_] = Minefield) ->
{length(Minefield), length(FirstRow)}.

mine_count(Row, Col, Minefield) ->
position_value(Row, Col, Minefield, minefield_value(Row, Col, Minefield)).

position_value(_, _, _, \$*) ->
\$*;
position_value(Row, Col, Minefield, _) ->
case lists:sum(neighbor_mines(Row, Col, Minefield)) of
0 ->
\$\s;
N ->
N + \$0
end.

minefield_value(Row, Col, Minefield) ->
lists:nth(Col, lists:nth(Row, Minefield)).

neighbor_mines(Row, Col, Minefield) ->
{Rows, Cols} = minefield_size(Minefield),
lists:map(fun ({NRow, NCol}) ->
neighbor_mine(NRow, NCol, Minefield, Rows, Cols)
end,
neighbors(Row, Col)).

neighbor_mine(Row, Col, _, Rows, Cols)
when Row < 1 orelse Row > Rows orelse Col < 1 orelse Col > Cols ->
0;
neighbor_mine(Row, Col, Minefield, _, _) ->
case minefield_value(Row, Col, Minefield) of
\$* ->
1;
_ ->
0
end.

neighbors(Row, Col) ->
[{NRow, NCol} || NRow <- lists:seq(Row - 1, Row + 1), NCol <- lists:seq(Col - 1, Col + 1)]
-- [{Row, Col}].``````

## 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?