Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

darts: Port Darts Exercise #1019

Merged
merged 4 commits into from
Apr 25, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -1222,6 +1222,17 @@
"topics": [
"loops"
]
},
{
"slug": "darts",
"uuid": "15b73808-78a4-4854-b7cf-82a478107024",
"core": false,
"unlocked_by": null,
"difficulty": 3,
"topics": [
"math",
"geometry"
]
}
]
}
10 changes: 10 additions & 0 deletions exercises/darts/.meta/generator/darts_case.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
require 'generator/exercise_case'

class DartsCase < Generator::ExerciseCase
def workload
[
"darts = Darts.new(#{input["x"]}, #{input["y"]})\n",
"assert_equal(#{expected.inspect}, darts.score)\n"
].join
end
end
29 changes: 29 additions & 0 deletions exercises/darts/.meta/solutions/darts.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class Darts
attr_reader :x, :y

def initialize(x, y)
@x = x
@y = y
end

def score
scores.detect do |range, _|
range.cover? distance_to_bullseye
end[1]
end

private

def scores
{
0.0..1.0 => 10,
1.0..5.0 => 5,
5.0..10.0 => 1,
10.0..1.0/0 => 0,
}
end

def distance_to_bullseye
Math.sqrt(x * x + y * y)
end
end
46 changes: 46 additions & 0 deletions exercises/darts/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Darts

Write a function that returns the earned points in a single toss of a Darts game.

[Darts](https://en.wikipedia.org/wiki/Darts) is a game where players
throw darts to a [target](https://en.wikipedia.org/wiki/Darts#/media/File:Darts_in_a_dartboard.jpg).

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.

kotp marked this conversation as resolved.
Show resolved Hide resolved
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](http://mathworld.wolfram.com/ConcentricCircles.html)) defined by the coordinates (0, 0).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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](http://mathworld.wolfram.com/ConcentricCircles.html)) defined by the coordinates (0, 0).
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](http://mathworld.wolfram.com/ConcentricCircles.html)) defined by the coordinates (0, 0).

The "Of course, " can be removed as we are providing the course.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be suggested at the source, rather than locally here.


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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.
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.

Cartesian is a name, and capitalized. The use of a comma is consistent and works because without the explanation would make a complete sentence itself.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The change from "function" to "method" is a local Ruby change, and not suitable for the Readme in problem-specifications, while the missing comma should be suggested at the original source.

* * * *

For installation and learning resources, refer to the
[Ruby resources page](http://exercism.io/languages/ruby/resources).

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.
83 changes: 83 additions & 0 deletions exercises/darts/darts_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
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