Skip to content

Commit

Permalink
Add darts exercise (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
BNAndras authored Dec 27, 2023
1 parent 4cb47dc commit 09e701e
Show file tree
Hide file tree
Showing 7 changed files with 206 additions and 0 deletions.
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,14 @@
"practices": [],
"prerequisites": [],
"difficulty": 2
},
{
"slug": "darts",
"name": "Darts",
"uuid": "0646b090-41a2-428d-b092-1ffbba71b316",
"practices": [],
"prerequisites": [],
"difficulty": 2
}
]
},
Expand Down
31 changes: 31 additions & 0 deletions exercises/practice/darts/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# 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:

![Our dart scoreboard with values from a complete miss to a bullseye](https://assets.exercism.org/images/exercises/darts/darts-scoreboard.svg)

- 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.

## Credit

The scoreboard image was created by [habere-et-dispertire][habere-et-dispertire] using [Inkscape][inkscape].

[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
[habere-et-dispertire]: https://exercism.org/profiles/habere-et-dispertire
[inkscape]: https://en.wikipedia.org/wiki/Inkscape
18 changes: 18 additions & 0 deletions exercises/practice/darts/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"authors": [
"BNAndras"
],
"files": {
"solution": [
"darts.gd"
],
"test": [
"darts_test.gd"
],
"example": [
".meta/example.gd"
]
},
"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"
}
9 changes: 9 additions & 0 deletions exercises/practice/darts/.meta/example.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
func score(x, y):
var distance = sqrt(pow(x, 2) + pow(y, 2))
if distance <= 1:
return 10
if distance <= 5:
return 5
if distance <= 10:
return 1
return 0
49 changes: 49 additions & 0 deletions exercises/practice/darts/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -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"
2 changes: 2 additions & 0 deletions exercises/practice/darts/darts.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
func score(x, y):
pass
89 changes: 89 additions & 0 deletions exercises/practice/darts/darts_test.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
func test_missed_target(solution_script):
var x = -9
var y = 9
var expected = 0
return [expected, solution_script.score(x, y)]


func test_on_the_outer_circle(solution_script):
var x = 0
var y = 10
var expected = 1
return [expected, solution_script.score(x, y)]


func test_on_the_middle_circle(solution_script):
var x = -5
var y = 0
var expected = 5
return [expected, solution_script.score(x, y)]


func test_on_the_inner_circle(solution_script):
var x = 0
var y = -1
var expected = 10
return [expected, solution_script.score(x, y)]


func test_exactly_on_center(solution_script):
var x = 0
var y = 0
var expected = 10
return [expected, solution_script.score(x, y)]


func test_near_the_center(solution_script):
var x = -0.1
var y = -0.1
var expected = 10
return [expected, solution_script.score(x, y)]


func test_just_within_the_inner_circle(solution_script):
var x = 0.7
var y = 0.7
var expected = 10
return [expected, solution_script.score(x, y)]


func test_just_outside_the_inner_circle(solution_script):
var x = 0.8
var y = -0.8
var expected = 5
return [expected, solution_script.score(x, y)]


func test_just_within_the_middle_circle(solution_script):
var x = -3.5
var y = 3.5
var expected = 5
return [expected, solution_script.score(x, y)]


func test_just_outside_the_middle_circle(solution_script):
var x = -3.6
var y = -3.6
var expected = 1
return [expected, solution_script.score(x, y)]


func test_just_within_the_outer_circle(solution_script):
var x = -7.0
var y = 7.0
var expected = 1
return [expected, solution_script.score(x, y)]


func test_just_outside_the_outer_circle(solution_script):
var x = 7.1
var y = -7.1
var expected = 0
return [expected, solution_script.score(x, y)]


func test_asymmetric_position(solution_script):
var x = 0.5
var y = -4
var expected = 5
return [expected, solution_script.score(x, y)]

0 comments on commit 09e701e

Please sign in to comment.