Exercism v3 launches on Sept 1st 2021. Learn more! ๐Ÿš€๐Ÿš€๐Ÿš€
Avatar of vpes

vpes's solution

to Minesweeper in the Erlang Track

Published at Oct 09 2019 · 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]).

-spec annotate(list()) -> list().
annotate([]) -> [];
annotate(Minefield=[""|_]) -> Minefield;
annotate(Minefield) -> 
	Rows=length(Minefield),
	Cols=string:length(lists:nth(1,Minefield)),
	Board = matrix_to_board(Minefield,1,maps:new()),
	lists:reverse(board_neighbours(1,1,Cols,Rows,Board,[],"")).

-spec matrix_to_board(list(), pos_integer(), map()) -> map().
matrix_to_board([],_,Board) -> Board;
matrix_to_board([H|T],Row,Board) ->
	matrix_to_board(T,Row+1,maps:merge(rows_to_cell(H,1,Row,Board),Board)).

-spec rows_to_cell(list(), pos_integer(), pos_integer(), map()) -> map().
rows_to_cell([],_,_,Board) -> Board;
rows_to_cell([H|T],Col,Row,Board)->
	Key = get_key(Col,Row),	
	rows_to_cell(T, Col+1,Row,maps:put(Key,H=:=$*, Board)).

-spec board_neighbours(pos_integer(), 
					   pos_integer(), 
					   pos_integer(), 
					   pos_integer(), 
					   map(),list(), string()) -> map().
board_neighbours(IndexX,IndexY,Cols,Rows,Board,Result,AccRow) when IndexX=:=Cols andalso IndexY=:=Rows -> 
		[AccRow++get_value(IndexX,IndexY,Cols,Rows,Board)|Result];
board_neighbours(IndexX,IndexY,Cols,Rows,Board,Result,AccRow) when IndexX > Cols ->
		board_neighbours(1,IndexY+1,Cols,Rows,Board,[AccRow|Result],"");
board_neighbours(IndexX,IndexY,Cols,Rows,Board,Result,AccRow) ->
	board_neighbours(IndexX+1,IndexY,Cols,Rows,Board,Result,AccRow++get_value(IndexX,IndexY,Cols,Rows,Board)).
	
-spec get_value(pos_integer(), pos_integer(), pos_integer(), pos_integer(), map()) -> string().
get_value(IndexX,IndexY, Cols, Rows,Board) ->
	Val = maps:get(get_key(IndexX,IndexY), Board),
	case Val of
		true ->"*";
		false ->  sum_neightbours([{IndexX-1,IndexY-1},{IndexX,IndexY-1},{IndexX+1,IndexY-1},
								  {IndexX-1,IndexY},{IndexX+1,IndexY}, 
								  {IndexX-1,IndexY+1},{IndexX,IndexY+1},{IndexX+1,IndexY+1}],
				  Board, Cols, Rows, 0)
	end.

-spec get_key(pos_integer(), pos_integer()) -> string().
get_key(Col,Row)-> io_lib:format("~b_~b", [Col,Row]).

-spec sum_neightbours(list(), map(), pos_integer(), pos_integer(),pos_integer()) -> string().
sum_neightbours([],_,_,_,0) -> " ";
sum_neightbours([],_,_,_,Acc) -> io_lib:format("~b", [Acc]);
sum_neightbours([{Col,Row}|T],Board, Cols, Rows, Acc) when Col > 0 andalso 
														   Row > 0 andalso
														   Col =< Cols andalso
														   Row =< Rows ->
	io:write({Col,Row}),
	Val = maps:get(get_key(Col,Row), Board),
	case Val of
		true -> sum_neightbours(T,Board, Cols, Rows,Acc+1);
		false -> sum_neightbours(T,Board, Cols, Rows,Acc)
	end;
sum_neightbours([_|T],Board, Cols, Rows, Acc) -> sum_neightbours(T,Board, Cols, Rows,Acc).

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?