Skip to content

Commit 8855117

Browse files
authored
Add flower-field (#1760)
* add `flower-field` * Deprecate `minesweeper`
1 parent 2eb533a commit 8855117

File tree

8 files changed

+283
-3
lines changed

8 files changed

+283
-3
lines changed

config.json

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -949,9 +949,9 @@
949949
"difficulty": 4
950950
},
951951
{
952-
"slug": "minesweeper",
953-
"name": "Minesweeper",
954-
"uuid": "9d6808fb-d367-4df9-a1f0-47ff83b75544",
952+
"slug": "flower-field",
953+
"name": "Flower Field",
954+
"uuid": "d55cb55a-9c0e-42b5-83fe-0bfec3d52f93",
955955
"practices": [
956956
"strings"
957957
],
@@ -963,6 +963,15 @@
963963
],
964964
"difficulty": 5
965965
},
966+
{
967+
"slug": "minesweeper",
968+
"name": "Minesweeper",
969+
"uuid": "9d6808fb-d367-4df9-a1f0-47ff83b75544",
970+
"practices": [],
971+
"prerequisites": [],
972+
"difficulty": 5,
973+
"status": "deprecated"
974+
},
966975
{
967976
"slug": "robot-simulator",
968977
"name": "Robot Simulator",
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: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"authors": [
3+
"fluxusfrequency"
4+
],
5+
"contributors": [
6+
"abeger",
7+
"BNAndras",
8+
"budmc29",
9+
"dkinzer",
10+
"hilary",
11+
"iHiD",
12+
"Insti",
13+
"kotp",
14+
"kytrinyx",
15+
"tryantwit"
16+
],
17+
"files": {
18+
"solution": [
19+
"flower_field.rb"
20+
],
21+
"test": [
22+
"flower_field_test.rb"
23+
],
24+
"example": [
25+
".meta/example.rb"
26+
]
27+
},
28+
"blurb": "Mark all the flowers in a garden."
29+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
class FlowerField
2+
def self.annotate(garden)
3+
new(garden).annotate
4+
end
5+
6+
attr_reader :garden
7+
def initialize(garden)
8+
@garden = garden
9+
end
10+
11+
def annotate
12+
(0..rows).map do |row|
13+
(0..columns).map do |column|
14+
notation_at(row, column)
15+
end.join("")
16+
end
17+
end
18+
19+
private
20+
21+
def notation_at(row, column)
22+
if flower_coordinates.include?([row, column])
23+
return "*"
24+
end
25+
flowers = surrounding_coordinates(row, column).count {|x, y|
26+
flower_coordinates.include?([x, y])
27+
}
28+
if flowers.zero?
29+
" "
30+
else
31+
flowers.to_s
32+
end
33+
end
34+
35+
def surrounding_coordinates(row, column)
36+
[
37+
[row-1, column-1],
38+
[row-1, column],
39+
[row-1, column+1],
40+
[row, column-1],
41+
[row, column+1],
42+
[row+1, column-1],
43+
[row+1, column],
44+
[row+1, column+1]
45+
].reject {|x, y| invalid_coordinate(x, y)}
46+
end
47+
48+
def invalid_coordinate(x, y)
49+
x < 0 || y < 0 || x > rows || y > columns
50+
end
51+
52+
def flower_coordinates
53+
return @flower_coordinates if @flower_coordinates
54+
55+
@flower_coordinates = []
56+
garden.each.with_index do |row, x|
57+
row.chars.each.with_index do |cell, y|
58+
if cell == "*"
59+
@flower_coordinates << [x, y]
60+
end
61+
end
62+
end
63+
@flower_coordinates
64+
end
65+
66+
def rows
67+
@rows ||= garden.size-1
68+
end
69+
70+
def columns
71+
@columns ||= garden.first.size-1
72+
end
73+
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: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class FlowerField
2+
# Implement this class.
3+
end
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
require 'minitest/autorun'
2+
require_relative 'flower_field'
3+
4+
class FlowerFieldTest < Minitest::Test
5+
def test_no_rows
6+
input = []
7+
expected = []
8+
assert_equal expected, FlowerField.annotate(input)
9+
end
10+
11+
def test_no_columns
12+
skip
13+
input = [""]
14+
expected = [""]
15+
assert_equal expected, FlowerField.annotate(input)
16+
end
17+
18+
def test_no_flowers
19+
skip
20+
input = [" ", " ", " "]
21+
expected = [" ", " ", " "]
22+
assert_equal expected, FlowerField.annotate(input)
23+
end
24+
25+
def test_garden_full_of_flowers
26+
skip
27+
input = ["***", "***", "***"]
28+
expected = ["***", "***", "***"]
29+
assert_equal expected, FlowerField.annotate(input)
30+
end
31+
32+
def test_flower_surrounded_by_spaces
33+
skip
34+
input = [" ", " * ", " "]
35+
expected = ["111", "1*1", "111"]
36+
assert_equal expected, FlowerField.annotate(input)
37+
end
38+
39+
def test_space_surrounded_by_flowers
40+
skip
41+
input = ["***", "* *", "***"]
42+
expected = ["***", "*8*", "***"]
43+
assert_equal expected, FlowerField.annotate(input)
44+
end
45+
46+
def test_horizontal_line
47+
skip
48+
input = [" * * "]
49+
expected = ["1*2*1"]
50+
assert_equal expected, FlowerField.annotate(input)
51+
end
52+
53+
def test_horizontal_line_flowers_at_edges
54+
skip
55+
input = ["* *"]
56+
expected = ["*1 1*"]
57+
assert_equal expected, FlowerField.annotate(input)
58+
end
59+
60+
def test_vertical_line
61+
skip
62+
input = [" ", "*", " ", "*", " "]
63+
expected = ["1", "*", "2", "*", "1"]
64+
assert_equal expected, FlowerField.annotate(input)
65+
end
66+
67+
def test_vertical_line_flowers_at_edges
68+
skip
69+
input = ["*", " ", " ", " ", "*"]
70+
expected = ["*", "1", " ", "1", "*"]
71+
assert_equal expected, FlowerField.annotate(input)
72+
end
73+
74+
def test_cross
75+
skip
76+
input = [" * ", " * ", "*****", " * ", " * "]
77+
expected = [" 2*2 ", "25*52", "*****", "25*52", " 2*2 "]
78+
assert_equal expected, FlowerField.annotate(input)
79+
end
80+
81+
def test_large_garden
82+
skip
83+
input = [" * * ", " * ", " * ", " * *", " * * ", " "]
84+
expected = ["1*22*1", "12*322", " 123*2", "112*4*", "1*22*2", "111111"]
85+
assert_equal expected, FlowerField.annotate(input)
86+
end
87+
end

0 commit comments

Comments
 (0)