module Darts | |||||
let withinCircle (radius: int) (x: double) (y: double) = | |||||
sqrt ((pown x 2) + (pown y 2)) <= double(radius) | |||||
let score (x: double) (y: double): int = | |||||
if withinCircle 10 x y then | |||||
if withinCircle 5 x y then | |||||
if withinCircle 1 x y then | |||||
10 | |||||
else | |||||
5 | |||||
else | |||||
1 | |||||
else 0 |
<Project Sdk="Microsoft.NET.Sdk"> | |||||
<PropertyGroup> | |||||
<TargetFramework>netcoreapp3.0</TargetFramework> | |||||
<IsPackable>false</IsPackable> | |||||
</PropertyGroup> | |||||
<ItemGroup> | |||||
<Compile Include="Darts.fs" /> | |||||
<Compile Include="DartsTests.fs" /> | |||||
</ItemGroup> | |||||
<ItemGroup> | |||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.6.1" /> | |||||
<PackageReference Include="xunit" Version="2.4.1" /> | |||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.2" /> | |||||
<PackageReference Include="FsUnit.xUnit" Version="3.8.1" /> | |||||
</ItemGroup> | |||||
</Project> |
// This file was auto-generated based on version 2.2.0 of the canonical data. | |||||
module DartsTests | |||||
open FsUnit.Xunit | |||||
open Xunit | |||||
open Darts | |||||
[<Fact>] | |||||
let ``Missed target`` () = | |||||
score -9.0 9.0 |> should equal 0 | |||||
[<Fact>] | |||||
let ``On the outer circle`` () = | |||||
score 0.0 10.0 |> should equal 1 | |||||
[<Fact>] | |||||
let ``On the middle circle`` () = | |||||
score -5.0 0.0 |> should equal 5 | |||||
[<Fact>] | |||||
let ``On the inner circle`` () = | |||||
score 0.0 -1.0 |> should equal 10 | |||||
[<Fact>] | |||||
let ``Exactly on centre`` () = | |||||
score 0.0 0.0 |> should equal 10 | |||||
[<Fact>] | |||||
let ``Near the centre`` () = | |||||
score -0.1 -0.1 |> should equal 10 | |||||
[<Fact>] | |||||
let ``Just within the inner circle`` () = | |||||
score 0.7 0.7 |> should equal 10 | |||||
[<Fact>] | |||||
let ``Just outside the inner circle`` () = | |||||
score 0.8 -0.8 |> should equal 5 | |||||
[<Fact>] | |||||
let ``Just within the middle circle`` () = | |||||
score -3.5 3.5 |> should equal 5 | |||||
[<Fact>] | |||||
let ``Just outside the middle circle`` () = | |||||
score -3.6 -3.6 |> should equal 1 | |||||
[<Fact>] | |||||
let ``Just within the outer circle`` () = | |||||
score -7.0 7.0 |> should equal 1 | |||||
[<Fact>] | |||||
let ``Just outside the outer circle`` () = | |||||
score 7.1 -7.1 |> should equal 0 | |||||
[<Fact>] | |||||
let ``Asymmetric position between the inner and middle circles`` () = | |||||
score 0.5 -4.0 |> should equal 5 | |||||
# 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. | |||||
## Running the tests | |||||
To run the tests, run the command `dotnet test` from within the exercise directory. | |||||
## Autoformatting the code | |||||
F# source code can be formatted with the [Fantomas](https://github.com/fsprojects/fantomas) tool. | |||||
After installing it with `dotnet tool restore`, run `dotnet fantomas .` to format code within the current directory. | |||||
## Further information | |||||
For more detailed information about the F# track, including how to get help if | |||||
you're having trouble, please visit the exercism.io [F# language page](http://exercism.io/languages/fsharp/resources). | |||||
## Source | |||||
Inspired by an exercise created by a professor Della Paolera in Argentina | |||||