Skip to content

Commit 8844396

Browse files
lemoncurryFridaTveit
authored andcommitted
darts: add new exercise (exercism#1605)
* darts: add exercise * Add config.json * Add README.md * Add reference implementation Darts.java * Change dif level of exercise darts * Change field names * Add starter implementation for exercise dif level <5 * Remove math from topics exercise darts config.json * Delete obsolete method
1 parent e2c22df commit 8844396

8 files changed

Lines changed: 149 additions & 0 deletions

File tree

‎config.json‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,16 @@
4141
"strings"
4242
]
4343
},
44+
{
45+
"slug": "darts",
46+
"uuid": "4d400a44-b190-4a0c-affb-99fad8ea18da",
47+
"core": false,
48+
"unlocked_by": "two-fer",
49+
"difficulty": 2,
50+
"topics": [
51+
"games"
52+
]
53+
},
4454
{
4555
"slug": "armstrong-numbers",
4656
"uuid": "8e1dd48c-e05e-4a72-bf0f-5aed8dd123f5",
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class Darts {
2+
3+
private static final int RADIUS_CIRCLE_OUTER = 10;
4+
private static final int RADIUS_CIRCLE_MIDDLE = 5;
5+
private static final int RADIUS_CIRCLE_INNER = 1;
6+
7+
private double x;
8+
private double y;
9+
10+
Darts(double x, double y) {
11+
this.x = x;
12+
this.y = y;
13+
}
14+
15+
int score() {
16+
if (RADIUS_CIRCLE_MIDDLE < distanceDartToOrigin()
17+
&& distanceDartToOrigin() <= RADIUS_CIRCLE_OUTER) {
18+
return 1;
19+
} else if (RADIUS_CIRCLE_INNER < distanceDartToOrigin()
20+
&& distanceDartToOrigin() <= RADIUS_CIRCLE_MIDDLE) {
21+
return 5;
22+
} else if (distanceDartToOrigin() <= RADIUS_CIRCLE_INNER) {
23+
return 10;
24+
}
25+
return 0;
26+
}
27+
28+
private double distanceDartToOrigin() {
29+
return Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));
30+
}
31+
32+
}

‎exercises/darts/.meta/version‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1.0.0

‎exercises/darts/README.md‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
..# Darts
2+
3+
Write a function that returns the earned points in a single toss of a Darts game.
4+
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).
7+
8+
In our particular instance of the game, the target rewards with 4 different amounts of points, depending on where the dart lands:
9+
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.
14+
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](http://mathworld.wolfram.com/ConcentricCircles.html)) defined by the coordinates (0, 0).
16+
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.
18+
# Running the tests
19+
20+
You can run all the tests for an exercise by entering
21+
22+
```sh
23+
$ gradle test
24+
```
25+
26+
in your terminal.
27+
28+
## Source
29+
30+
Inspired by an excersie created by a professor Della Paolera in Argentina
31+
32+
## Submitting Incomplete Solutions
33+
34+
It's possible to submit an incomplete solution so you can see how others have completed the exercise.

‎exercises/darts/build.gradle‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
apply plugin: "java"
2+
apply plugin: "eclipse"
3+
apply plugin: "idea"
4+
5+
repositories {
6+
mavenCentral()
7+
}
8+
9+
dependencies {
10+
testCompile "junit:junit:4.12"
11+
}
12+
13+
test {
14+
testLogging {
15+
exceptionFormat = 'full'
16+
events = ["passed", "failed", "skipped"]
17+
}
18+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Darts {
2+
3+
Darts(double x, double y) {
4+
throw new UnsupportedOperationException("Delete this statement and write your own implementation.");
5+
}
6+
7+
int score() {
8+
throw new UnsupportedOperationException("Delete this statement and write your own implementation.");
9+
}
10+
11+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import org.junit.Ignore;
2+
import org.junit.Test;
3+
4+
import static org.junit.Assert.assertEquals;
5+
6+
public class DartsTest {
7+
8+
@Test
9+
public void testDartOutsideTarget() {
10+
Darts darts = new Darts(15.3, 13.2);
11+
assertEquals(0, darts.score());
12+
}
13+
14+
@Ignore("Remove to run test")
15+
@Test
16+
public void testDartLandsOnBorderOfTarget() {
17+
Darts darts = new Darts(10, 0);
18+
assertEquals(1, darts.score());
19+
}
20+
21+
@Ignore("Remove to run test")
22+
@Test
23+
public void testDartLandsOnMiddleOfCircle() {
24+
Darts darts = new Darts(3, 3.7);
25+
assertEquals(5, darts.score());
26+
}
27+
28+
@Ignore("Remove to run test")
29+
@Test
30+
public void testDartLandsOnBorderBetweenOutsideAndMiddleCircles() {
31+
Darts darts = new Darts(0, 5);
32+
assertEquals(5, darts.score());
33+
}
34+
35+
@Ignore("Remove to run test")
36+
@Test
37+
public void testDartLandsInTheInnerCircle() {
38+
Darts darts = new Darts(0, 0);
39+
assertEquals(10, darts.score());
40+
}
41+
42+
}

‎exercises/settings.gradle‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ include 'collatz-conjecture'
2323
include 'complex-numbers'
2424
include 'crypto-square'
2525
include 'custom-set'
26+
include 'darts'
2627
include 'diamond'
2728
include 'difference-of-squares'
2829
include 'diffie-hellman'

0 commit comments

Comments
 (0)