Skip to content

Commit 0fb4498

Browse files
authored
Add flower-field, deprecating minesweeper (#1590)
1 parent 4026d9e commit 0fb4498

File tree

18 files changed

+524
-0
lines changed

18 files changed

+524
-0
lines changed

config.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1393,13 +1393,27 @@
13931393
],
13941394
"difficulty": 6
13951395
},
1396+
{
1397+
"slug": "flower-field",
1398+
"name": "Flower Field",
1399+
"uuid": "f1c96aaa-0944-4a11-83df-9f3a4f5417f0",
1400+
"practices": [],
1401+
"prerequisites": [],
1402+
"difficulty": 7,
1403+
"topics": [
1404+
"algorithms",
1405+
"arrays",
1406+
"games"
1407+
]
1408+
},
13961409
{
13971410
"slug": "minesweeper",
13981411
"name": "Minesweeper",
13991412
"uuid": "f5b00e76-3015-444d-8012-9862990db08c",
14001413
"practices": [],
14011414
"prerequisites": [],
14021415
"difficulty": 7,
1416+
"status": "deprecated",
14031417
"topics": [
14041418
"algorithms",
14051419
"arrays",
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: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"authors": [
3+
"CRivasGomez"
4+
],
5+
"contributors": [
6+
"BNAndras",
7+
"iignatov",
8+
"masters3d",
9+
"SleeplessByte"
10+
],
11+
"files": {
12+
"solution": [
13+
"flower-field.ts"
14+
],
15+
"test": [
16+
"flower-field.test.ts"
17+
],
18+
"example": [
19+
".meta/proof.ci.ts"
20+
]
21+
},
22+
"blurb": "Mark all the flowers in a garden."
23+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
const FLOWER = '*'
2+
3+
const DELTAS = [
4+
[-1, -1],
5+
[-1, 0],
6+
[-1, 1],
7+
[1, 1],
8+
[1, 0],
9+
[1, -1],
10+
[0, 1],
11+
[0, -1],
12+
]
13+
14+
export function annotate(rows: string[]): string[] {
15+
if (noDataPresent(rows)) {
16+
return rows
17+
}
18+
19+
const inputBoard = rows.map((row) => [...row])
20+
21+
const result = inputBoard.map((row, x) =>
22+
[...row].map((cell, y) => cellToFlowerOrCount(cell, inputBoard, x, y))
23+
)
24+
25+
return stringify(result as string[][])
26+
}
27+
28+
function cellToFlowerOrCount(
29+
cell: string,
30+
inputBoard: string[][],
31+
x: number,
32+
y: number
33+
): number | '*' | ' ' {
34+
if (cell === FLOWER) {
35+
return FLOWER
36+
}
37+
return countAdjacentFlowers(inputBoard, x, y) || ' '
38+
}
39+
40+
function countAdjacentFlowers(board: string[][], x: number, y: number): number {
41+
return DELTAS.filter((d) => adjacentSquareIsOnBoard(board, x, d)).filter(
42+
(d) => adjacentSquareHasFlower(board, x, y, d)
43+
).length
44+
}
45+
46+
function stringify(board: string[][]): string[] {
47+
return board.map((row) => row.join(''))
48+
}
49+
50+
function noDataPresent(rows: string[]): boolean {
51+
return rows.length === 0 || rows[0].length === 0
52+
}
53+
54+
function adjacentSquareIsOnBoard(
55+
board: string[][],
56+
x: number,
57+
d: number[]
58+
): string[] {
59+
return board[x + d[0]]
60+
}
61+
62+
function adjacentSquareHasFlower(
63+
board: string[][],
64+
x: number,
65+
y: number,
66+
d: number[]
67+
): boolean {
68+
return board[x + d[0]][y + d[1]] === FLOWER
69+
}
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: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"recommendations": [
3+
"arcanis.vscode-zipfs",
4+
"dbaeumer.vscode-eslint",
5+
"esbenp.prettier-vscode"
6+
]
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"cSpell.words": ["exercism"],
3+
"search.exclude": {
4+
"**/.yarn": true,
5+
"**/.pnp.*": true
6+
}
7+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
compressionLevel: mixed
2+
3+
enableGlobalCache: true
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = {
2+
// eslint-disable-next-line @typescript-eslint/no-require-imports
3+
presets: [[require('@exercism/babel-preset-typescript'), { corejs: '3.38' }]],
4+
plugins: [],
5+
}

0 commit comments

Comments
 (0)