-
-
Notifications
You must be signed in to change notification settings - Fork 132
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #592 from ricemery/dart
Add darts exercise
- Loading branch information
Showing
7 changed files
with
103 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# 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. | ||
|
||
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). | ||
|
||
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. | ||
The Scala exercises assume an SBT project scheme. The exercise solution source | ||
should be placed within the exercise directory/src/main/scala. The exercise | ||
unit tests can be found within the exercise directory/src/test/scala. | ||
|
||
To run the tests simply run the command `sbt test` in the exercise directory. | ||
|
||
For more detailed info about the Scala track see the [help | ||
page](http://exercism.io/languages/scala). | ||
|
||
|
||
## 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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
scalaVersion := "2.12.1" | ||
|
||
libraryDependencies += "org.scalatest" %% "scalatest" % "3.0.1" % "test" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
object Darts { | ||
def score(x: Double, y: Double): Int = { | ||
val distance = math.sqrt(x * x + y * y) | ||
distance match { | ||
case _ if distance <= 1.0 => 10 | ||
case _ if distance <= 5.0 => 5 | ||
case _ if distance <= 10.0 => 1 | ||
case _ => 0 | ||
} | ||
} | ||
} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import org.scalatest.{Matchers, FunSuite} | ||
|
||
/** @version 1.0.0 */ | ||
class DartsTest extends FunSuite with Matchers { | ||
|
||
test("A dart lands outside the target") { | ||
Darts.score(15.3, 13.2) should be (0) | ||
} | ||
|
||
test("A dart lands just in the border of the target") { | ||
pending | ||
Darts.score(10, 0) should be (1) | ||
} | ||
|
||
test("A dart lands in the middle circle") { | ||
pending | ||
Darts.score(3, 3.7) should be (5) | ||
} | ||
|
||
test("A dart lands right in the border between outside and middle circles") { | ||
pending | ||
Darts.score(0, 5) should be (5) | ||
} | ||
|
||
test("A dart lands in the inner circle") { | ||
pending | ||
Darts.score(0, 0) should be (10) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import java.io.File | ||
|
||
import testgen.TestSuiteBuilder._ | ||
import testgen._ | ||
|
||
object DartsTestGenerator { | ||
def main(args: Array[String]): Unit = { | ||
val file = new File("src/main/resources/darts.json") | ||
|
||
val code = TestSuiteBuilder.build(file, fromLabeledTestFromInput("x", "y")) | ||
println(s"-------------") | ||
println(code) | ||
println(s"-------------") | ||
} | ||
} |