Skip to content

Commit

Permalink
New exercise - darts
Browse files Browse the repository at this point in the history
  • Loading branch information
GroophyLifefor committed Mar 19, 2024
1 parent 80ae56e commit 1d3eca3
Show file tree
Hide file tree
Showing 6 changed files with 222 additions and 0 deletions.
31 changes: 31 additions & 0 deletions exercises/practice/darts/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Description

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

[Darts][darts] is a game where players throw darts at a [target][darts-target].

In our particular instance of the game, the target rewards 4 different amounts of points, depending on where the dart lands:

![Our dart scoreboard with values from a complete miss to a bullseye](https://assets.exercism.org/images/exercises/darts/darts-scoreboard.svg)

- 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 at the same point — that is, the circles are [concentric][] defined by the coordinates (0, 0).

Write a function that given a point in the target (defined by its [Cartesian coordinates][cartesian-coordinates] `x` and `y`, where `x` and `y` are [real][real-numbers]), returns the correct amount earned by a dart landing at that point.

## Credit

The scoreboard image was created by [habere-et-dispertire][habere-et-dispertire] using [Inkscape][inkscape].

[darts]: https://en.wikipedia.org/wiki/Darts
[darts-target]: https://en.wikipedia.org/wiki/Darts#/media/File:Darts_in_a_dartboard.jpg
[concentric]: https://mathworld.wolfram.com/ConcentricCircles.html
[cartesian-coordinates]: https://www.mathsisfun.com/data/cartesian-coordinates.html
[real-numbers]: https://www.mathsisfun.com/numbers/real-numbers.html
[habere-et-dispertire]: https://exercism.org/profiles/habere-et-dispertire
[inkscape]: https://en.wikipedia.org/wiki/Inkscape
23 changes: 23 additions & 0 deletions exercises/practice/darts/.meta/Example.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
@echo off
setlocal enabledelayedexpansion

set "x=%~1"
set "y=%~2"
set "result="

set "Sqrt(N)=( x=(N)/(11*1024)+40, x=((N)/x+x)/2, x=((N)/x+x)/2, x=((N)/x+x)/2, x=((N)/x+x)/2, x=((N)/x+x)/2 )"

set /a p=x * x + y * y
set /a "dart_location=!sqrt(n):n=%p%!
if %dart_location% LEQ 1 (
set result=10
) else if %dart_location% LEQ 5 (
set result=5
) else if %dart_location% LSS 10 (
set result=1
) else (
set result=0
)
echo %result%
16 changes: 16 additions & 0 deletions exercises/practice/darts/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"authors": ["GroophyLifefor"],
"files": {
"solution": [
"Darts.bat"
],
"test": [
"DartsTest.bat"
],
"example": [
".meta/Example.bat"
]
},
"blurb": "Write a function that returns the earned points in a single toss of a Darts game.",
"source": "Inspired by an exercise created by a professor Della Paolera in Argentina"
}
13 changes: 13 additions & 0 deletions exercises/practice/darts/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# This is an auto-generated file.
#
# Regenerating this file via `configlet sync` will:
# - Recreate every `description` key/value pair
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
# - Preserve any other key/value pair
#
# As user-added comments (using the # character) will be removed when this file
# is regenerated, comments can be added via a `comment` key.

[af9ffe10-dc13-42d8-a742-e7bdafac449d]
description = "Say Hi!"
16 changes: 16 additions & 0 deletions exercises/practice/darts/Darts.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
@echo off
setlocal enabledelayedexpansion

set "x=%~1"
set "y=%~2"
set "result="

REM Usage: set /a "value=sqrt(n):n=100"
REM echo %value%
REM '100' is the number to find the square root of '10'
set "Sqrt(N)=( x=(N)/(11*1024)+40, x=((N)/x+x)/2, x=((N)/x+x)/2, x=((N)/x+x)/2, x=((N)/x+x)/2, x=((N)/x+x)/2 )"

REM Your code goes here


echo %result%
123 changes: 123 additions & 0 deletions exercises/practice/darts/DartsTest.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
@echo off
REM ---------------------------------------------------
REM Darts Unit Testing
REM
REM sUnit Testing Framework version: 0.2
REM
REM float test cases not added because of batch scipting language limitation
REM ---------------------------------------------------

:Main
REM Initalize result variable
set "slug=Darts"

CALL :Initialize

REM --------------------
REM Test Case Start \/\/
REM Resource: https://github.com/exercism/problem-specifications/blob/main/exercises/darts/canonical-data.json
REM --------------------
set "expected=0"
set "if_success=Test passed"
set "if_failed=Test failed: Missed target."
CALL :Assert "-9" "9"

set "expected=0"
set "if_success=Test passed"
set "if_failed=Test failed: On the outer circle."
CALL :Assert "0" "10"

set "expected=5"
set "if_success=Test passed"
set "if_failed=Test failed: On the middle circle."
CALL :Assert "-5" "0"

set "expected=10"
set "if_success=Test passed"
set "if_failed=Test failed: On the inner circle."
CALL :Assert "0" "-1"

set "expected=10"
set "if_success=Test passed"
set "if_failed=Test failed: Exactly on center."
CALL :Assert "0" "0"

set "expected=5"
set "if_success=Test passed"
set "if_failed=Test failed: Asymmetric position between the inner and middle circles."
CALL :Assert "0" "-4"

set "expected=5"
set "if_success=Test passed"
set "if_failed=Test failed: Asymmetric position between the inner and middle circles."
CALL :Assert "1" "-4"

REM --------------------
REM Test Case End /\/\/\
REM --------------------

CALL :ResolveStatus
exit /b %errorlevel%
REM End of Main

REM ---------------------------------------------------
REM Assert [..Parameters(up to 9)]
REM ---------------------------------------------------
GOTO :End REM Prevents the code below from being executed
:Assert
set "stdout="

REM Run the program and capture the output then delete the file
CALL %slug%.bat %1 %2 %3 %4 %5 %6 %7 %8 %9 > stdout.bin 2>&1
set /p stdout=<stdout.bin
del stdout.bin

REM Check if the result is correct
if "%stdout%" == "%expected%" (
if defined if_success (
echo %if_success%

REM Reset the variable to avoid duplicating the message.
set "if_success="
set "if_failed="
)

REM If the result is correct, exit with code 0
set /a successCount+=1
exit /b 0
) else (
if defined if_failed (
echo %if_failed%

REM Reset the variable to avoid duplicating the message.
set "if_success="
set "if_failed="
)

REM If the result is incorrect, exit with code 1
set /a failCount+=1
exit /b 1
)
GOTO :EOF REM Go back to the line after the call to :Assert

:Initialize
REM It's for initialize, not about checking empty file
set "successCount=0"
set "failCount=0"
GOTO :EOF REM Go back to the line after the call to :CheckEmptyFile

:ResolveStatus
set "status="
if %failCount% gtr 0 (
REM status: Fail
REM message: The test failed.
exit /b 1

) else (
REM status: Pass
exit /b 0

)
GOTO :EOF REM Go back to the line after the call to :ExportResultAsJson

:End

0 comments on commit 1d3eca3

Please sign in to comment.