Skip to content
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
21 changes: 15 additions & 6 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -1239,6 +1239,18 @@
],
"difficulty": 6
},
{
"slug": "flower-field",
"name": "Flower Field",
"uuid": "bddd180a-d634-454a-af03-4d625f77e1e2",
"practices": [],
"prerequisites": [
"constructors",
"lists",
"strings"
],
"difficulty": 6
},
{
"slug": "food-chain",
"name": "Food Chain",
Expand Down Expand Up @@ -1289,12 +1301,9 @@
"name": "Minesweeper",
"uuid": "416a1489-12af-4593-8540-0f55285c96b4",
"practices": [],
"prerequisites": [
"constructors",
"lists",
"strings"
],
"difficulty": 6
"prerequisites": [],
"difficulty": 6,
"status": "deprecated"
},
{
"slug": "parallel-letter-frequency",
Expand Down
26 changes: 26 additions & 0 deletions exercises/practice/flower-field/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Instructions

Your task is to add flower counts to empty squares in a completed Flower Field garden.
The garden itself is a rectangle board composed of squares that are either empty (`' '`) or a flower (`'*'`).

For each empty square, count the number of flowers adjacent to it (horizontally, vertically, diagonally).
If the empty square has no adjacent flowers, leave it empty.
Otherwise replace it with the count of adjacent flowers.

For example, you may receive a 5 x 4 board like this (empty spaces are represented here with the '·' character for display on screen):

```text
·*·*·
··*··
··*··
·····
```

Which your code should transform into this:

```text
1*3*1
13*31
·2*2·
·111·
```
7 changes: 7 additions & 0 deletions exercises/practice/flower-field/.docs/introduction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Introduction

[Flower Field][history] is a compassionate reimagining of the popular game Minesweeper.
The object of the game is to find all the flowers in the garden using numeric hints that indicate how many flowers are directly adjacent (horizontally, vertically, diagonally) to a square.
"Flower Field" shipped in regional versions of Microsoft Windows in Italy, Germany, South Korea, Japan and Taiwan.

[history]: https://web.archive.org/web/20020409051321fw_/http://rcm.usr.dsi.unimi.it/rcmweb/fnm/
41 changes: 41 additions & 0 deletions exercises/practice/flower-field/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"authors": [
"stkent"
],
"contributors": [
"aadityakulkarni",
"BNAndras",
"FridaTveit",
"hgvanpariya",
"jmrunkle",
"jsertel",
"kytrinyx",
"lemoncurry",
"matthewmorgan",
"matthewstyler",
"morrme",
"msomji",
"muzimuzhi",
"redshirt4",
"SleeplessByte",
"Smarticles101",
"sshine",
"vivshaw",
"Zaldrick"
],
"files": {
"solution": [
"src/main/java/FlowerFieldBoard.java"
],
"test": [
"src/test/java/FlowerFieldBoardTest.java"
],
"example": [
".meta/src/reference/java/FlowerFieldBoard.java"
],
"invalidator": [
"build.gradle"
]
},
"blurb": "Mark all the flowers in a garden."
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import java.util.ArrayList;
import java.util.List;

final class FlowerFieldBoard {

private static final char FLOWER_CHAR = '*';

private static final char SPACE_CHAR = ' ';

private final List<String> rawRepresentation;

private final int numberOfRows;

private final int numberOfColumns;

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

List<String> withNumbers() {
final List<String> result = new ArrayList<>();

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

return result;
}

private String getRowWithNumbers(final int rowNumber) {
StringBuilder result = new StringBuilder(numberOfColumns);

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

return result.toString();
}

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

final int flowerCount = computeFlowerCountAround(rowNumber, columnNumber);

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

private int computeFlowerCountAround(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 flowers 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) == FLOWER_CHAR) {
result += 1;
}
}
}

return result;
}

}
46 changes: 46 additions & 0 deletions exercises/practice/flower-field/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# This is an auto-generated file.
#
# Regenerating this file via `configlet sync` will:
# - Recreate every `description` key/value pair
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
# - Preserve any other key/value pair
#
# As user-added comments (using the # character) will be removed when this file
# is regenerated, comments can be added via a `comment` key.

[237ff487-467a-47e1-9b01-8a891844f86c]
description = "no rows"

[4b4134ec-e20f-439c-a295-664c38950ba1]
description = "no columns"

[d774d054-bbad-4867-88ae-069cbd1c4f92]
description = "no flowers"

[225176a0-725e-43cd-aa13-9dced501f16e]
description = "garden full of flowers"

[3f345495-f1a5-4132-8411-74bd7ca08c49]
description = "flower surrounded by spaces"

[6cb04070-4199-4ef7-a6fa-92f68c660fca]
description = "space surrounded by flowers"

[272d2306-9f62-44fe-8ab5-6b0f43a26338]
description = "horizontal line"

[c6f0a4b2-58d0-4bf6-ad8d-ccf4144f1f8e]
description = "horizontal line, flowers at edges"

[a54e84b7-3b25-44a8-b8cf-1753c8bb4cf5]
description = "vertical line"

[b40f42f5-dec5-4abc-b167-3f08195189c1]
description = "vertical line, flowers at edges"

[58674965-7b42-4818-b930-0215062d543c]
description = "cross"

[dd9d4ca8-9e68-4f78-a677-a2a70fd7a7b8]
description = "large garden"
25 changes: 25 additions & 0 deletions exercises/practice/flower-field/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
plugins {
id "java"
}

repositories {
mavenCentral()
}

dependencies {
testImplementation platform("org.junit:junit-bom:5.10.0")
testImplementation "org.junit.jupiter:junit-jupiter"
testImplementation "org.assertj:assertj-core:3.25.1"

testRuntimeOnly "org.junit.platform:junit-platform-launcher"
}

test {
useJUnitPlatform()

testLogging {
exceptionFormat = "full"
showStandardStreams = true
events = ["passed", "failed", "skipped"]
}
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.7-bin.zip
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Loading