 artemkorsakov's solution

to Darts in the C# Track

Published at Feb 20 2019 · 6 comments
Instructions
Test suite
Solution

Note:

This exercise has changed since this solution was written.

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.

Running the tests

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

Further information

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.

Source

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

Submitting Incomplete Solutions

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

DartsTest.cs

// This file was auto-generated based on version 1.0.0 of the canonical data.

using Xunit;

public class DartsTest
{
[Fact]
public void A_dart_lands_outside_the_target()
{
Assert.Equal(0, Darts.Score(15.3, 13.2));
}

[Fact(Skip = "Remove to run test")]
public void A_dart_lands_just_in_the_border_of_the_target()
{
Assert.Equal(1, Darts.Score(10, 0));
}

[Fact(Skip = "Remove to run test")]
public void A_dart_lands_in_the_middle_circle()
{
Assert.Equal(5, Darts.Score(3, 3.7));
}

[Fact(Skip = "Remove to run test")]
public void A_dart_lands_right_in_the_border_between_outside_and_middle_circles()
{
Assert.Equal(5, Darts.Score(0, 5));
}

[Fact(Skip = "Remove to run test")]
public void A_dart_lands_in_the_inner_circle()
{
Assert.Equal(10, Darts.Score(0, 0));
}
}
﻿using System;

public static class Darts
{
public static int Score(double x, double y)
{
var radius = Math.Sqrt(x * x + y * y);
return radius - 1 < double.Epsilon ? 10 :
radius - 5 < double.Epsilon ? 5 :
radius - 10 < double.Epsilon ? 1 :
0;
}
}

Find this solution interesting? Ask the author a question to learn more. HI! May I ask you, why did you use this Math.Sqrt(x * x + y * y)/same: sqrt{x²+y²} formula? Solution Author
commented 154 days ago

Hi! I reasoned like this: The point is located in (x, y) coordinates - this means that the radius is the hypotenuse of the triangle with the x, y legs. Then I used (the Pythagorean theorem): x²+y² = radius². I hope that I reasoned correctly. the radius is the hypotenuse of the triangle with the x, y legs

Is the radius not actually the "legs" x, y up to a certain point (x: -10 / +10) (y: -10 / +10 - for a whole circle)? And hypotenuse the segment between x and y?

And when we talk about triangles (four of them), does that mean we have a square in the end, right? Maybe I do not understand it correctly, but are we not talking about circles? Solution Author
commented 153 days ago

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).

The centre of the circles is the point (0, 0). I think the radius is the distance from the point (0, 0) to the point (x, y). The distance from the point (xa, ya) to the point (xb, yb) equals distance = sqrt{(xb - xa)² + (yb - ya)²} or distance = sqrt{x²+y²}.

I think If the borders are defined by dots (10, 0), (-10, 0), (0, -10), (0, 10) it's a square, not a circle. I need to draw that in order to understand, but now i got it! Thanks a lot! Большое спасибо!

What can you learn from this solution?

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?