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

DataTriny's solution

to Minesweeper in the Erlang Track

Published at Jul 04 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([[]]) -> [[]];
annotate(Minefield) ->
	Mines = locate_mines(Minefield),
	annotate_rows(length(Minefield), length(hd(Minefield)), Mines).

locate_mines(Field) ->
	locate_mines_on_rows(Field, 1, []).

locate_mines_on_rows([], _, Mines) -> Mines;
locate_mines_on_rows([Row | Rows], Y, Mines) ->
	locate_mines_on_rows(Rows, Y + 1, locate_mines_on_cells(Row, Y) ++ Mines).

locate_mines_on_cells(Row, Y) ->
	locate_mines_on_cells(Row, Y, 1, []).

locate_mines_on_cells([], _, _, Mines) -> Mines;
locate_mines_on_cells([$  | Cells], Y, X, Mines) ->
	locate_mines_on_cells(Cells, Y, X + 1, Mines);
locate_mines_on_cells([$* | Cells], Y, X, Mines) ->
	locate_mines_on_cells(Cells, Y, X + 1, [{Y, X} | Mines]).

annotate_rows(Y, X, Mines) ->
	annotate_rows(Y, X, Mines, []).

annotate_rows(0, _, _, Field) -> Field;
annotate_rows(Y, X, Mines, Field) ->
	annotate_rows(Y - 1, X, Mines, [annotate_cells(Y, X, Mines) | Field]).

annotate_cells(Y, X, Mines) ->
	annotate_cells(Y, X, Mines, []).

annotate_cells(_, 0, _, Row) -> Row;
annotate_cells(Y, X, Mines, Row) ->
	annotate_cells(Y, X - 1, Mines, [annotate_cell(Y, X, Mines) | Row]).

annotate_cell(Y, X, Mines) ->
	annotate_cell(Y, X, Mines, 0).

annotate_cell(_, _, [], 0) -> $ ;
annotate_cell(_, _, [], Neighbors) -> 48 + Neighbors;
annotate_cell(Y, X, [{Y, X} | _], _) -> $*;
annotate_cell(Y, X, [{Mine_y, Mine_x} | Mines], Neighbors) when Mine_y >= Y - 1, Mine_y =< Y + 1, Mine_x >= X - 1, Mine_x =< X + 1 ->
	annotate_cell(Y, X, Mines, Neighbors + 1);
annotate_cell(Y, X, [_ | Mines], Neighbors) ->
	annotate_cell(Y, X, Mines, Neighbors).

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?