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

## to Minesweeper in the Scala Track

Published at Aug 26 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 |
+-----+
``````

The Scala exercises assume an SBT project scheme. The exercise solution source should be placed within the exercise directory/src/main/scala. The exercise unit tests can be found within the exercise directory/src/test/scala.

To run the tests simply run the command `sbt test` in the exercise directory.

For more detailed info about the Scala track see the help page.

## Submitting Incomplete Solutions

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

### MinesweeperTest.scala

``````import org.scalatest.{Matchers, FunSuite}

/** @version 1.1.0 */
class MinesweeperTest extends FunSuite with Matchers {

test("no rows") {
Minesweeper.annotate(List()) should be(List())
}

test("no columns") {
pending
Minesweeper.annotate(List("")) should be(List(""))
}

test("no mines") {
pending
Minesweeper.annotate(List("   ",
"   ",
"   ")) should be(
List("   ",
"   ",
"   "))
}

test("minefield with only mines") {
pending
Minesweeper.annotate(List("***",
"***",
"***")) should be(
List("***",
"***",
"***"))
}

test("mine surrounded by spaces") {
pending
Minesweeper.annotate(List("   ",
" * ",
"   ")) should be(
List("111",
"1*1",
"111"))
}

test("space surrounded by mines") {
pending
Minesweeper.annotate(List("***",
"* *",
"***")) should be(
List("***",
"*8*",
"***"))
}

test("horizontal line") {
pending
Minesweeper.annotate(List(" * * ")) should be(List("1*2*1"))
}

test("horizontal line, mines at edges") {
pending
Minesweeper.annotate(List("*   *")) should be(List("*1 1*"))
}

test("vertical line") {
pending
Minesweeper.annotate(List(" ",
"*",
" ",
"*",
" ")) should be(
List("1",
"*",
"2",
"*",
"1"))
}

test("vertical line, mines at edges") {
pending
Minesweeper.annotate(List("*",
" ",
" ",
" ",
"*")) should be(
List("*",
"1",
" ",
"1",
"*"))
}

test("cross") {
pending
Minesweeper.annotate(List("  *  ",
"  *  ",
"*****",
"  *  ",
"  *  ")) should be(
List(" 2*2 ",
"25*52",
"*****",
"25*52",
" 2*2 "))
}

test("large minefield") {
pending
Minesweeper.annotate(List(" *  * ",
"  *   ",
"    * ",
"   * *",
" *  * ",
"      ")) should be(
List("1*22*1",
"12*322",
" 123*2",
"112*4*",
"1*22*2",
"111111"))
}
}``````
``````object Minesweeper {
def annotate(list: List[String]): List[String] = {
if (list.isEmpty) return list
val matrix = list.map(_.toList)
matrix.indices.map(i => matrix(i).indices.map(j => calcCells(i, j, matrix)).mkString).toList
}

private def calcCells(x: Int, y: Int, matrix: List[List[Char]]): String = {
if (matrix(x)(y) == '*') return "*"
val s = (
for {i <- x - 1 to x + 1
if i >= 0 && i < matrix.length
j <- y - 1 to y + 1
if j >= 0 && j < matrix.head.length
} yield if (matrix(i)(j) == '*') 1 else 0).sum
if (s == 0) " "
else s.toString
}
}``````