Skip to content

Commit 0ce8c7b

Browse files
committed
Add flower-field, deprecating minesweeper
1 parent d460a46 commit 0ce8c7b

File tree

14 files changed

+837
-7
lines changed

14 files changed

+837
-7
lines changed

config.json

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1239,6 +1239,17 @@
12391239
],
12401240
"difficulty": 6
12411241
},
1242+
{
1243+
"slug": "flower-field",
1244+
"name": "Flower Field",
1245+
"uuid": "bddd180a-d634-454a-af03-4d625f77e1e2",
1246+
"practices": [],
1247+
"prerequisites": [
1248+
"maps",
1249+
"strings"
1250+
],
1251+
"difficulty": 6
1252+
},
12421253
{
12431254
"slug": "food-chain",
12441255
"name": "Food Chain",
@@ -1289,12 +1300,9 @@
12891300
"name": "Minesweeper",
12901301
"uuid": "416a1489-12af-4593-8540-0f55285c96b4",
12911302
"practices": [],
1292-
"prerequisites": [
1293-
"constructors",
1294-
"lists",
1295-
"strings"
1296-
],
1297-
"difficulty": 6
1303+
"prerequisites": [],
1304+
"difficulty": 6,
1305+
"status": "deprecated"
12981306
},
12991307
{
13001308
"slug": "parallel-letter-frequency",
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Instructions
2+
3+
Your task is to add flower counts to empty squares in a completed Flower Field garden.
4+
The garden itself is a rectangle board composed of squares that are either empty (`' '`) or a flower (`'*'`).
5+
6+
For each empty square, count the number of flowers adjacent to it (horizontally, vertically, diagonally).
7+
If the empty square has no adjacent flowers, leave it empty.
8+
Otherwise replace it with the count of adjacent flowers.
9+
10+
For example, you may receive a 5 x 4 board like this (empty spaces are represented here with the '·' character for display on screen):
11+
12+
```text
13+
·*·*·
14+
··*··
15+
··*··
16+
·····
17+
```
18+
19+
Which your code should transform into this:
20+
21+
```text
22+
1*3*1
23+
13*31
24+
·2*2·
25+
·111·
26+
```
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Introduction
2+
3+
[Flower Field][history] is a compassionate reimagining of the popular game Minesweeper.
4+
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.
5+
"Flower Field" shipped in regional versions of Microsoft Windows in Italy, Germany, South Korea, Japan and Taiwan.
6+
7+
[history]: https://web.archive.org/web/20020409051321fw_/http://rcm.usr.dsi.unimi.it/rcmweb/fnm/
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
{
2+
"authors": [
3+
"stkent"
4+
],
5+
"contributors": [
6+
"aadityakulkarni",
7+
"BNAndras",
8+
"FridaTveit",
9+
"hgvanpariya",
10+
"jmrunkle",
11+
"jsertel",
12+
"kytrinyx",
13+
"lemoncurry",
14+
"matthewmorgan",
15+
"matthewstyler",
16+
"morrme",
17+
"msomji",
18+
"muzimuzhi",
19+
"redshirt4",
20+
"SleeplessByte",
21+
"Smarticles101",
22+
"sshine",
23+
"vivshaw",
24+
"Zaldrick"
25+
],
26+
"files": {
27+
"solution": [
28+
"src/main/java/FlowerFieldBoard.java"
29+
],
30+
"test": [
31+
"src/test/java/FlowerFieldBoardTest.java"
32+
],
33+
"example": [
34+
".meta/src/reference/java/FlowerFieldBoard.java"
35+
],
36+
"invalidator": [
37+
"build.gradle"
38+
]
39+
},
40+
"blurb": "Mark all the flowers in a garden."
41+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import java.util.ArrayList;
2+
import java.util.List;
3+
4+
final class FlowerFieldBoard {
5+
6+
private static final char FLOWER_CHAR = '*';
7+
8+
private static final char SPACE_CHAR = ' ';
9+
10+
private final List<String> rawRepresentation;
11+
12+
private final int numberOfRows;
13+
14+
private final int numberOfColumns;
15+
16+
FlowerFieldBoard(final List<String> rawRepresentation) {
17+
this.rawRepresentation = rawRepresentation;
18+
this.numberOfRows = rawRepresentation.size();
19+
this.numberOfColumns = rawRepresentation.isEmpty() ? 0 : rawRepresentation.get(0).length();
20+
}
21+
22+
List<String> withNumbers() {
23+
final List<String> result = new ArrayList<>();
24+
25+
for (int rowNumber = 0; rowNumber < numberOfRows; rowNumber++) {
26+
result.add(getRowWithNumbers(rowNumber));
27+
}
28+
29+
return result;
30+
}
31+
32+
private String getRowWithNumbers(final int rowNumber) {
33+
StringBuilder result = new StringBuilder(numberOfColumns);
34+
35+
for (int columnNumber = 0; columnNumber < numberOfColumns; columnNumber++) {
36+
result.append(getCellNumber(rowNumber, columnNumber));
37+
}
38+
39+
return result.toString();
40+
}
41+
42+
private char getCellNumber(final int rowNumber, final int columnNumber) {
43+
// If (rowNumber, columnNumber) is a flower, we're done.
44+
if (rawRepresentation.get(rowNumber).charAt(columnNumber) == FLOWER_CHAR) {
45+
return FLOWER_CHAR;
46+
}
47+
48+
final int flowerCount = computeFlowerCountAround(rowNumber, columnNumber);
49+
50+
// If computed count is positive, add it to the annotated row. Otherwise, add a blank space.
51+
return flowerCount > 0 ? Character.forDigit(flowerCount, 10) : SPACE_CHAR;
52+
}
53+
54+
private int computeFlowerCountAround(final int rowNumber, final int columnNumber) {
55+
int result = 0;
56+
57+
// Compute row and column ranges to inspect (respecting board edges).
58+
final int minRowToInspect = Math.max(rowNumber - 1, 0);
59+
final int maxRowToInspect = Math.min(rowNumber + 1, numberOfRows - 1);
60+
final int minColToInspect = Math.max(columnNumber - 1, 0);
61+
final int maxColToInspect = Math.min(columnNumber + 1, numberOfColumns - 1);
62+
63+
// Count flowers in the cells surrounding (row, col).
64+
for (int rowToInspect = minRowToInspect; rowToInspect <= maxRowToInspect; rowToInspect++) {
65+
for (int colToInspect = minColToInspect; colToInspect <= maxColToInspect; colToInspect++) {
66+
if (rawRepresentation.get(rowToInspect).charAt(colToInspect) == FLOWER_CHAR) {
67+
result += 1;
68+
}
69+
}
70+
}
71+
72+
return result;
73+
}
74+
75+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# This is an auto-generated file.
2+
#
3+
# Regenerating this file via `configlet sync` will:
4+
# - Recreate every `description` key/value pair
5+
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
6+
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
7+
# - Preserve any other key/value pair
8+
#
9+
# As user-added comments (using the # character) will be removed when this file
10+
# is regenerated, comments can be added via a `comment` key.
11+
12+
[237ff487-467a-47e1-9b01-8a891844f86c]
13+
description = "no rows"
14+
15+
[4b4134ec-e20f-439c-a295-664c38950ba1]
16+
description = "no columns"
17+
18+
[d774d054-bbad-4867-88ae-069cbd1c4f92]
19+
description = "no flowers"
20+
21+
[225176a0-725e-43cd-aa13-9dced501f16e]
22+
description = "garden full of flowers"
23+
24+
[3f345495-f1a5-4132-8411-74bd7ca08c49]
25+
description = "flower surrounded by spaces"
26+
27+
[6cb04070-4199-4ef7-a6fa-92f68c660fca]
28+
description = "space surrounded by flowers"
29+
30+
[272d2306-9f62-44fe-8ab5-6b0f43a26338]
31+
description = "horizontal line"
32+
33+
[c6f0a4b2-58d0-4bf6-ad8d-ccf4144f1f8e]
34+
description = "horizontal line, flowers at edges"
35+
36+
[a54e84b7-3b25-44a8-b8cf-1753c8bb4cf5]
37+
description = "vertical line"
38+
39+
[b40f42f5-dec5-4abc-b167-3f08195189c1]
40+
description = "vertical line, flowers at edges"
41+
42+
[58674965-7b42-4818-b930-0215062d543c]
43+
description = "cross"
44+
45+
[dd9d4ca8-9e68-4f78-a677-a2a70fd7a7b8]
46+
description = "large garden"
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
plugins {
2+
id "java"
3+
}
4+
5+
repositories {
6+
mavenCentral()
7+
}
8+
9+
dependencies {
10+
testImplementation platform("org.junit:junit-bom:5.10.0")
11+
testImplementation "org.junit.jupiter:junit-jupiter"
12+
testImplementation "org.assertj:assertj-core:3.25.1"
13+
14+
testRuntimeOnly "org.junit.platform:junit-platform-launcher"
15+
}
16+
17+
test {
18+
useJUnitPlatform()
19+
20+
testLogging {
21+
exceptionFormat = "full"
22+
showStandardStreams = true
23+
events = ["passed", "failed", "skipped"]
24+
}
25+
}
Binary file not shown.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
distributionBase=GRADLE_USER_HOME
2+
distributionPath=wrapper/dists
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-8.7-bin.zip
4+
validateDistributionUrl=true
5+
zipStoreBase=GRADLE_USER_HOME
6+
zipStorePath=wrapper/dists

0 commit comments

Comments
 (0)