Skip to content

Commit 09e701e

Browse files
authored
Add darts exercise (#30)
1 parent 4cb47dc commit 09e701e

File tree

7 files changed

+206
-0
lines changed

7 files changed

+206
-0
lines changed

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,14 @@
7474
"practices": [],
7575
"prerequisites": [],
7676
"difficulty": 2
77+
},
78+
{
79+
"slug": "darts",
80+
"name": "Darts",
81+
"uuid": "0646b090-41a2-428d-b092-1ffbba71b316",
82+
"practices": [],
83+
"prerequisites": [],
84+
"difficulty": 2
7785
}
7886
]
7987
},
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Instructions
2+
3+
Write a function that returns the earned points in a single toss of a Darts game.
4+
5+
[Darts][darts] is a game where players throw darts at a [target][darts-target].
6+
7+
In our particular instance of the game, the target rewards 4 different amounts of points, depending on where the dart lands:
8+
9+
![Our dart scoreboard with values from a complete miss to a bullseye](https://assets.exercism.org/images/exercises/darts/darts-scoreboard.svg)
10+
11+
- If the dart lands outside the target, player earns no points (0 points).
12+
- If the dart lands in the outer circle of the target, player earns 1 point.
13+
- If the dart lands in the middle circle of the target, player earns 5 points.
14+
- If the dart lands in the inner circle of the target, player earns 10 points.
15+
16+
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.
17+
Of course, they are all centered at the same point — that is, the circles are [concentric][] defined by the coordinates (0, 0).
18+
19+
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.
20+
21+
## Credit
22+
23+
The scoreboard image was created by [habere-et-dispertire][habere-et-dispertire] using [Inkscape][inkscape].
24+
25+
[darts]: https://en.wikipedia.org/wiki/Darts
26+
[darts-target]: https://en.wikipedia.org/wiki/Darts#/media/File:Darts_in_a_dartboard.jpg
27+
[concentric]: https://mathworld.wolfram.com/ConcentricCircles.html
28+
[cartesian-coordinates]: https://www.mathsisfun.com/data/cartesian-coordinates.html
29+
[real-numbers]: https://www.mathsisfun.com/numbers/real-numbers.html
30+
[habere-et-dispertire]: https://exercism.org/profiles/habere-et-dispertire
31+
[inkscape]: https://en.wikipedia.org/wiki/Inkscape
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"authors": [
3+
"BNAndras"
4+
],
5+
"files": {
6+
"solution": [
7+
"darts.gd"
8+
],
9+
"test": [
10+
"darts_test.gd"
11+
],
12+
"example": [
13+
".meta/example.gd"
14+
]
15+
},
16+
"blurb": "Write a function that returns the earned points in a single toss of a Darts game.",
17+
"source": "Inspired by an exercise created by a professor Della Paolera in Argentina"
18+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
func score(x, y):
2+
var distance = sqrt(pow(x, 2) + pow(y, 2))
3+
if distance <= 1:
4+
return 10
5+
if distance <= 5:
6+
return 5
7+
if distance <= 10:
8+
return 1
9+
return 0
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
[9033f731-0a3a-4d9c-b1c0-34a1c8362afb]
13+
description = "Missed target"
14+
15+
[4c9f6ff4-c489-45fd-be8a-1fcb08b4d0ba]
16+
description = "On the outer circle"
17+
18+
[14378687-ee58-4c9b-a323-b089d5274be8]
19+
description = "On the middle circle"
20+
21+
[849e2e63-85bd-4fed-bc3b-781ae962e2c9]
22+
description = "On the inner circle"
23+
24+
[1c5ffd9f-ea66-462f-9f06-a1303de5a226]
25+
description = "Exactly on center"
26+
27+
[b65abce3-a679-4550-8115-4b74bda06088]
28+
description = "Near the center"
29+
30+
[66c29c1d-44f5-40cf-9927-e09a1305b399]
31+
description = "Just within the inner circle"
32+
33+
[d1012f63-c97c-4394-b944-7beb3d0b141a]
34+
description = "Just outside the inner circle"
35+
36+
[ab2b5666-b0b4-49c3-9b27-205e790ed945]
37+
description = "Just within the middle circle"
38+
39+
[70f1424e-d690-4860-8caf-9740a52c0161]
40+
description = "Just outside the middle circle"
41+
42+
[a7dbf8db-419c-4712-8a7f-67602b69b293]
43+
description = "Just within the outer circle"
44+
45+
[e0f39315-9f9a-4546-96e4-a9475b885aa7]
46+
description = "Just outside the outer circle"
47+
48+
[045d7d18-d863-4229-818e-b50828c75d19]
49+
description = "Asymmetric position between the inner and middle circles"

exercises/practice/darts/darts.gd

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
func score(x, y):
2+
pass
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
func test_missed_target(solution_script):
2+
var x = -9
3+
var y = 9
4+
var expected = 0
5+
return [expected, solution_script.score(x, y)]
6+
7+
8+
func test_on_the_outer_circle(solution_script):
9+
var x = 0
10+
var y = 10
11+
var expected = 1
12+
return [expected, solution_script.score(x, y)]
13+
14+
15+
func test_on_the_middle_circle(solution_script):
16+
var x = -5
17+
var y = 0
18+
var expected = 5
19+
return [expected, solution_script.score(x, y)]
20+
21+
22+
func test_on_the_inner_circle(solution_script):
23+
var x = 0
24+
var y = -1
25+
var expected = 10
26+
return [expected, solution_script.score(x, y)]
27+
28+
29+
func test_exactly_on_center(solution_script):
30+
var x = 0
31+
var y = 0
32+
var expected = 10
33+
return [expected, solution_script.score(x, y)]
34+
35+
36+
func test_near_the_center(solution_script):
37+
var x = -0.1
38+
var y = -0.1
39+
var expected = 10
40+
return [expected, solution_script.score(x, y)]
41+
42+
43+
func test_just_within_the_inner_circle(solution_script):
44+
var x = 0.7
45+
var y = 0.7
46+
var expected = 10
47+
return [expected, solution_script.score(x, y)]
48+
49+
50+
func test_just_outside_the_inner_circle(solution_script):
51+
var x = 0.8
52+
var y = -0.8
53+
var expected = 5
54+
return [expected, solution_script.score(x, y)]
55+
56+
57+
func test_just_within_the_middle_circle(solution_script):
58+
var x = -3.5
59+
var y = 3.5
60+
var expected = 5
61+
return [expected, solution_script.score(x, y)]
62+
63+
64+
func test_just_outside_the_middle_circle(solution_script):
65+
var x = -3.6
66+
var y = -3.6
67+
var expected = 1
68+
return [expected, solution_script.score(x, y)]
69+
70+
71+
func test_just_within_the_outer_circle(solution_script):
72+
var x = -7.0
73+
var y = 7.0
74+
var expected = 1
75+
return [expected, solution_script.score(x, y)]
76+
77+
78+
func test_just_outside_the_outer_circle(solution_script):
79+
var x = 7.1
80+
var y = -7.1
81+
var expected = 0
82+
return [expected, solution_script.score(x, y)]
83+
84+
85+
func test_asymmetric_position(solution_script):
86+
var x = 0.5
87+
var y = -4
88+
var expected = 5
89+
return [expected, solution_script.score(x, y)]

0 commit comments

Comments
 (0)