ðŸŽ‰ Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io ðŸŽ‰

Published at Aug 18 2019
·
0 comments

Instructions

Test suite

Solution

Write a function that returns the earned points in a single toss of a Darts game.

Darts is a game where players throw darts to a target.

In our particular instance of the game, the target rewards with 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 to 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 `real`

cartesian coordinates `x`

and `y`

), returns the correct amount earned by a dart landing in that point.
The Scala exercises assume an SBT project scheme. The exercise solution source
should be placed within the exercise directory/src/main/scala. The exercise
unit tests can be found within the exercise directory/src/test/scala.

To run the tests simply run the command `sbt test`

in the exercise directory.

For more detailed info about the Scala track see the help page.

Inspired by an excersie created by a professor Della Paolera in Argentina

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

```
import org.scalatest.{Matchers, FunSuite}
/** @version 1.0.0 */
class DartsTest extends FunSuite with Matchers {
test("A dart lands outside the target") {
Darts.score(15.3, 13.2) should be (0)
}
test("A dart lands just in the border of the target") {
pending
Darts.score(10, 0) should be (1)
}
test("A dart lands in the middle circle") {
pending
Darts.score(3, 3.7) should be (5)
}
test("A dart lands right in the border between outside and middle circles") {
pending
Darts.score(0, 5) should be (5)
}
test("A dart lands in the inner circle") {
pending
Darts.score(0, 0) should be (10)
}
}
```

```
object Darts {
def score(x: Double, y: Double): Int = math.hypot(x, y) match {
case res if res <= 1 => 10
case res if res <= 5 => 5
case res if res <= 10 => 1
case _ => 0
}
}
```

A huge amount can be learned from reading other peopleâ€™s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

- What compromises have been made?
- Are there new concepts here that you could read more about to improve your understanding?

Level up your programming skills with 3,423 exercises across 52 languages, and insightful discussion with our volunteer team of welcoming mentors.
Exercism is
**100% free forever**.

## Community comments