Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 9 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -593,13 +593,22 @@
"prerequisites": [],
"difficulty": 1
},
{
"slug": "flower-field",
"name": "Flower Field",
"uuid": "3443aa60-53d4-4483-ad96-07eba9a23b9e",
"practices": [],
"prerequisites": [],
"difficulty": 6
},
{
"slug": "minesweeper",
"name": "Minesweeper",
"uuid": "96e47e5b-ab4f-472d-b56f-9ddcb3994320",
"practices": [],
"prerequisites": [],
"difficulty": 1,
"status": "deprecated",
"topics": [
"parsing",
"transforming"
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/
25 changes: 25 additions & 0 deletions exercises/practice/flower-field/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"authors": [
"petemcfarlane"
],
"contributors": [
"arueckauer",
"keiravillekode",
"kunicmarko20",
"kytrinyx",
"neenjaw",
"mk-mxp"
],
"files": {
"solution": [
"FlowerField.php"
],
"test": [
"FlowerFieldTest.php"
],
"example": [
".meta/example.php"
]
},
"blurb": "Mark all the flowers in a garden."
}
99 changes: 99 additions & 0 deletions exercises/practice/flower-field/.meta/example.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php

declare(strict_types=1);

class FlowerField
{
// In PHP < 8.1 `readonly` is unknown
private array $garden;
private array $annotatedFlowerfield = [];

public function __construct(array $input)
{
$this->gardenFrom($input);
}

public function annotate(): array
{
if (empty($this->annotatedFlowerfield)) {
$this->annotateFlowerfield();
}

return $this->asAnnotatedFlowerfield();
}

private function gardenFrom(array $input): void
{
$this->garden = \array_map(
// In PHP < 8.2, str_split returns [''] for empty strings.
// In PHP >= 8.2 it returns the required [].
fn ($row) => empty($row) ? [] : \str_split($row),
$input,
);
}

private function asAnnotatedFlowerfield(): array
{
return \array_map(
fn ($row) => \implode('', $row),
$this->annotatedFlowerfield,
);
}

private function annotateFlowerfield(): void
{
$this->annotatedFlowerfield = $this->garden;

foreach (\array_keys($this->garden) as $row) {
foreach (\array_keys($this->garden[$row]) as $col) {
if (!$this->isFlower($row, $col)) {
$flowerCount = $this->countFlowersAround($row, $col);
$this->annotatedFlowerfield[$row][$col] =
$flowerCount > 0 ? $flowerCount : ' ';
}
}
}
}

private function countFlowersAround(int $row, int $col): int
{
$flowerCount = 0;
if ($row > 0) {
$flowerCount += $this->countFlowersOfRowAroundCol($row - 1, $col);
}

$flowerCount += $this->countFlowersOfRowAroundCol($row, $col);

if ($row < \count($this->garden) - 1) {
$flowerCount += $this->countFlowersOfRowAroundCol($row + 1, $col);
}

return $flowerCount;
}

private function countFlowersOfRowAroundCol(int $row, int $col): int
{
$flowerCount = 0;
if ($col > 0) {
$flowerCount += $this->flowerScore($row, $col - 1);
}

$flowerCount += $this->flowerScore($row, $col);

if ($col < \count($this->garden[$row]) - 1) {
$flowerCount += $this->flowerScore($row, $col + 1);
}

return $flowerCount;
}

private function flowerScore(int $row, int $col): int
{
return $this->isFlower($row, $col) ? 1 : 0;
}

private function isFlower(int $row, int $col): bool
{
return $this->garden[$row][$col] === '*';
}
}
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"
37 changes: 37 additions & 0 deletions exercises/practice/flower-field/FlowerField.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

/*
* By adding type hints and enabling strict type checking, code can become
* easier to read, self-documenting and reduce the number of potential bugs.
* By default, type declarations are non-strict, which means they will attempt
* to change the original type to match the type specified by the
* type-declaration.
*
* In other words, if you pass a string to a function requiring a float,
* it will attempt to convert the string value to a float.
*
* To enable strict mode, a single declare directive must be placed at the top
* of the file.
* This means that the strictness of typing is configured on a per-file basis.
* This directive not only affects the type declarations of parameters, but also
* a function's return type.
*
* For more info review the Concept on strict type checking in the PHP track
* <link>.
*
* To disable strict typing, comment out the directive below.
*/

declare(strict_types=1);

class FlowerField
{
public function __construct(array $garden)
{
}

public function annotate(): array
{
throw new \BadFunctionCallException(sprintf('Implement the %s method', __FUNCTION__));
}
}
Loading