Skip to content

Commit ade61ec

Browse files
authored
Add flower-field, deprecating minesweeper (#861)
1 parent b29e243 commit ade61ec

File tree

11 files changed

+307
-3
lines changed

11 files changed

+307
-3
lines changed

config.json

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1290,9 +1290,9 @@
12901290
]
12911291
},
12921292
{
1293-
"slug": "minesweeper",
1294-
"name": "Minesweeper",
1295-
"uuid": "ca6d49c7-e850-4e81-9540-582a1353f604",
1293+
"slug": "flower-field",
1294+
"name": "Flower Field",
1295+
"uuid": "4b9d68e5-2faa-44b2-b330-92be228da2d9",
12961296
"practices": [],
12971297
"prerequisites": [],
12981298
"difficulty": 7,
@@ -1404,6 +1404,19 @@
14041404
"looping",
14051405
"strings"
14061406
]
1407+
},
1408+
{
1409+
"slug": "minesweeper",
1410+
"name": "Minesweeper",
1411+
"uuid": "ca6d49c7-e850-4e81-9540-582a1353f604",
1412+
"practices": [],
1413+
"prerequisites": [],
1414+
"difficulty": 7,
1415+
"status": "deprecated",
1416+
"topics": [
1417+
"parsing",
1418+
"transforming"
1419+
]
14071420
}
14081421
]
14091422
},
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+
.DS_Store
2+
/.build
3+
/Packages
4+
/*.xcodeproj
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
class Board {
2+
var rows: [String]
3+
4+
init(_ rows: [String]) {
5+
self.rows = rows
6+
}
7+
8+
func transform() -> [String] {
9+
var result = [String]()
10+
for (rowIndex, row) in rows.enumerated() {
11+
var resultRow = ""
12+
for (columnIndex, column) in row.enumerated() {
13+
if column == "*" {
14+
resultRow += "*"
15+
} else {
16+
let count = countFlowers(rowIndex: rowIndex, columnIndex: columnIndex)
17+
resultRow += count == 0 ? " " : String(count)
18+
}
19+
}
20+
result.append(resultRow)
21+
}
22+
23+
return result
24+
}
25+
26+
private func countFlowers(rowIndex: Int, columnIndex: Int) -> Int {
27+
var count = 0
28+
for row in rowIndex - 1...rowIndex + 1 {
29+
for column in columnIndex - 1...columnIndex + 1 {
30+
if row >= 0 && row < rows.count && column >= 0 && column < rows[row].count {
31+
let currentRow = rows[row]
32+
let currentColumn = currentRow[currentRow.index(currentRow.startIndex, offsetBy: column)]
33+
if currentColumn == "*" {
34+
count += 1
35+
}
36+
}
37+
}
38+
}
39+
return count
40+
}
41+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"authors": [],
3+
"contributors": [
4+
"bhargavg",
5+
"BNAndras",
6+
"harquail",
7+
"lyuha",
8+
"masters3d",
9+
"robtimp",
10+
"ThomasHaz",
11+
"meatball133"
12+
],
13+
"files": {
14+
"solution": [
15+
"Sources/FlowerField/FlowerField.swift"
16+
],
17+
"test": [
18+
"Tests/FlowerFieldTests/FlowerFieldTests.swift"
19+
],
20+
"example": [
21+
".meta/Sources/FlowerField/FlowerFieldExample.swift"
22+
]
23+
},
24+
"blurb": "Mark all the flowers in a garden."
25+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import Testing
2+
import Foundation
3+
@testable import {{exercise|camelCase}}
4+
5+
let RUNALL = Bool(ProcessInfo.processInfo.environment["RUNALL", default: "false"]) ?? false
6+
7+
@Suite struct {{exercise|camelCase}}Tests {
8+
{% for case in cases %}
9+
{% if forloop.first -%}
10+
@Test("{{case.description}}")
11+
{% else -%}
12+
@Test("{{case.description}}", .enabled(if: RUNALL))
13+
{% endif -%}
14+
func test{{case.description |camelCase }}() {
15+
{% ifnot case.input.garden -%}
16+
let input = [String]()
17+
let output = [String]()
18+
{% else -%}
19+
let input = {{case.input.garden | toStringArray}}
20+
let output = {{case.expected | toStringArray}}
21+
{% endif -%}
22+
#expect(Board(input).transform() == output)
23+
}
24+
{% endfor -%}
25+
}
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: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// swift-tools-version:6.0
2+
3+
import PackageDescription
4+
5+
let package = Package(
6+
name: "FlowerField",
7+
products: [
8+
.library(
9+
name: "FlowerField",
10+
targets: ["FlowerField"])
11+
],
12+
dependencies: [],
13+
targets: [
14+
.target(
15+
name: "FlowerField",
16+
dependencies: []),
17+
.testTarget(
18+
name: "FlowerFieldTests",
19+
dependencies: ["FlowerField"]),
20+
]
21+
)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Board {
2+
// Write your code for the 'Flower Field' exercise in this file.
3+
}

0 commit comments

Comments
 (0)