Skip to content

Commit

Permalink
Merge pull request #592 from ricemery/dart
Browse files Browse the repository at this point in the history
Add darts exercise
  • Loading branch information
ErikSchierboom authored Jan 1, 2019
2 parents 57e40ce + 6d13c21 commit a232c8f
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 0 deletions.
12 changes: 12 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -1088,6 +1088,18 @@
"strings",
"transforming"
]
},
{
"slug": "darts",
"uuid": "939bfbf2-55c9-4c54-b2ef-3247bb6fc67c",
"core": false,
"unlocked_by": "sum-of-multiples",
"difficulty": 1,
"topics": [
"math",
"control_flow_if_else_statements",
"floating_point_numbers"
]
}
]
}
33 changes: 33 additions & 0 deletions exercises/darts/README.md
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.
3 changes: 3 additions & 0 deletions exercises/darts/build.sbt
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"
11 changes: 11 additions & 0 deletions exercises/darts/example.scala
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.
29 changes: 29 additions & 0 deletions exercises/darts/src/test/scala/DartsTest.scala
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)
}
}
15 changes: 15 additions & 0 deletions testgen/src/main/scala/DartsTestGenerator.scala
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"-------------")
}
}

0 comments on commit a232c8f

Please sign in to comment.