Skip to content

Commit 527058c

Browse files
authored
Sync darts (#867)
1 parent ee39b8c commit 527058c

File tree

6 files changed

+153
-109
lines changed

6 files changed

+153
-109
lines changed
Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,31 @@
11
# Instructions
22

3-
Write a function that returns the earned points in a single toss of a Darts game.
3+
Calculate the points scored in a single toss of a Darts game.
44

5-
[Darts](https://en.wikipedia.org/wiki/Darts) is a game where players
6-
throw darts to a [target](https://en.wikipedia.org/wiki/Darts#/media/File:Darts_in_a_dartboard.jpg).
5+
[Darts][darts] is a game where players throw darts at a [target][darts-target].
76

8-
In our particular instance of the game, the target rewards with 4 different amounts of points, depending on where the dart lands:
7+
In our particular instance of the game, the target rewards 4 different amounts of points, depending on where the dart lands:
98

10-
* If the dart lands outside the target, player earns no points (0 points).
11-
* If the dart lands in the outer circle of the target, player earns 1 point.
12-
* If the dart lands in the middle circle of the target, player earns 5 points.
13-
* If the dart lands in the inner circle of the target, player earns 10 points.
9+
![Our dart scoreboard with values from a complete miss to a bullseye](https://assets.exercism.org/images/exercises/darts/darts-scoreboard.svg)
1410

15-
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 to the same point (That is, the circles are [concentric](https://mathworld.wolfram.com/ConcentricCircles.html)) defined by the coordinates (0, 0).
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.
1615

17-
Write a function that given a point in the target (defined by its `real` cartesian coordinates `x` and `y`), returns the correct amount earned by a dart landing in that point.
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+
Given a point in the target (defined by its [Cartesian coordinates][cartesian-coordinates] `x` and `y`, where `x` and `y` are [real][real-numbers]), calculate the correct score 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

exercises/practice/darts/.meta/config.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@
1313
".meta/example.php"
1414
]
1515
},
16-
"blurb": "Write a function that returns the earned points in a single toss of a Darts game"
16+
"blurb": "Calculate the points scored in a single toss of a Darts game.",
17+
"source": "Inspired by an exercise created by a professor Della Paolera in Argentina"
1718
}
Lines changed: 11 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,20 @@
11
<?php
22

3-
/*
4-
* By adding type hints and enabling strict type checking, code can become
5-
* easier to read, self-documenting and reduce the number of potential bugs.
6-
* By default, type declarations are non-strict, which means they will attempt
7-
* to change the original type to match the type specified by the
8-
* type-declaration.
9-
*
10-
* In other words, if you pass a string to a function requiring a float,
11-
* it will attempt to convert the string value to a float.
12-
*
13-
* To enable strict mode, a single declare directive must be placed at the top
14-
* of the file.
15-
* This means that the strictness of typing is configured on a per-file basis.
16-
* This directive not only affects the type declarations of parameters, but also
17-
* a function's return type.
18-
*
19-
* For more info review the Concept on strict type checking in the PHP track
20-
* <link>.
21-
*
22-
* To disable strict typing, comment out the directive below.
23-
*/
24-
253
declare(strict_types=1);
264

27-
class Darts
5+
function score(float $xAxis, float $yAxis): int
286
{
29-
public int $score = 0;
7+
$location = $xAxis ** 2 + $yAxis ** 2;
308

31-
public function __construct(float $xAxis, float $yAxis)
32-
{
33-
$this->score = $this->calculateScore($xAxis, $yAxis);
9+
if ($location > 100) {
10+
return 0;
3411
}
35-
36-
private function calculateScore(float $xAxis, float $yAxis): int
37-
{
38-
$location = $xAxis ** 2 + $yAxis ** 2;
39-
40-
if ($location > 100) {
41-
return 0;
42-
}
43-
if ($location > 25) {
44-
return 1;
45-
}
46-
if ($location > 1) {
47-
return 5;
48-
}
49-
50-
return 10;
12+
if ($location > 25) {
13+
return 1;
14+
}
15+
if ($location > 1) {
16+
return 5;
5117
}
18+
19+
return 10;
5220
}
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.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,7 @@
2424

2525
declare(strict_types=1);
2626

27-
class Darts
27+
function score(float $xAxis, float $yAxis): int
2828
{
29-
public int $score = 0;
30-
31-
public function __construct(float $xAxis, float $yAxis)
32-
{
33-
throw new BadFunctionCallException("Please implement the Darts class!");
34-
}
29+
throw new BadFunctionCallException("Please implement the __FUNCTION__ function!");
3530
}
Lines changed: 65 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,5 @@
11
<?php
22

3-
/*
4-
* By adding type hints and enabling strict type checking, code can become
5-
* easier to read, self-documenting and reduce the number of potential bugs.
6-
* By default, type declarations are non-strict, which means they will attempt
7-
* to change the original type to match the type specified by the
8-
* type-declaration.
9-
*
10-
* In other words, if you pass a string to a function requiring a float,
11-
* it will attempt to convert the string value to a float.
12-
*
13-
* To enable strict mode, a single declare directive must be placed at the top
14-
* of the file.
15-
* This means that the strictness of typing is configured on a per-file basis.
16-
* This directive not only affects the type declarations of parameters, but also
17-
* a function's return type.
18-
*
19-
* For more info review the Concept on strict type checking in the PHP track
20-
* <link>.
21-
*
22-
* To disable strict typing, comment out the directive below.
23-
*/
24-
253
declare(strict_types=1);
264

275
class DartsTest extends PHPUnit\Framework\TestCase
@@ -31,81 +9,120 @@ public static function setUpBeforeClass(): void
319
require_once 'Darts.php';
3210
}
3311

12+
/**
13+
* uuid 9033f731-0a3a-4d9c-b1c0-34a1c8362afb
14+
* @testdox Missed target
15+
*/
3416
public function testMissedTarget(): void
3517
{
36-
$board = new Darts(-9, 9);
37-
$this->assertEquals(0, $board->score);
18+
$this->assertEquals(0, score(-9.0, 9.0));
3819
}
3920

21+
/**
22+
* uuid 4c9f6ff4-c489-45fd-be8a-1fcb08b4d0ba
23+
* @testdox On the outer circle
24+
*/
4025
public function testInOuterCircle(): void
4126
{
42-
$board = new Darts(0, 10);
43-
$this->assertEquals(1, $board->score);
27+
$this->assertEquals(1, score(0.0, 10.0));
4428
}
4529

30+
/**
31+
* uuid 14378687-ee58-4c9b-a323-b089d5274be8
32+
* @testdox On the middle circle
33+
*/
4634
public function testInMiddleCircle(): void
4735
{
48-
$board = new Darts(-5, 0);
49-
$this->assertEquals(5, $board->score);
36+
$this->assertEquals(5, score(-5.0, 0.0));
5037
}
5138

39+
/**
40+
* uuid 849e2e63-85bd-4fed-bc3b-781ae962e2c9
41+
* @testdox On the inner circle
42+
*/
5243
public function testInInnerCircle(): void
5344
{
54-
$board = new Darts(0, -1);
55-
$this->assertEquals(10, $board->score);
45+
$this->assertEquals(10, score(0.0, -1.0));
5646
}
5747

48+
/**
49+
* uuid 1c5ffd9f-ea66-462f-9f06-a1303de5a226
50+
* @testdox Exactly on center
51+
*/
5852
public function testInCenter(): void
5953
{
60-
$board = new Darts(0, 0);
61-
$this->assertEquals(10, $board->score);
54+
$this->assertEquals(10, score(0.0, 0.0));
6255
}
6356

57+
/**
58+
* uuid b65abce3-a679-4550-8115-4b74bda06088
59+
* @testdox Near the center
60+
*/
6461
public function testNearCenter(): void
6562
{
66-
$board = new Darts(-0.1, -0.1);
67-
$this->assertEquals(10, $board->score);
63+
$this->assertEquals(10, score(-0.1, -0.1));
6864
}
6965

66+
/**
67+
* uuid 66c29c1d-44f5-40cf-9927-e09a1305b399
68+
* @testdox Just within the inner circle
69+
*/
7070
public function testJustInsideCenter(): void
7171
{
72-
$board = new Darts(0.7, 0.7);
73-
$this->assertEquals(10, $board->score);
72+
$this->assertEquals(10, score(0.7, 0.7));
7473
}
7574

75+
/**
76+
* uuid d1012f63-c97c-4394-b944-7beb3d0b141a
77+
* @testdox Just outside the inner circle
78+
*/
7679
public function testJustOutsideCenter(): void
7780
{
78-
$board = new Darts(0.8, -0.8);
79-
$this->assertEquals(5, $board->score);
81+
$this->assertEquals(5, score(0.8, -0.8));
8082
}
8183

84+
/**
85+
* uuid ab2b5666-b0b4-49c3-9b27-205e790ed945
86+
* @testdox Just within the middle circle
87+
*/
8288
public function testJustWithinMiddleCircle(): void
8389
{
84-
$board = new Darts(-3.5, 3.5);
85-
$this->assertEquals(5, $board->score);
90+
$this->assertEquals(5, score(-3.5, 3.5));
8691
}
8792

93+
/**
94+
* uuid 70f1424e-d690-4860-8caf-9740a52c0161
95+
* @testdox Just outside the middle circle
96+
*/
8897
public function testJustOutsideMiddleCircle(): void
8998
{
90-
$board = new Darts(-3.6, -3.6);
91-
$this->assertEquals(1, $board->score);
99+
$this->assertEquals(1, score(-3.6, -3.6));
92100
}
93101

102+
/**
103+
* uuid a7dbf8db-419c-4712-8a7f-67602b69b293
104+
* @testdox Just within the outer circle
105+
*/
94106
public function testJustInsideOuterCircle(): void
95107
{
96-
$board = new Darts(-7.0, 7.0);
97-
$this->assertEquals(1, $board->score);
108+
$this->assertEquals(1, score(-7.0, 7.0));
98109
}
99110

111+
/**
112+
* uuid e0f39315-9f9a-4546-96e4-a9475b885aa7
113+
* @testdox Just outside the outer circle
114+
*/
100115
public function testJustOutsideOuterCircle(): void
101116
{
102-
$board = new Darts(7.1, -7.1);
103-
$this->assertEquals(0, $board->score);
117+
$this->assertEquals(0, score(7.1, -7.1));
104118
}
105119

120+
/**
121+
* uuid 045d7d18-d863-4229-818e-b50828c75d19
122+
* @testdox Asymmetric position between the inner and middle circles
123+
*/
106124
public function testAsymmetricPositionBetweenInnerAndOuterCircles(): void
107125
{
108-
$board = new Darts(0.5, -4);
109-
$this->assertEquals(5, $board->score);
126+
$this->assertEquals(5, score(0.5, -4));
110127
}
111128
}

0 commit comments

Comments
 (0)