Avatar of artemkorsakov

artemkorsakov's solution

to Darts in the Java Track

Published at Feb 01 2019 · 0 comments
Instructions
Test suite
Solution

Note:

This exercise has changed since this solution was written.

..# Darts

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

You can run all the tests for an exercise by entering

$ gradle test

in your terminal.

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

import org.junit.Ignore;
import org.junit.Test;

import static org.junit.Assert.assertEquals;

public class DartsTest {

    @Test
    public void testDartOutsideTarget() {
        Darts darts = new Darts(15.3, 13.2);
        assertEquals(0, darts.score());
    }

    @Ignore("Remove to run test")
    @Test
    public void testDartLandsOnBorderOfTarget() {
        Darts darts = new Darts(10, 0);
        assertEquals(1, darts.score());
    }

    @Ignore("Remove to run test")
    @Test
    public void testDartLandsOnMiddleOfCircle() {
        Darts darts = new Darts(3, 3.7);
        assertEquals(5, darts.score());
    }

    @Ignore("Remove to run test")
    @Test
    public void testDartLandsOnBorderBetweenOutsideAndMiddleCircles() {
        Darts darts = new Darts(0, 5);
        assertEquals(5, darts.score());
    }

    @Ignore("Remove to run test")
    @Test
    public void testDartLandsInTheInnerCircle() {
        Darts darts = new Darts(0, 0);
        assertEquals(10, darts.score());
    }

}
class Darts {
    private int score;

    Darts(double x, double y) {
        double radius = Math.sqrt(x*x + y*y);
        score = radius > 10 ? 0 : radius > 5 ? 1 : radius > 1 ? 5 : 10;
    }

    int score() {
        return score;
    }

}

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?