ðŸŽ‰ Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io ðŸŽ‰

shandaman's solution

to Minesweeper in the Erlang Track

Published at Apr 07 2021 · 0 comments
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 |
+-----+
``````

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

``````%% Generated with 'testgen v0.2.0'
%% Revision 1 of the exercises generator was used
%% https://github.com/exercism/problem-specifications/raw/42dd0cea20498fd544b152c4e2c0a419bb7e266a/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=[

],
{"no rows",
?_assertMatch(Expected, minesweeper:annotate(Input))}.

'2_no_columns_test_'() ->
Input=[
""
],
Expected=[
""
],
{"no columns",
?_assertMatch(Expected, minesweeper:annotate(Input))}.

'3_no_mines_test_'() ->
Input=[
"   ",
"   ",
"   "
],
Expected=[
"   ",
"   ",
"   "
],
{"no mines",
?_assertMatch(Expected, minesweeper:annotate(Input))}.

'4_minefield_with_only_mines_test_'() ->
Input=[
"***",
"***",
"***"
],
Expected=[
"***",
"***",
"***"
],
{"minefield with only mines",
?_assertMatch(Expected, minesweeper:annotate(Input))}.

'5_mine_surrounded_by_spaces_test_'() ->
Input=[
"   ",
" * ",
"   "
],
Expected=[
"111",
"1*1",
"111"
],
{"mine surrounded by spaces",
?_assertMatch(Expected, minesweeper:annotate(Input))}.

'6_space_surrounded_by_mines_test_'() ->
Input=[
"***",
"* *",
"***"
],
Expected=[
"***",
"*8*",
"***"
],
{"space surrounded by mines",
?_assertMatch(Expected, minesweeper:annotate(Input))}.

'7_horizontal_line_test_'() ->
Input=[
" * * "
],
Expected=[
"1*2*1"
],
{"horizontal line",
?_assertMatch(Expected, minesweeper:annotate(Input))}.

'8_horizontal_line_mines_at_edges_test_'() ->
Input=[
"*   *"
],
Expected=[
"*1 1*"
],
{"horizontal line, mines at edges",
?_assertMatch(Expected, minesweeper:annotate(Input))}.

'9_vertical_line_test_'() ->
Input=[
" ",
"*",
" ",
"*",
" "
],
Expected=[
"1",
"*",
"2",
"*",
"1"
],
{"vertical line",
?_assertMatch(Expected, minesweeper:annotate(Input))}.

'10_vertical_line_mines_at_edges_test_'() ->
Input=[
"*",
" ",
" ",
" ",
"*"
],
Expected=[
"*",
"1",
" ",
"1",
"*"
],
{"vertical line, mines at edges",
?_assertMatch(Expected, minesweeper:annotate(Input))}.

'11_cross_test_'() ->
Input=[
"  *  ",
"  *  ",
"*****",
"  *  ",
"  *  "
],
Expected=[
" 2*2 ",
"25*52",
"*****",
"25*52",
" 2*2 "
],
{"cross",
?_assertMatch(Expected, minesweeper:annotate(Input))}.

'12_large_minefield_test_'() ->
Input=[
" *  * ",
"  *   ",
"    * ",
"   * *",
" *  * ",
"      "
],
Expected=[
"1*22*1",
"12*322",
" 123*2",
"112*4*",
"1*22*2",
"111111"
],
{"large minefield",
?_assertMatch(Expected, minesweeper:annotate(Input))}.``````
``````-module(minesweeper).

-export([annotate/1, count/1, countColumn/4, surround/3, cell/2]).

annotate(Minefield) ->
Dict = count(Minefield),
annotate(Minefield, Dict, [], 0).

annotate([], _, Acc, _) ->
lists:reverse(Acc);
annotate([Head | Tail], Dict, Acc, Row) ->
annotate(Tail, Dict, [annotate(Head, Dict, "", Row, 0) | Acc], Row + 1).

annotate([], _, Acc, _, _) ->
string:reverse(Acc);
annotate([42 | Tail], Dict, Acc, Row, Col) ->
annotate(Tail, Dict, [42 | Acc], Row, Col + 1);
annotate([_ | Tail], Dict, Acc, Row, Col) ->
Val = maps:get({Row, Col}, Dict, 0),
if Val =/= 0 ->
annotate(Tail, Dict, [integer_to_list(Val) | Acc], Row, Col + 1);
true ->
annotate(Tail, Dict, [" " | Acc], Row, Col + 1)
end.

count(List) ->
count(List, 0, #{}).

count([], _, Dict) ->
Dict;
count([Head | Tail], CurrentRow, Dict) ->
count(Tail, CurrentRow + 1, countColumn(Head, CurrentRow, 0, Dict)).

countColumn([], _, _, Dict) ->
Dict;
countColumn([42 | Tail], CurrentRow, CurrentColumn, Dict) ->
countColumn(Tail,
CurrentRow,
CurrentColumn + 1,
surround(CurrentRow, CurrentColumn, Dict));
countColumn([_ | Tail], CurrentRow, CurrentColumn, Dict) ->
countColumn(Tail, CurrentRow, CurrentColumn + 1, Dict).

surround(Row, Column, Dict) ->
Cells =
[{X + Row, Y + Column} || X <- [-1, 0, 1], Y <- [-1, 0, 1], (X =/= 0) or (Y =/= 0)],
lists:foldl(fun(X, Acc) -> cell(X, Acc) end, Dict, Cells).

cell({Row, Column}, Dict) ->
Init = maps:get({Row, Column}, Dict, 0),
maps:put({Row, Column}, Init + 1, Dict).``````

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?