Avatar of infernalred

infernalred's solution

to Darts in the C# Track

Published at Oct 04 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.

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

DartsTest.cs

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

using Xunit;

public class DartsTest
{
    [Fact]
    public void Missed_target()
    {
        Assert.Equal(0, Darts.Score(-9, 9));
    }

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

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

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

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

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

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

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

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

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

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

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

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

Darts.cs

using System;

public static class Darts
{
    public static int Score(double x, double y)
    {
        double point = Math.Sqrt(x * x + y * y);
        return point <= 1.0 ? 10 : point <= 5.0 ? 5 : point <= 10.0 ? 1 : 0;
    }
}

DartsTest.cs

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

using Xunit;

public class DartsTest
{
    [Fact]
    public void Missed_target()
    {
        Assert.Equal(0, Darts.Score(-9, 9));
    }

    [Fact]
    public void On_the_outer_circle()
    {
        Assert.Equal(1, Darts.Score(0, 10));
    }

    [Fact]
    public void On_the_middle_circle()
    {
        Assert.Equal(5, Darts.Score(-5, 0));
    }

    [Fact]
    public void On_the_inner_circle()
    {
        Assert.Equal(10, Darts.Score(0, -1));
    }

    [Fact]
    public void Exactly_on_centre()
    {
        Assert.Equal(10, Darts.Score(0, 0));
    }

    [Fact]
    public void Near_the_centre()
    {
        Assert.Equal(10, Darts.Score(-0.1, -0.1));
    }

    [Fact]
    public void Just_within_the_inner_circle()
    {
        Assert.Equal(10, Darts.Score(0.7, 0.7));
    }

    [Fact]
    public void Just_outside_the_inner_circle()
    {
        Assert.Equal(5, Darts.Score(0.8, -0.8));
    }

    [Fact]
    public void Just_within_the_middle_circle()
    {
        Assert.Equal(5, Darts.Score(-3.5, 3.5));
    }

    [Fact]
    public void Just_outside_the_middle_circle()
    {
        Assert.Equal(1, Darts.Score(-3.6, -3.6));
    }

    [Fact]
    public void Just_within_the_outer_circle()
    {
        Assert.Equal(1, Darts.Score(-7, 7));
    }

    [Fact]
    public void Just_outside_the_outer_circle()
    {
        Assert.Equal(0, Darts.Score(7.1, -7.1));
    }

    [Fact]
    public void Asymmetric_position_between_the_inner_and_middle_circles()
    {
        Assert.Equal(5, Darts.Score(0.5, -4));
    }
}

Community comments

Find this solution interesting? Ask the author a question to learn more.

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?