Skip to content

Commit 8b94ce0

Browse files
flower-field replaces minesweeper (#541)
1 parent 6bc703b commit 8b94ce0

File tree

13 files changed

+366
-1
lines changed

13 files changed

+366
-1
lines changed

.github/Dockerfile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ RUN cd /home/opam/opam-repository \
1414
RUN opam update \
1515
&& opam install core \
1616
core_unix \
17-
merlin \
1817
yaml \
1918
ezjsonm \
2019
mustache \

config.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,13 +453,22 @@
453453
"search"
454454
]
455455
},
456+
{
457+
"slug": "flower-field",
458+
"name": "Flower Field",
459+
"uuid": "5e0945fe-1c46-4a2b-b66e-564330f3a92f",
460+
"practices": [],
461+
"prerequisites": [],
462+
"difficulty": 7
463+
},
456464
{
457465
"slug": "minesweeper",
458466
"name": "Minesweeper",
459467
"uuid": "a0468f9b-55d1-45dd-9a2f-476aaf4d6c2e",
460468
"practices": [],
461469
"prerequisites": [],
462470
"difficulty": 7,
471+
"status": "deprecated",
463472
"topics": [
464473
"arrays",
465474
"transforming"
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: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"authors": [],
3+
"contributors": [
4+
"daveyarwood",
5+
"dvberkel",
6+
"iHiD",
7+
"ismaelga",
8+
"keiravillekode",
9+
"kytrinyx",
10+
"marionebl",
11+
"Peaupote",
12+
"pminten",
13+
"sbl",
14+
"sshine",
15+
"stevejb71",
16+
"tmcgilchrist"
17+
],
18+
"files": {
19+
"solution": [
20+
"flower_field.ml"
21+
],
22+
"test": [
23+
"test.ml"
24+
],
25+
"example": [
26+
".meta/example.ml"
27+
]
28+
},
29+
"blurb": "Mark all the flowers in a garden."
30+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
open Base
2+
3+
type field =
4+
| Flower
5+
| Empty of int (* Empty spot, number is adjacent flowers *)
6+
7+
type board = { fields: field array array; width: int; height: int }
8+
9+
let board_of_strings strings =
10+
let to_field = function
11+
| '*' -> Flower
12+
| _ -> Empty 0 in
13+
let f s = String.to_list s |> List.map ~f:to_field |> Array.of_list in
14+
let fields = List.map strings ~f |> Array.of_list in
15+
let height = Array.length fields in
16+
let width = if height > 0 then Array.length fields.(0) else 0 in
17+
{ fields; width; height }
18+
19+
let strings_of_board board =
20+
let to_char = function
21+
| Flower -> '*'
22+
| Empty 0 -> ' '
23+
| Empty n -> Char.of_int_exn (Char.to_int '0' + n) in
24+
let f a = Array.to_list a |> List.map ~f:to_char |> String.of_char_list in
25+
Array.to_list board.fields |> List.map ~f
26+
27+
let adjacent_offsets =
28+
let open List in
29+
([-1; 0; 1] >>= fun x ->
30+
[-1; 0; 1] >>= fun y ->
31+
return (x, y))
32+
|> List.filter ~f:(fun (x, y) -> x <> 0 || y <> 0)
33+
34+
let mark_flower board c =
35+
let valid_coord (x, y) = x >= 0 && x < board.width && y >= 0 && y < board.height in
36+
let add_vec (ax, ay) (bx, by) = (ax + bx, ay + by) in
37+
let inc_field = function
38+
| Flower -> Flower
39+
| Empty n -> Empty (n + 1) in
40+
List.map adjacent_offsets ~f:(fun o -> add_vec c o)
41+
|> List.filter ~f:valid_coord
42+
|> List.iter ~f:(fun (x, y) -> board.fields.(y).(x) <- inc_field board.fields.(y).(x))
43+
44+
let annotate strings =
45+
let board = board_of_strings strings in
46+
for x = 0 to board.width - 1 do
47+
for y = 0 to board.height - 1 do
48+
match board.fields.(y).(x) with
49+
| Flower -> mark_flower board (x, y)
50+
| Empty _ -> ()
51+
done
52+
done;
53+
strings_of_board board
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+
default: clean test
2+
3+
test:
4+
dune runtest
5+
6+
clean:
7+
dune clean
8+
9+
.PHONY: clean

exercises/practice/flower-field/dune

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
(executable
2+
(name test)
3+
(libraries base ounit2))
4+
5+
(alias
6+
(name runtest)
7+
(deps (:x test.exe))
8+
(action (run %{x})))
9+
10+
(alias
11+
(name buildtest)
12+
(deps (:x test.exe)))
13+
14+
(env
15+
(dev
16+
(flags (:standard -warn-error -A))))
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
(lang dune 1.1)

0 commit comments

Comments
 (0)