Skip to content

Commit 784b989

Browse files
authored
Replace Minesweeper with Flower Field (#742)
1 parent 9611d28 commit 784b989

File tree

10 files changed

+1027
-1
lines changed

10 files changed

+1027
-1
lines changed

config.json

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -800,6 +800,14 @@
800800
"randomness"
801801
]
802802
},
803+
{
804+
"slug": "flower-field",
805+
"name": "Flower Field",
806+
"uuid": "0af8fefb-2d20-483d-9f4a-9a274ad46bb7",
807+
"practices": [],
808+
"prerequisites": [],
809+
"difficulty": 5
810+
},
803811
{
804812
"slug": "food-chain",
805813
"name": "Food Chain",
@@ -831,7 +839,8 @@
831839
"uuid": "eae564ba-b4d9-4463-ae3f-f7f05f4e52ea",
832840
"practices": [],
833841
"prerequisites": [],
834-
"difficulty": 5
842+
"difficulty": 5,
843+
"status": "deprecated"
835844
},
836845
{
837846
"slug": "rational-numbers",
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Hints for Bash Flower Field Exercise
2+
3+
This exercise seems to require a two-dimensional array.
4+
Bash only offers one-dimensional indexed or associative [arrays][array].
5+
6+
Multi-dimensional arrays can be simulated by using an associative arrays: encode the indices into a comma-separated string to use as the array key.
7+
8+
```bash
9+
# store this matrix:
10+
# a b
11+
# c d
12+
declare -A array2d
13+
array2d["0,0"]="a"
14+
array2d["0,1"]="b"
15+
array2d["1,0"]="c"
16+
array2d["1,1"]="d"
17+
```
18+
19+
[array]: https://www.gnu.org/software/bash/manual/bash.html#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: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"authors": [
3+
"IsaacG"
4+
],
5+
"files": {
6+
"solution": [
7+
"flower_field.sh"
8+
],
9+
"test": [
10+
"flower_field.bats"
11+
],
12+
"example": [
13+
".meta/example.sh"
14+
]
15+
},
16+
"blurb": "Mark all the flowers in a garden."
17+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#!/usr/bin/env bash
2+
3+
# some global vars
4+
declare -A board
5+
height=$#
6+
width=${#1}
7+
8+
parse_input() {
9+
local rownum=0 row col index char
10+
for row in "$@"; do
11+
for ((col = 0; col < "${#row}"; col++)); do
12+
index="${rownum},${col}"
13+
char=${row:col:1}
14+
board[$index]=$char
15+
done
16+
(( rownum++ ))
17+
done
18+
}
19+
20+
count() {
21+
local r=$1 c=$2
22+
local count=0
23+
for dr in -1 0 1; do
24+
(( r + dr < 0 || r + dr == height )) && continue
25+
for dc in -1 0 1; do
26+
(( c + dc < 0 || c + dc == width )) && continue
27+
(( dr == 0 && dc == 0 )) && continue
28+
index="$((r + dr)),$((c + dc))"
29+
[[ ${board[$index]} == "*" ]] && (( count++ ))
30+
done
31+
done
32+
(( count == 0 )) && echo " " || echo "$count"
33+
}
34+
35+
create_output() {
36+
local row output col
37+
for ((row = 0; row < height; row++)); do
38+
output=""
39+
for ((col = 0; col < width; col++)); do
40+
if [[ ${board[$row,$col]} == "*" ]]; then
41+
output+="*"
42+
else
43+
output+=$( count "$row" "$col" )
44+
fi
45+
done
46+
echo "$output"
47+
done
48+
}
49+
50+
parse_input "$@"
51+
create_output
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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+
include = false
15+
16+
[4b4134ec-e20f-439c-a295-664c38950ba1]
17+
description = "no columns"
18+
19+
[d774d054-bbad-4867-88ae-069cbd1c4f92]
20+
description = "no flowers"
21+
22+
[225176a0-725e-43cd-aa13-9dced501f16e]
23+
description = "garden full of flowers"
24+
25+
[3f345495-f1a5-4132-8411-74bd7ca08c49]
26+
description = "flower surrounded by spaces"
27+
28+
[6cb04070-4199-4ef7-a6fa-92f68c660fca]
29+
description = "space surrounded by flowers"
30+
31+
[272d2306-9f62-44fe-8ab5-6b0f43a26338]
32+
description = "horizontal line"
33+
34+
[c6f0a4b2-58d0-4bf6-ad8d-ccf4144f1f8e]
35+
description = "horizontal line, flowers at edges"
36+
37+
[a54e84b7-3b25-44a8-b8cf-1753c8bb4cf5]
38+
description = "vertical line"
39+
40+
[b40f42f5-dec5-4abc-b167-3f08195189c1]
41+
description = "vertical line, flowers at edges"
42+
43+
[58674965-7b42-4818-b930-0215062d543c]
44+
description = "cross"
45+
46+
[dd9d4ca8-9e68-4f78-a677-a2a70fd7a7b8]
47+
description = "large garden"

0 commit comments

Comments
 (0)