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

# lujanfernaud's solution

## to Darts in the Ruby Track

Published at May 24 2020 · 0 comments
Instructions
Test suite
Solution

Write a method 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: 0 points.
• in the outer circle of the target: 1 point.
• in the middle circle of the target: 5 points.
• in the inner circle of the target: 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. They are all centered to the same point (That is, the circles are concentric) defined by the coordinates (0, 0).

Write a method 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.

For installation and learning resources, refer to the Ruby resources page.

For running the tests provided, you will need the Minitest gem. Open a terminal window and run the following command to install minitest:

``````gem install minitest
``````

If you would like color output, you can `require 'minitest/pride'` in the test file, or note the alternative instruction, below, for running the test file.

Run the tests from the exercise directory using the following command:

``````ruby darts_test.rb
``````

To include color from the command line:

``````ruby -r minitest/pride darts_test.rb
``````

## Source

Inspired by an exercise 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.

### darts_test.rb

``````require 'minitest/autorun'
require_relative 'darts'

# Common test data version: 2.2.0 f60c43b
class DartsTest < Minitest::Test
def test_missed_target
# skip
darts = Darts.new(-9, 9)
assert_equal(0, darts.score)
end

def test_on_the_outer_circle
skip
darts = Darts.new(0, 10)
assert_equal(1, darts.score)
end

def test_on_the_middle_circle
skip
darts = Darts.new(-5, 0)
assert_equal(5, darts.score)
end

def test_on_the_inner_circle
skip
darts = Darts.new(0, -1)
assert_equal(10, darts.score)
end

def test_exactly_on_centre
skip
darts = Darts.new(0, 0)
assert_equal(10, darts.score)
end

def test_near_the_centre
skip
darts = Darts.new(-0.1, -0.1)
assert_equal(10, darts.score)
end

def test_just_within_the_inner_circle
skip
darts = Darts.new(0.7, 0.7)
assert_equal(10, darts.score)
end

def test_just_outside_the_inner_circle
skip
darts = Darts.new(0.8, -0.8)
assert_equal(5, darts.score)
end

def test_just_within_the_middle_circle
skip
darts = Darts.new(-3.5, 3.5)
assert_equal(5, darts.score)
end

def test_just_outside_the_middle_circle
skip
darts = Darts.new(-3.6, -3.6)
assert_equal(1, darts.score)
end

def test_just_within_the_outer_circle
skip
darts = Darts.new(-7.0, 7.0)
assert_equal(1, darts.score)
end

def test_just_outside_the_outer_circle
skip
darts = Darts.new(7.1, -7.1)
assert_equal(0, darts.score)
end

def test_asymmetric_position_between_the_inner_and_middle_circles
skip
darts = Darts.new(0.5, -4)
assert_equal(5, darts.score)
end
end``````
``````class Darts
CIRCLE_TO_RANGE = {
inner:  0.0..1.4,
middle: 1.4..7.0,
outer:  7.0..14.0
}.freeze

POINTS = {
inner:  10,
middle: 5,
outer:  1
}.freeze

def initialize(*coordinates)
@position = coordinates.map(&:abs).sum
end

def score
case @position
when CIRCLE_TO_RANGE[:inner]  then POINTS[:inner]
when CIRCLE_TO_RANGE[:middle] then POINTS[:middle]
when CIRCLE_TO_RANGE[:outer]  then POINTS[:outer]
else 0
end
end
end``````