-
-
Notifications
You must be signed in to change notification settings - Fork 115
Add flower-field to replace minesweeper
#757
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| # Instructions | ||
|
|
||
| Your task is to add flower counts to empty squares in a completed Flower Field garden. | ||
| The garden itself is a rectangle board composed of squares that are either empty (`' '`) or a flower (`'*'`). | ||
|
|
||
| For each empty square, count the number of flowers adjacent to it (horizontally, vertically, diagonally). | ||
| If the empty square has no adjacent flowers, leave it empty. | ||
| Otherwise replace it with the count of adjacent flowers. | ||
|
|
||
| For example, you may receive a 5 x 4 board like this (empty spaces are represented here with the '·' character for display on screen): | ||
|
|
||
| ```text | ||
| ·*·*· | ||
| ··*·· | ||
| ··*·· | ||
| ····· | ||
| ``` | ||
|
|
||
| Which your code should transform into this: | ||
|
|
||
| ```text | ||
| 1*3*1 | ||
| 13*31 | ||
| ·2*2· | ||
| ·111· | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| # Introduction | ||
|
|
||
| [Flower Field][history] is a compassionate reimagining of the popular game Minesweeper. | ||
| 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. | ||
| "Flower Field" shipped in regional versions of Microsoft Windows in Italy, Germany, South Korea, Japan and Taiwan. | ||
|
|
||
| [history]: https://web.archive.org/web/20020409051321fw_/http://rcm.usr.dsi.unimi.it/rcmweb/fnm/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| { | ||
| "authors": [ | ||
| "jiegillet" | ||
| ], | ||
| "contributors": [ | ||
| "BNAndras" | ||
| ], | ||
| "files": { | ||
| "solution": [ | ||
| "src/FlowerField.elm" | ||
| ], | ||
| "test": [ | ||
| "tests/Tests.elm" | ||
| ], | ||
| "example": [ | ||
| ".meta/src/FlowerField.example.elm" | ||
| ] | ||
| }, | ||
| "blurb": "Mark all the flowers in a garden." | ||
| } | ||
95 changes: 95 additions & 0 deletions
95
exercises/practice/flower-field/.meta/src/FlowerField.example.elm
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| module FlowerField exposing (annotate) | ||
|
|
||
| import Dict exposing (Dict) | ||
|
|
||
|
|
||
| type Tile | ||
| = Empty | ||
| | Flower | ||
| | Annotated Int | ||
|
|
||
|
|
||
| annotate : String -> String | ||
| annotate garden = | ||
| let | ||
| positionMap = | ||
| makePositionMap garden | ||
| in | ||
| positionMap | ||
| |> Dict.map (annotateTile positionMap) | ||
| |> mergeRows | ||
| |> Dict.values | ||
| |> List.map (List.map printTile >> String.concat) | ||
| |> String.join "\n" | ||
|
|
||
|
|
||
| makePositionMap : String -> Dict ( Int, Int ) Tile | ||
| makePositionMap input = | ||
| input | ||
| |> String.lines | ||
| |> List.indexedMap | ||
| (\row line -> | ||
| line | ||
| |> String.toList | ||
| |> List.indexedMap | ||
| (\column char -> | ||
| case char of | ||
| '*' -> | ||
| ( ( row, column ), Flower ) | ||
|
|
||
| _ -> | ||
| ( ( row, column ), Empty ) | ||
| ) | ||
| ) | ||
| |> List.concat | ||
| |> Dict.fromList | ||
|
|
||
|
|
||
| annotateTile : Dict ( Int, Int ) Tile -> ( Int, Int ) -> Tile -> Tile | ||
| annotateTile garden ( row, col ) tile = | ||
| case tile of | ||
| Empty -> | ||
| [ ( row + 1, col + 1 ) | ||
| , ( row + 1, col ) | ||
| , ( row + 1, col - 1 ) | ||
| , ( row, col + 1 ) | ||
| , ( row, col - 1 ) | ||
| , ( row - 1, col + 1 ) | ||
| , ( row - 1, col ) | ||
| , ( row - 1, col - 1 ) | ||
| ] | ||
| |> List.filter (\p -> Dict.get p garden == Just Flower) | ||
| |> List.length | ||
| |> Annotated | ||
|
|
||
| other -> | ||
| other | ||
|
|
||
|
|
||
| mergeRows : Dict ( Int, Int ) Tile -> Dict Int (List Tile) | ||
| mergeRows = | ||
| Dict.foldl | ||
| -- we rely on Dict.foldl traversing the keys in order | ||
| (\( row, _ ) tile rows -> | ||
| Dict.update row | ||
| (\line -> Just (tile :: Maybe.withDefault [] line)) | ||
| rows | ||
| ) | ||
| Dict.empty | ||
| >> Dict.map (always List.reverse) | ||
|
|
||
|
|
||
| printTile : Tile -> String | ||
| printTile tile = | ||
| case tile of | ||
| Flower -> | ||
| "*" | ||
|
|
||
| Empty -> | ||
| "." | ||
|
|
||
| Annotated 0 -> | ||
| "." | ||
|
|
||
| Annotated count -> | ||
| String.fromInt count |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| # This is an auto-generated file. | ||
| # | ||
| # Regenerating this file via `configlet sync` will: | ||
| # - Recreate every `description` key/value pair | ||
| # - Recreate every `reimplements` key/value pair, where they exist in problem-specifications | ||
| # - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion) | ||
| # - Preserve any other key/value pair | ||
| # | ||
| # As user-added comments (using the # character) will be removed when this file | ||
| # is regenerated, comments can be added via a `comment` key. | ||
|
|
||
| [237ff487-467a-47e1-9b01-8a891844f86c] | ||
| description = "no rows" | ||
|
|
||
| [4b4134ec-e20f-439c-a295-664c38950ba1] | ||
| description = "no columns" | ||
| include = false | ||
| comment = "empty board already covered by 237ff487-467a-47e1-9b01-8a891844f86c" | ||
|
|
||
| [d774d054-bbad-4867-88ae-069cbd1c4f92] | ||
| description = "no flowers" | ||
|
|
||
| [225176a0-725e-43cd-aa13-9dced501f16e] | ||
| description = "garden full of flowers" | ||
|
|
||
| [3f345495-f1a5-4132-8411-74bd7ca08c49] | ||
| description = "flower surrounded by spaces" | ||
|
|
||
| [6cb04070-4199-4ef7-a6fa-92f68c660fca] | ||
| description = "space surrounded by flowers" | ||
|
|
||
| [272d2306-9f62-44fe-8ab5-6b0f43a26338] | ||
| description = "horizontal line" | ||
|
|
||
| [c6f0a4b2-58d0-4bf6-ad8d-ccf4144f1f8e] | ||
| description = "horizontal line, flowers at edges" | ||
|
|
||
| [a54e84b7-3b25-44a8-b8cf-1753c8bb4cf5] | ||
| description = "vertical line" | ||
|
|
||
| [b40f42f5-dec5-4abc-b167-3f08195189c1] | ||
| description = "vertical line, flowers at edges" | ||
|
|
||
| [58674965-7b42-4818-b930-0215062d543c] | ||
| description = "cross" | ||
|
|
||
| [dd9d4ca8-9e68-4f78-a677-a2a70fd7a7b8] | ||
| description = "large garden" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| { | ||
| "type": "application", | ||
| "source-directories": [ | ||
| "src" | ||
| ], | ||
| "elm-version": "0.19.1", | ||
| "dependencies": { | ||
| "direct": { | ||
| "elm/core": "1.0.5", | ||
| "elm/json": "1.1.3", | ||
| "elm/parser": "1.1.0", | ||
| "elm/random": "1.0.0", | ||
| "elm/regex": "1.0.0", | ||
| "elm/time": "1.0.0", | ||
| "elm/html": "1.0.0" | ||
| }, | ||
| "indirect": {} | ||
| }, | ||
| "test-dependencies": { | ||
| "direct": { | ||
| "elm-explorations/test": "2.1.0", | ||
| "rtfeldman/elm-iso8601-date-strings": "1.1.4" | ||
| }, | ||
| "indirect": { | ||
| "elm/bytes": "1.0.8", | ||
| "elm/virtual-dom": "1.0.3" | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| module FlowerField exposing (annotate) | ||
|
|
||
|
|
||
| annotate : String -> String | ||
| annotate garden = | ||
| Debug.todo "Please implement annotate" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| module Tests exposing (tests) | ||
|
|
||
| import Expect | ||
| import FlowerField | ||
| import Test exposing (Test, describe, skip, test) | ||
|
|
||
|
|
||
| tests : Test | ||
| tests = | ||
| describe "FlowerField" | ||
| [ -- skip <| | ||
| test "no rows" <| | ||
| \() -> | ||
| FlowerField.annotate "" | ||
| |> Expect.equal "" | ||
| , skip <| | ||
| test "no flowers" <| | ||
| \() -> | ||
| FlowerField.annotate """... | ||
| ... | ||
| ...""" |> Expect.equal """... | ||
| ... | ||
| ...""" | ||
| , skip <| | ||
| test "garden full of flowers" <| | ||
| \() -> | ||
| FlowerField.annotate """*** | ||
| *** | ||
| ***""" |> Expect.equal """*** | ||
| *** | ||
| ***""" | ||
| , skip <| | ||
| test "flower surrounded by spaces" <| | ||
| \() -> | ||
| FlowerField.annotate """... | ||
| .*. | ||
| ...""" |> Expect.equal """111 | ||
| 1*1 | ||
| 111""" | ||
| , skip <| | ||
| test "space surrounded by flowers" <| | ||
| \() -> | ||
| FlowerField.annotate """*** | ||
| *.* | ||
| ***""" |> Expect.equal """*** | ||
| *8* | ||
| ***""" | ||
| , skip <| | ||
| test "horizontal line" <| | ||
| \() -> | ||
| FlowerField.annotate ".*.*." | ||
| |> Expect.equal "1*2*1" | ||
| , skip <| | ||
| test "horizontal line, flowers at edges" <| | ||
| \() -> | ||
| FlowerField.annotate "*...*" | ||
| |> Expect.equal "*1.1*" | ||
| , skip <| | ||
| test "vertical line" <| | ||
| \() -> | ||
| FlowerField.annotate """. | ||
| * | ||
| . | ||
| * | ||
| .""" |> Expect.equal """1 | ||
| * | ||
| 2 | ||
| * | ||
| 1""" | ||
| , skip <| | ||
| test "vertical line, flowers at edges" <| | ||
| \() -> | ||
| FlowerField.annotate """* | ||
| . | ||
| . | ||
| . | ||
| *""" |> Expect.equal """* | ||
| 1 | ||
| . | ||
| 1 | ||
| *""" | ||
| , skip <| | ||
| test "cross" <| | ||
| \() -> | ||
| FlowerField.annotate """..*.. | ||
| ..*.. | ||
| ***** | ||
| ..*.. | ||
| ..*..""" |> Expect.equal """.2*2. | ||
| 25*52 | ||
| ***** | ||
| 25*52 | ||
| .2*2.""" | ||
| , skip <| | ||
| test "large garden" <| | ||
| \() -> | ||
| FlowerField.annotate """.*..*. | ||
| ..*... | ||
| ....*. | ||
| ...*.* | ||
| .*..*. | ||
| ......""" |> Expect.equal """1*22*1 | ||
| 12*322 | ||
| .123*2 | ||
| 112*4* | ||
| 1*22*2 | ||
| 111111""" | ||
| ] |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍