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.

To run the tests, run the command `dotnet test`

from within the exercise directory.

Initially, only the first test will be enabled. This is to encourage you to solve the exercise one step at a time.
Once you get the first test passing, remove the `Skip`

property from the next test and work on getting that test passing.
Once none of the tests are skipped and they are all passing, you can submit your solution
using `exercism submit Darts.cs`

For more detailed information about the C# track, including how to get help if you're having trouble, please visit the exercism.io C# language page.

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

```
// This file was auto-generated based on version 1.1.0 of the canonical data.
using Xunit;
public class DartsTest
{
[Fact]
public void A_dart_lands_outside_the_target()
{
Assert.Equal(0, Darts.Score(-9, 9));
}
[Fact(Skip = "Remove to run test")]
public void A_dart_lands_just_in_the_border_of_the_target()
{
Assert.Equal(1, Darts.Score(0, 10));
}
[Fact(Skip = "Remove to run test")]
public void A_dart_lands_in_the_outer_circle()
{
Assert.Equal(1, Darts.Score(4, 4));
}
[Fact(Skip = "Remove to run test")]
public void A_dart_lands_right_in_the_border_between_outer_and_middle_circles()
{
Assert.Equal(5, Darts.Score(5, 0));
}
[Fact(Skip = "Remove to run test")]
public void A_dart_lands_in_the_middle_circle()
{
Assert.Equal(5, Darts.Score(0.8, -0.8));
}
[Fact(Skip = "Remove to run test")]
public void A_dart_lands_right_in_the_border_between_middle_and_inner_circles()
{
Assert.Equal(10, Darts.Score(0, -1));
}
[Fact(Skip = "Remove to run test")]
public void A_dart_lands_in_the_inner_circle()
{
Assert.Equal(10, Darts.Score(-0.1, -0.1));
}
}
```

```
using System;
public static class Darts
{
public static int Score(double x, double y)
{
double distance = Math.Sqrt(Math.Pow(x, 2) + Math.Pow(y, 2));
return distance <= 1 ? 10 : distance <= 5 ? 5 : distance <= 10 ? 1 : 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,385 exercises across 50 languages, and insightful discussion with our volunteer team of welcoming mentors.
Exercism is
**100% free forever**.

## Community comments