Skip to content

Commit 306f296

Browse files
flower-field replaces minesweeper (#898)
1 parent 3dfc557 commit 306f296

File tree

10 files changed

+524
-0
lines changed

10 files changed

+524
-0
lines changed

bin/auto-sync.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ difference-of-squares
2424
dnd-character
2525
eliuds-eggs
2626
etl
27+
flower-field
2728
food-chain
2829
grade-school
2930
hamming

config.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -593,13 +593,22 @@
593593
"prerequisites": [],
594594
"difficulty": 1
595595
},
596+
{
597+
"slug": "flower-field",
598+
"name": "Flower Field",
599+
"uuid": "3443aa60-53d4-4483-ad96-07eba9a23b9e",
600+
"practices": [],
601+
"prerequisites": [],
602+
"difficulty": 6
603+
},
596604
{
597605
"slug": "minesweeper",
598606
"name": "Minesweeper",
599607
"uuid": "96e47e5b-ab4f-472d-b56f-9ddcb3994320",
600608
"practices": [],
601609
"prerequisites": [],
602610
"difficulty": 1,
611+
"status": "deprecated",
603612
"topics": [
604613
"parsing",
605614
"transforming"

contribution/checkDeprecatedExercises.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ function getExercises(): array
5757
"eliuds-eggs",
5858
"etl",
5959
"flatten-array",
60+
"flower-field",
6061
"food-chain",
6162
"gigasecond",
6263
"grade-school",
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: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"authors": [
3+
"petemcfarlane"
4+
],
5+
"contributors": [
6+
"arueckauer",
7+
"keiravillekode",
8+
"kunicmarko20",
9+
"kytrinyx",
10+
"neenjaw",
11+
"mk-mxp"
12+
],
13+
"files": {
14+
"solution": [
15+
"FlowerField.php"
16+
],
17+
"test": [
18+
"FlowerFieldTest.php"
19+
],
20+
"example": [
21+
".meta/example.php"
22+
]
23+
},
24+
"blurb": "Mark all the flowers in a garden."
25+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
class FlowerField
6+
{
7+
// In PHP < 8.1 `readonly` is unknown
8+
private array $garden;
9+
private array $annotatedFlowerfield = [];
10+
11+
public function __construct(array $input)
12+
{
13+
$this->gardenFrom($input);
14+
}
15+
16+
public function annotate(): array
17+
{
18+
if (empty($this->annotatedFlowerfield)) {
19+
$this->annotateFlowerfield();
20+
}
21+
22+
return $this->asAnnotatedFlowerfield();
23+
}
24+
25+
private function gardenFrom(array $input): void
26+
{
27+
$this->garden = \array_map(
28+
// In PHP < 8.2, str_split returns [''] for empty strings.
29+
// In PHP >= 8.2 it returns the required [].
30+
fn ($row) => empty($row) ? [] : \str_split($row),
31+
$input,
32+
);
33+
}
34+
35+
private function asAnnotatedFlowerfield(): array
36+
{
37+
return \array_map(
38+
fn ($row) => \implode('', $row),
39+
$this->annotatedFlowerfield,
40+
);
41+
}
42+
43+
private function annotateFlowerfield(): void
44+
{
45+
$this->annotatedFlowerfield = $this->garden;
46+
47+
foreach (\array_keys($this->garden) as $row) {
48+
foreach (\array_keys($this->garden[$row]) as $col) {
49+
if (!$this->isFlower($row, $col)) {
50+
$flowerCount = $this->countFlowersAround($row, $col);
51+
$this->annotatedFlowerfield[$row][$col] =
52+
$flowerCount > 0 ? $flowerCount : ' ';
53+
}
54+
}
55+
}
56+
}
57+
58+
private function countFlowersAround(int $row, int $col): int
59+
{
60+
$flowerCount = 0;
61+
if ($row > 0) {
62+
$flowerCount += $this->countFlowersOfRowAroundCol($row - 1, $col);
63+
}
64+
65+
$flowerCount += $this->countFlowersOfRowAroundCol($row, $col);
66+
67+
if ($row < \count($this->garden) - 1) {
68+
$flowerCount += $this->countFlowersOfRowAroundCol($row + 1, $col);
69+
}
70+
71+
return $flowerCount;
72+
}
73+
74+
private function countFlowersOfRowAroundCol(int $row, int $col): int
75+
{
76+
$flowerCount = 0;
77+
if ($col > 0) {
78+
$flowerCount += $this->flowerScore($row, $col - 1);
79+
}
80+
81+
$flowerCount += $this->flowerScore($row, $col);
82+
83+
if ($col < \count($this->garden[$row]) - 1) {
84+
$flowerCount += $this->flowerScore($row, $col + 1);
85+
}
86+
87+
return $flowerCount;
88+
}
89+
90+
private function flowerScore(int $row, int $col): int
91+
{
92+
return $this->isFlower($row, $col) ? 1 : 0;
93+
}
94+
95+
private function isFlower(int $row, int $col): bool
96+
{
97+
return $this->garden[$row][$col] === '*';
98+
}
99+
}
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: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
/*
4+
* By adding type hints and enabling strict type checking, code can become
5+
* easier to read, self-documenting and reduce the number of potential bugs.
6+
* By default, type declarations are non-strict, which means they will attempt
7+
* to change the original type to match the type specified by the
8+
* type-declaration.
9+
*
10+
* In other words, if you pass a string to a function requiring a float,
11+
* it will attempt to convert the string value to a float.
12+
*
13+
* To enable strict mode, a single declare directive must be placed at the top
14+
* of the file.
15+
* This means that the strictness of typing is configured on a per-file basis.
16+
* This directive not only affects the type declarations of parameters, but also
17+
* a function's return type.
18+
*
19+
* For more info review the Concept on strict type checking in the PHP track
20+
* <link>.
21+
*
22+
* To disable strict typing, comment out the directive below.
23+
*/
24+
25+
declare(strict_types=1);
26+
27+
class FlowerField
28+
{
29+
public function __construct(array $garden)
30+
{
31+
}
32+
33+
public function annotate(): array
34+
{
35+
throw new \BadFunctionCallException(sprintf('Implement the %s method', __FUNCTION__));
36+
}
37+
}

0 commit comments

Comments
 (0)