Skip to content

minesweeper: add to track #174

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 21, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion config.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@
"beer-song",
"difference-of-squares",
"largest-series-product",
"queen-attack"
"queen-attack",
"minesweeper"
],
"exercises": [
{
Expand Down Expand Up @@ -257,6 +258,11 @@
"slug": "queen-attack",
"difficulty": 1,
"topics": []
},
{
"slug": "minesweeper",
"difficulty": 1,
"topics": []
}
],
"deprecated": [
Expand Down
17 changes: 17 additions & 0 deletions exercises/minesweeper/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
apply plugin: "java"
apply plugin: "eclipse"
apply plugin: "idea"

repositories {
mavenCentral()
}

dependencies {
testCompile "junit:junit:4.12"
}
test {
testLogging {
exceptionFormat = 'full'
events = ["passed", "failed", "skipped"]
}
}
111 changes: 111 additions & 0 deletions exercises/minesweeper/src/example/java/MinesweeperBoard.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

final class MinesweeperBoard {

private static final char MINE_CHAR = '*';

private static final char SPACE_CHAR = ' ';

private final List<String> rawRepresentation;

private final int numberOfRows;

private final int numberOfColumns;

MinesweeperBoard(final List<String> rawRepresentation) {
validateInputBoard(rawRepresentation);
this.rawRepresentation = rawRepresentation;
this.numberOfRows = rawRepresentation.size();
this.numberOfColumns = rawRepresentation.isEmpty() ? 0 : rawRepresentation.get(0).length();
}

List<String> getAnnotatedRepresentation() throws IllegalArgumentException {
final List<String> result = new ArrayList<>();

for (int rowNumber = 0; rowNumber < numberOfRows; rowNumber++) {
result.add(getAnnotatedRow(rowNumber));
}

return result;
}

private String getAnnotatedRow(final int rowNumber) {
String result = "";

for (int columnNumber = 0; columnNumber < numberOfColumns; columnNumber++) {
result += getCellAnnotation(rowNumber, columnNumber);
}

return result;
}

private char getCellAnnotation(final int rowNumber, final int columnNumber) {
// If (rowNumber, columnNumber) is a mine, we're done.
if (rawRepresentation.get(rowNumber).charAt(columnNumber) == MINE_CHAR) {
return MINE_CHAR;
}

final int mineCount = computeMineCountAround(rowNumber, columnNumber);

// If computed count is positive, add it to the annotated row. Otherwise, add a blank space.
return mineCount > 0 ? Character.forDigit(mineCount, 10) : SPACE_CHAR;
}

private int computeMineCountAround(final int rowNumber, final int columnNumber) {
int result = 0;

// Compute row and column ranges to inspect (respecting board edges).
final int minRowToInspect = Math.max(rowNumber - 1, 0);
final int maxRowToInspect = Math.min(rowNumber + 1, numberOfRows - 1);
final int minColToInspect = Math.max(columnNumber - 1, 0);
final int maxColToInspect = Math.min(columnNumber + 1, numberOfColumns - 1);

// Count mines in the cells surrounding (row, col).
for (int rowToInspect = minRowToInspect; rowToInspect <= maxRowToInspect; rowToInspect++) {
for (int colToInspect = minColToInspect; colToInspect <= maxColToInspect; colToInspect++) {
if (rawRepresentation.get(rowToInspect).charAt(colToInspect) == MINE_CHAR) {
result += 1;
}
}
}

return result;
}

private void validateInputBoard(final List<String> inputBoard) throws IllegalArgumentException {
validateInputBoardIsNotNull(inputBoard);

if (inputBoard.isEmpty()) {
return;
}

validateInputBoardCharacters(inputBoard);
validateInputBoardColumnCounts(inputBoard);
}

private void validateInputBoardIsNotNull(final List<String> inputBoard) throws IllegalArgumentException {
if (inputBoard == null) {
throw new IllegalArgumentException("Input board may not be null.");
}
}

private void validateInputBoardCharacters(final List<String> inputBoard) throws IllegalArgumentException {
final String allBoardCharacters = String.join("", inputBoard);

if (!allBoardCharacters.matches("^[ *]*$")) {
throw new IllegalArgumentException("Input board can only contain the characters ' ' and '*'.");
}
}

private void validateInputBoardColumnCounts(final List<String> inputBoard) throws IllegalArgumentException {
final Set<Integer> setOfColumnCounts = inputBoard.stream().map(String::length).collect(Collectors.toSet());

if (setOfColumnCounts.size() > 1) {
throw new IllegalArgumentException("Input board rows must all have the same number of columns.");
}
}

}
5 changes: 5 additions & 0 deletions exercises/minesweeper/src/main/java/MinesweeperBoard.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
public final class MinesweeperBoard {



}
Loading