# artemkorsakov's solution

## to Minesweeper in the Java Track

Published at Feb 13 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 |
+-----+
``````

# Running the tests

You can run all the tests for an exercise by entering

``````\$ gradle test
``````

## Submitting Incomplete Solutions

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

### MinesweeperBoardTest.java

``````import org.junit.Ignore;
import org.junit.Test;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import static org.junit.Assert.assertEquals;

public class MinesweeperBoardTest {

@Test
public void testInputBoardWithNoRowsAndNoColumns() {
List<String> inputBoard = Collections.emptyList();
List<String> expectedNumberedBoard = Collections.emptyList();
List<String> actualNumberedBoard = new MinesweeperBoard(inputBoard).withNumbers();

assertEquals(expectedNumberedBoard, actualNumberedBoard);
}

@Ignore("Remove to run test")
@Test
public void testInputBoardWithOneRowAndNoColumns() {
List<String> inputBoard = Collections.singletonList("");
List<String> expectedNumberedBoard = Collections.singletonList("");
List<String> actualNumberedBoard = new MinesweeperBoard(inputBoard).withNumbers();

assertEquals(expectedNumberedBoard, actualNumberedBoard);
}

@Ignore("Remove to run test")
@Test
public void testInputBoardWithNoMines() {
List<String> inputBoard = Arrays.asList(
"   ",
"   ",
"   "
);

List<String> expectedNumberedBoard = Arrays.asList(
"   ",
"   ",
"   "
);

List<String> actualNumberedBoard = new MinesweeperBoard(inputBoard).withNumbers();

assertEquals(expectedNumberedBoard, actualNumberedBoard);
}

@Ignore("Remove to run test")
@Test
public void testInputBoardWithOnlyMines() {
List<String> inputBoard = Arrays.asList(
"***",
"***",
"***"
);

List<String> expectedNumberedBoard = Arrays.asList(
"***",
"***",
"***"
);

List<String> actualNumberedBoard = new MinesweeperBoard(inputBoard).withNumbers();

assertEquals(expectedNumberedBoard, actualNumberedBoard);
}

@Ignore("Remove to run test")
@Test
public void testInputBoardWithSingleMineAtCenter() {
List<String> inputBoard = Arrays.asList(
"   ",
" * ",
"   "
);

List<String> expectedNumberedBoard = Arrays.asList(
"111",
"1*1",
"111"
);

List<String> actualNumberedBoard = new MinesweeperBoard(inputBoard).withNumbers();

assertEquals(expectedNumberedBoard, actualNumberedBoard);
}

@Ignore("Remove to run test")
@Test
public void testInputBoardWithMinesAroundPerimeter() {
List<String> inputBoard = Arrays.asList(
"***",
"* *",
"***"
);

List<String> expectedNumberedBoard = Arrays.asList(
"***",
"*8*",
"***"
);

List<String> actualNumberedBoard = new MinesweeperBoard(inputBoard).withNumbers();

assertEquals(expectedNumberedBoard, actualNumberedBoard);
}

@Ignore("Remove to run test")
@Test
public void testInputBoardWithSingleRowAndTwoMines() {
List<String> inputBoard = Collections.singletonList(
" * * "
);

List<String> expectedNumberedBoard = Collections.singletonList(
"1*2*1"
);

List<String> actualNumberedBoard = new MinesweeperBoard(inputBoard).withNumbers();

assertEquals(expectedNumberedBoard, actualNumberedBoard);
}

@Ignore("Remove to run test")
@Test
public void testInputBoardWithSingleRowAndTwoMinesAtEdges() {
List<String> inputBoard = Collections.singletonList(
"*   *"
);

List<String> expectedNumberedBoard = Collections.singletonList(
"*1 1*"
);

List<String> actualNumberedBoard = new MinesweeperBoard(inputBoard).withNumbers();

assertEquals(expectedNumberedBoard, actualNumberedBoard);
}

@Ignore("Remove to run test")
@Test
public void testInputBoardWithSingleColumnAndTwoMines() {
List<String> inputBoard = Arrays.asList(
" ",
"*",
" ",
"*",
" "
);

List<String> expectedNumberedBoard = Arrays.asList(
"1",
"*",
"2",
"*",
"1"
);

List<String> actualNumberedBoard = new MinesweeperBoard(inputBoard).withNumbers();

assertEquals(expectedNumberedBoard, actualNumberedBoard);
}

@Ignore("Remove to run test")
@Test
public void testInputBoardWithSingleColumnAndTwoMinesAtEdges() {
List<String> inputBoard = Arrays.asList(
"*",
" ",
" ",
" ",
"*"
);

List<String> expectedNumberedBoard = Arrays.asList(
"*",
"1",
" ",
"1",
"*"
);

List<String> actualNumberedBoard = new MinesweeperBoard(inputBoard).withNumbers();

assertEquals(expectedNumberedBoard, actualNumberedBoard);
}

@Ignore("Remove to run test")
@Test
public void testInputBoardWithMinesInCross() {
List<String> inputBoard = Arrays.asList(
"  *  ",
"  *  ",
"*****",
"  *  ",
"  *  "
);

List<String> expectedNumberedBoard = Arrays.asList(
" 2*2 ",
"25*52",
"*****",
"25*52",
" 2*2 "
);

List<String> actualNumberedBoard = new MinesweeperBoard(inputBoard).withNumbers();

assertEquals(expectedNumberedBoard, actualNumberedBoard);
}

@Ignore("Remove to run test")
@Test
public void testLargeInputBoard() {
List<String> inputBoard = Arrays.asList(
" *  * ",
"  *   ",
"    * ",
"   * *",
" *  * ",
"      "
);

List<String> expectedNumberedBoard = Arrays.asList(
"1*22*1",
"12*322",
" 123*2",
"112*4*",
"1*22*2",
"111111"
);

List<String> actualNumberedBoard = new MinesweeperBoard(inputBoard).withNumbers();

assertEquals(expectedNumberedBoard, actualNumberedBoard);
}

}``````
``````import java.util.List;
import java.util.stream.Collectors;

class MinesweeperBoard {
private List<char[]> boardWithNumbers;

MinesweeperBoard(List<String> inputBoard) {
boardWithNumbers = getBoardWithNumbers(inputBoard);
}

List<String> withNumbers() {
return boardWithNumbers.parallelStream().map(String::new).collect(Collectors.toList());
}

private List<char[]> getBoardWithNumbers(List<String> inputBoard) {
List<char[]> result = inputBoard.parallelStream().map(String::toCharArray).collect(Collectors.toList());
for (int i = 0; i < result.size(); i++) {
for (int j = 0; j < result.get(i).length; j++) {
if (result.get(i)[j] == '*') {
result = increaseSquare(result, i, j);
}
}
}
return result;
}

private List<char[]> increaseSquare(List<char[]> square, int row, int col) {
if (row > 0) {
char[] prevRow = increaseRow(square.get(row - 1), col);
square.set(row - 1, prevRow);
}
char[] curRow = increaseRow(square.get(row), col);
square.set(row, curRow);
if (row < square.size() - 1) {
char[] nextRow = increaseRow(square.get(row + 1), col);
square.set(row + 1, nextRow);
}
return square;
}

private char[] increaseRow(char[] row, int col) {
if (col > 0) {
row[col - 1] = increaseItem(row[col - 1]);
}
row[col] = increaseItem(row[col]);
if (col < row.length - 1) {
row[col + 1] = increaseItem(row[col + 1]);
}
return row;
}

private char increaseItem(char c) {
if (c == '*') {
return c;
}
if (c == ' ') {
return '1';
}
int num = Character.getNumericValue(c) + 1;
return (char) (num + '0');
}

}``````