From 7d211ad3b1e7df643b3142931d10c04e2a49f8e4 Mon Sep 17 00:00:00 2001 From: Glenn Jackman Date: Tue, 29 Aug 2023 08:41:13 -0400 Subject: [PATCH] Add Darts exercise (#77) --- config.json | 10 +++- .../practice/darts/.docs/instructions.md | 23 ++++++++ exercises/practice/darts/.meta/config.json | 18 +++++++ exercises/practice/darts/.meta/example.8th | 14 +++++ exercises/practice/darts/.meta/tests.toml | 49 +++++++++++++++++ exercises/practice/darts/darts-tests.8th | 15 ++++++ exercises/practice/darts/darts.8th | 3 ++ exercises/practice/darts/test-words.8th | 53 +++++++++++++++++++ exercises/practice/darts/test.8th | 3 ++ 9 files changed, 187 insertions(+), 1 deletion(-) create mode 100644 exercises/practice/darts/.docs/instructions.md create mode 100644 exercises/practice/darts/.meta/config.json create mode 100644 exercises/practice/darts/.meta/example.8th create mode 100644 exercises/practice/darts/.meta/tests.toml create mode 100644 exercises/practice/darts/darts-tests.8th create mode 100644 exercises/practice/darts/darts.8th create mode 100644 exercises/practice/darts/test-words.8th create mode 100644 exercises/practice/darts/test.8th diff --git a/config.json b/config.json index 8826094..ce0c58e 100644 --- a/config.json +++ b/config.json @@ -83,6 +83,14 @@ "difficulty": 2, "practices": [] }, + { + "slug": "darts", + "name": "Darts", + "uuid": "773c9910-ae75-4560-b110-e9d057438aa9", + "prerequisites": [], + "difficulty": 2, + "practices": [] + }, { "slug": "bob", "name": "Bob", @@ -252,4 +260,4 @@ "used_for/mobile", "used_for/web_development" ] -} \ No newline at end of file +} diff --git a/exercises/practice/darts/.docs/instructions.md b/exercises/practice/darts/.docs/instructions.md new file mode 100644 index 0000000..70f0e53 --- /dev/null +++ b/exercises/practice/darts/.docs/instructions.md @@ -0,0 +1,23 @@ +# Instructions + +Write a function that returns the earned points in a single toss of a Darts game. + +[Darts][darts] is a game where players throw darts at a [target][darts-target]. + +In our particular instance of the game, the target rewards 4 different amounts of points, depending on where the dart lands: + +- If the dart lands outside the target, player earns no points (0 points). +- If the dart lands in the outer circle of the target, player earns 1 point. +- If the dart lands in the middle circle of the target, player earns 5 points. +- If the dart lands in the inner circle of the target, player earns 10 points. + +The outer circle has a radius of 10 units (this is equivalent to the total radius for the entire target), the middle circle a radius of 5 units, and the inner circle a radius of 1. +Of course, they are all centered at the same point — that is, the circles are [concentric][] defined by the coordinates (0, 0). + +Write a function that given a point in the target (defined by its [Cartesian coordinates][cartesian-coordinates] `x` and `y`, where `x` and `y` are [real][real-numbers]), returns the correct amount earned by a dart landing at that point. + +[darts]: https://en.wikipedia.org/wiki/Darts +[darts-target]: https://en.wikipedia.org/wiki/Darts#/media/File:Darts_in_a_dartboard.jpg +[concentric]: https://mathworld.wolfram.com/ConcentricCircles.html +[cartesian-coordinates]: https://www.mathsisfun.com/data/cartesian-coordinates.html +[real-numbers]: https://www.mathsisfun.com/numbers/real-numbers.html diff --git a/exercises/practice/darts/.meta/config.json b/exercises/practice/darts/.meta/config.json new file mode 100644 index 0000000..7c4e4a6 --- /dev/null +++ b/exercises/practice/darts/.meta/config.json @@ -0,0 +1,18 @@ +{ + "authors": [ + "glennj" + ], + "files": { + "solution": [ + "darts.8th" + ], + "test": [ + "darts-tests.8th" + ], + "example": [ + ".meta/example.8th" + ] + }, + "blurb": "Write a function that returns the earned points in a single toss of a Darts game.", + "source": "Inspired by an exercise created by a professor Della Paolera in Argentina" +} diff --git a/exercises/practice/darts/.meta/example.8th b/exercises/practice/darts/.meta/example.8th new file mode 100644 index 0000000..406ceb4 --- /dev/null +++ b/exercises/practice/darts/.meta/example.8th @@ -0,0 +1,14 @@ +\ Courtesy @ron at https://8th-dev.com/forum/index.php/topic,2713.0.html + +with: n +: distance \ x y -- n + sqr swap sqr + sqrt ; + +: score \ n -- n + dup 10 > if drop 0 ;then + dup 5 > if drop 1 ;then + dup 1 > if drop 5 ;then + drop 10 ; + +: darts-score \ x y -- n + distance score ; diff --git a/exercises/practice/darts/.meta/tests.toml b/exercises/practice/darts/.meta/tests.toml new file mode 100644 index 0000000..fbe2976 --- /dev/null +++ b/exercises/practice/darts/.meta/tests.toml @@ -0,0 +1,49 @@ +# 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. + +[9033f731-0a3a-4d9c-b1c0-34a1c8362afb] +description = "Missed target" + +[4c9f6ff4-c489-45fd-be8a-1fcb08b4d0ba] +description = "On the outer circle" + +[14378687-ee58-4c9b-a323-b089d5274be8] +description = "On the middle circle" + +[849e2e63-85bd-4fed-bc3b-781ae962e2c9] +description = "On the inner circle" + +[1c5ffd9f-ea66-462f-9f06-a1303de5a226] +description = "Exactly on center" + +[b65abce3-a679-4550-8115-4b74bda06088] +description = "Near the center" + +[66c29c1d-44f5-40cf-9927-e09a1305b399] +description = "Just within the inner circle" + +[d1012f63-c97c-4394-b944-7beb3d0b141a] +description = "Just outside the inner circle" + +[ab2b5666-b0b4-49c3-9b27-205e790ed945] +description = "Just within the middle circle" + +[70f1424e-d690-4860-8caf-9740a52c0161] +description = "Just outside the middle circle" + +[a7dbf8db-419c-4712-8a7f-67602b69b293] +description = "Just within the outer circle" + +[e0f39315-9f9a-4546-96e4-a9475b885aa7] +description = "Just outside the outer circle" + +[045d7d18-d863-4229-818e-b50828c75d19] +description = "Asymmetric position between the inner and middle circles" diff --git a/exercises/practice/darts/darts-tests.8th b/exercises/practice/darts/darts-tests.8th new file mode 100644 index 0000000..f01d36f --- /dev/null +++ b/exercises/practice/darts/darts-tests.8th @@ -0,0 +1,15 @@ +"Missed target" 0 ( -9 9 darts-score ) test_eq +"On the outer circle" 1 ( 0 10 darts-score ) test_eq +"On the middle circle" 5 ( -5 0 darts-score ) test_eq +"On the inner circle" 10 ( 0 -1 darts-score ) test_eq +"Exactly on center" 10 ( 0 0 darts-score ) test_eq +"Near the center" 10 ( -0.1 -0.1 darts-score ) test_eq +"Just within the inner circle" 10 ( 0.7 0.7 darts-score ) test_eq +"Just outside the inner circle" 5 ( 0.8 -0.8 darts-score ) test_eq +"Just within the middle circle" 5 ( -3.5 3.5 darts-score ) test_eq +"Just outside the middle circle" 1 ( -3.6 -3.6 darts-score ) test_eq +"Just within the outer circle" 1 ( -7.0 7.0 darts-score ) test_eq +"Just outside the outer circle" 0 ( 7.1 -7.1 darts-score ) test_eq +"Asymmetric position between the inner and middle circles" 5 ( 0.5 -4 darts-score ) test_eq + +bye diff --git a/exercises/practice/darts/darts.8th b/exercises/practice/darts/darts.8th new file mode 100644 index 0000000..016e817 --- /dev/null +++ b/exercises/practice/darts/darts.8th @@ -0,0 +1,3 @@ +: darts-score \ n n -- n + +; diff --git a/exercises/practice/darts/test-words.8th b/exercises/practice/darts/test-words.8th new file mode 100644 index 0000000..0b5d43c --- /dev/null +++ b/exercises/practice/darts/test-words.8th @@ -0,0 +1,53 @@ +needs console/loaded + +: isword? \ x -- x f + dup >kind ns:w n:= +; + +: test_eq \ s x w -- | s w x -- + isword? !if swap then + a:new swap 0 swap a:! 0 a:@ nip w:exec + n:= if + con:green con:onBlack . space " ... OK" . con:white con:onBlack cr + else + con:red con:onBlack . space " ... FAIL" . con:white con:onBlack cr + then +; + +: test_eqs \ s x w -- | s w x -- + isword? !if swap then + a:new swap 0 swap a:! 0 a:@ nip w:exec + s:= if + con:green con:onBlack . space " ... OK" . con:white con:onBlack cr + else + con:red con:onBlack . space " ... FAIL" . con:white con:onBlack cr + then +; + +: test_true \ s w -- + a:new swap 0 swap a:! 0 a:@ nip w:exec + if + con:green con:onBlack . space " ... OK" . con:white con:onBlack cr + else + con:red con:onBlack . space " ... FAIL" . con:white con:onBlack cr + then +; + +: test_false \ s w -- + a:new swap 0 swap a:! 0 a:@ nip w:exec + !if + con:green con:onBlack . space " ... OK" . con:white con:onBlack cr + else + con:red con:onBlack . space " ... FAIL" . con:white con:onBlack cr + then +; + +: test_null \ s w -- + a:new swap 0 swap a:! 0 a:@ nip w:exec + null? if + drop con:green con:onBlack . space " ... OK" . con:white con:onBlack cr + else + drop con:red con:onBlack . space " ... FAIL" . con:white con:onBlack cr + then +; + diff --git a/exercises/practice/darts/test.8th b/exercises/practice/darts/test.8th new file mode 100644 index 0000000..827c0ee --- /dev/null +++ b/exercises/practice/darts/test.8th @@ -0,0 +1,3 @@ +"test-words.8th" dup . cr f:include +"darts.8th" dup . cr f:include +"darts-tests.8th" dup . cr f:include