Published at Jan 27 2019
·
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 |
+-----+
```

To run the tests, run the command `dotnet test`

from within the exercise directory.

For more detailed information about the F# track, including how to get help if you're having trouble, please visit the exercism.io F# language page.

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

```
// This file was auto-generated based on version 1.1.0 of the canonical data.
module MinesweeperTest
open FsUnit.Xunit
open Xunit
open Minesweeper
[<Fact>]
let ``No rows`` () =
let minefield: string list = []
let expected: string list = []
annotate minefield |> should equal expected
[<Fact(Skip = "Remove to run test")>]
let ``No columns`` () =
let minefield = [""]
let expected = [""]
annotate minefield |> should equal expected
[<Fact(Skip = "Remove to run test")>]
let ``No mines`` () =
let minefield =
[ " ";
" ";
" " ]
let expected =
[ " ";
" ";
" " ]
annotate minefield |> should equal expected
[<Fact(Skip = "Remove to run test")>]
let ``Minefield with only mines`` () =
let minefield =
[ "***";
"***";
"***" ]
let expected =
[ "***";
"***";
"***" ]
annotate minefield |> should equal expected
[<Fact(Skip = "Remove to run test")>]
let ``Mine surrounded by spaces`` () =
let minefield =
[ " ";
" * ";
" " ]
let expected =
[ "111";
"1*1";
"111" ]
annotate minefield |> should equal expected
[<Fact(Skip = "Remove to run test")>]
let ``Space surrounded by mines`` () =
let minefield =
[ "***";
"* *";
"***" ]
let expected =
[ "***";
"*8*";
"***" ]
annotate minefield |> should equal expected
[<Fact(Skip = "Remove to run test")>]
let ``Horizontal line`` () =
let minefield = [" * * "]
let expected = ["1*2*1"]
annotate minefield |> should equal expected
[<Fact(Skip = "Remove to run test")>]
let ``Horizontal line, mines at edges`` () =
let minefield = ["* *"]
let expected = ["*1 1*"]
annotate minefield |> should equal expected
[<Fact(Skip = "Remove to run test")>]
let ``Vertical line`` () =
let minefield =
[ " ";
"*";
" ";
"*";
" " ]
let expected =
[ "1";
"*";
"2";
"*";
"1" ]
annotate minefield |> should equal expected
[<Fact(Skip = "Remove to run test")>]
let ``Vertical line, mines at edges`` () =
let minefield =
[ "*";
" ";
" ";
" ";
"*" ]
let expected =
[ "*";
"1";
" ";
"1";
"*" ]
annotate minefield |> should equal expected
[<Fact(Skip = "Remove to run test")>]
let ``Cross`` () =
let minefield =
[ " * ";
" * ";
"*****";
" * ";
" * " ]
let expected =
[ " 2*2 ";
"25*52";
"*****";
"25*52";
" 2*2 " ]
annotate minefield |> should equal expected
[<Fact(Skip = "Remove to run test")>]
let ``Large minefield`` () =
let minefield =
[ " * * ";
" * ";
" * ";
" * *";
" * * ";
" " ]
let expected =
[ "1*22*1";
"12*322";
" 123*2";
"112*4*";
"1*22*2";
"111111" ]
annotate minefield |> should equal expected
```

```
module Minesweeper
open System
type position =
{
x:int
y:int
}
let minesNumb x n =
x
|> Seq.mapi(fun i x -> if x = '*' then Some({x=n;y=i;})
else None)
|> Seq.choose id
let minesList x =
x
|> Seq.mapi(fun i x -> i |> minesNumb x)
|> Seq.concat
let sorroundP (p:position) =
[{x = p.x-1; y = p.y-1};
{x = p.x-1; y = p.y};
{x = p.x-1; y = p.y+1};
{x = p.x; y = p.y-1};
{x = p.x; y = p.y+1};
{x = p.x+1; y = p.y-1};
{x = p.x+1; y = p.y};
{x = p.x+1; y = p.y+1}] |> List.toSeq
let count m p =
let ps = p |> sorroundP |> Seq.filter (fun x -> m |> Seq.contains x) |> Seq.length
if ps = 0 then ' '
else System.Char.Parse(ps.ToString())
let tranform i m =
i
|> List.mapi(fun i g -> g
|> Seq.mapi(fun n x -> if x = ' ' then {x=i;y=n;}|> count m
else x)
|> String.Concat )
let annotate (input:string list) =
let mines = input |> minesList
if mines |> Seq.isEmpty then input
else mines |> tranform input
```

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?

## Community comments