Skip to content

Commit c43fb8c

Browse files
authored
Add flower-field, deprecate minesweeper (#1581)
* Add flower-field, deprecate minesweeper * Fix contributors, spelling and test
1 parent 64d675e commit c43fb8c

File tree

11 files changed

+425
-3
lines changed

11 files changed

+425
-3
lines changed

config.json

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2691,9 +2691,9 @@
26912691
"difficulty": 7
26922692
},
26932693
{
2694-
"slug": "minesweeper",
2695-
"name": "Minesweeper",
2696-
"uuid": "8d665135-31dc-490a-8f6d-51c6654099bd",
2694+
"slug": "flower-field",
2695+
"name": "Flower Field",
2696+
"uuid": "65b9ba19-0d82-4cde-b909-6a8440b24fd6",
26972697
"practices": [
26982698
"enum"
26992699
],
@@ -2713,6 +2713,15 @@
27132713
],
27142714
"difficulty": 7
27152715
},
2716+
{
2717+
"slug": "minesweeper",
2718+
"name": "Minesweeper",
2719+
"uuid": "8d665135-31dc-490a-8f6d-51c6654099bd",
2720+
"practices": [],
2721+
"prerequisites": [],
2722+
"difficulty": 7,
2723+
"status": "deprecated"
2724+
},
27162725
{
27172726
"slug": "piecing-it-together",
27182727
"name": "Piecing It Together",
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: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Used by "mix format"
2+
[
3+
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
4+
]
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"authors": [],
3+
"contributors": [
4+
"andrewsardone",
5+
"angelikatyborska",
6+
"Cohen-Carlisle",
7+
"dalexj",
8+
"devonestes",
9+
"jinyeow",
10+
"kahgoh",
11+
"kytrinyx",
12+
"lpil",
13+
"neenjaw",
14+
"parkerl",
15+
"petehuang",
16+
"pminten",
17+
"rubysolo",
18+
"sotojuan",
19+
"Teapane",
20+
"tjcelaya",
21+
"waiting-for-dev"
22+
],
23+
"files": {
24+
"solution": [
25+
"lib/flower_field.ex"
26+
],
27+
"test": [
28+
"test/flower_field_test.exs"
29+
],
30+
"example": [
31+
".meta/example.ex"
32+
]
33+
},
34+
"blurb": "Mark all the flowers in a garden."
35+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
defmodule FlowerField do
2+
@doc """
3+
Annotate empty spots next to flowers with the number of flowers next to them.
4+
"""
5+
@spec annotate([String.t()]) :: [String.t()]
6+
def annotate([]), do: []
7+
8+
def annotate(board) do
9+
h = length(board)
10+
# Only 7-bit ASCII in the board, so this is safe
11+
w = String.length(hd(board))
12+
13+
annotations =
14+
Enum.reduce(Stream.with_index(board), %{}, fn {line, y}, acc ->
15+
Enum.reduce(Stream.with_index(String.to_charlist(line)), acc, fn
16+
{?*, x}, acc -> add_adjacents(acc, {x, y}, {w, h})
17+
_, acc -> acc
18+
end)
19+
end)
20+
21+
Enum.map(Stream.with_index(board), fn {line, y} ->
22+
Enum.map(Stream.with_index(String.to_charlist(line)), fn
23+
# Don't replace flowers
24+
{?*, _} ->
25+
?*
26+
27+
{_, x} ->
28+
case annotations[{x, y}] do
29+
nil -> ?\s
30+
n -> ?0 + n
31+
end
32+
end)
33+
|> to_string
34+
end)
35+
end
36+
37+
@adjacent_vecs for x <- [-1, 0, 1],
38+
y <- [-1, 0, 1],
39+
x != 0 or y != 0,
40+
do: {x, y}
41+
42+
defp add_adjacents(d, c, bounds) do
43+
Enum.reduce(@adjacent_vecs, d, fn v, acc ->
44+
c1 = add_vec(c, v)
45+
46+
if valid?(c1, bounds) do
47+
Map.update(acc, c1, 1, &(&1 + 1))
48+
else
49+
acc
50+
end
51+
end)
52+
end
53+
54+
defp add_vec({cx, cy}, {vx, vy}), do: {cx + vx, cy + vy}
55+
56+
defp valid?({cx, cy}, {w, h}), do: cx >= 0 and cx < w and cy >= 0 and cy < h
57+
end
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: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
defmodule FlowerField do
2+
@doc """
3+
Annotate empty spots next to flowers with the number of flowers next to them.
4+
"""
5+
@spec annotate([String.t()]) :: [String.t()]
6+
7+
def annotate(board) do
8+
end
9+
end
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
defmodule FlowerField.MixProject do
2+
use Mix.Project
3+
4+
def project do
5+
[
6+
app: :flower_field,
7+
version: "0.1.0",
8+
# elixir: "~> 1.8",
9+
start_permanent: Mix.env() == :prod,
10+
deps: deps()
11+
]
12+
end
13+
14+
# Run "mix help compile.app" to learn about applications.
15+
def application do
16+
[
17+
extra_applications: [:logger]
18+
]
19+
end
20+
21+
# Run "mix help deps" to learn about dependencies.
22+
defp deps do
23+
[
24+
# {:dep_from_hexpm, "~> 0.3.0"},
25+
# {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"}
26+
]
27+
end
28+
end

0 commit comments

Comments
 (0)