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

Add exercise: darts #217

Merged
merged 3 commits into from Jul 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions config.json
Expand Up @@ -413,6 +413,14 @@
"practices": [],
"prerequisites": [],
"difficulty": 1
},
{
"slug": "darts",
"name": "Darts",
"uuid": "4e7b84c0-5f6c-4bf3-b991-d7eae18ca05f",
"practices": [],
"prerequisites": [],
"difficulty": 1
}
]
}
Expand Down
23 changes: 23 additions & 0 deletions exercises/practice/darts/.docs/instructions.md
@@ -0,0 +1,23 @@
# Instructions

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:

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

[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
18 changes: 18 additions & 0 deletions exercises/practice/darts/.meta/config.json
@@ -0,0 +1,18 @@
{
"authors": [
"simisc"
],
"files": {
"solution": [
"darts.f90"
],
"test": [
"darts_test.f90"
],
"example": [
".meta/example.f90"
]
},
"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"
}
24 changes: 24 additions & 0 deletions exercises/practice/darts/.meta/example.f90
@@ -0,0 +1,24 @@
module darts
implicit none

contains

function score(x, y) result(points)
real, intent(in):: x, y
integer :: points
real :: dist

dist = hypot(x, y)

if (dist <= 1) then
points = 10
else if (dist <= 5) then
points = 5
else if (dist <= 10) then
points = 1
else
points = 0
end if
end function score

end module darts
49 changes: 49 additions & 0 deletions exercises/practice/darts/.meta/tests.toml
@@ -0,0 +1,49 @@
# 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.

[9033f731-0a3a-4d9c-b1c0-34a1c8362afb]
description = "Missed target"

[4c9f6ff4-c489-45fd-be8a-1fcb08b4d0ba]
description = "On the outer circle"

[14378687-ee58-4c9b-a323-b089d5274be8]
description = "On the middle circle"

[849e2e63-85bd-4fed-bc3b-781ae962e2c9]
description = "On the inner circle"

[1c5ffd9f-ea66-462f-9f06-a1303de5a226]
description = "Exactly on center"

[b65abce3-a679-4550-8115-4b74bda06088]
description = "Near the center"

[66c29c1d-44f5-40cf-9927-e09a1305b399]
description = "Just within the inner circle"

[d1012f63-c97c-4394-b944-7beb3d0b141a]
description = "Just outside the inner circle"

[ab2b5666-b0b4-49c3-9b27-205e790ed945]
description = "Just within the middle circle"

[70f1424e-d690-4860-8caf-9740a52c0161]
description = "Just outside the middle circle"

[a7dbf8db-419c-4712-8a7f-67602b69b293]
description = "Just within the outer circle"

[e0f39315-9f9a-4546-96e4-a9475b885aa7]
description = "Just outside the outer circle"

[045d7d18-d863-4229-818e-b50828c75d19]
description = "Asymmetric position between the inner and middle circles"
71 changes: 71 additions & 0 deletions exercises/practice/darts/CMakeLists.txt
@@ -0,0 +1,71 @@
# Get the exercise name from the current directory
get_filename_component(exercise ${CMAKE_CURRENT_SOURCE_DIR} NAME)

# Basic CMake project
cmake_minimum_required(VERSION 3.0.0)

# Name the project after the exercise
project(${exercise} Fortran)

# set debug build default
set(CMAKE_BUILD_TYPE Debug)

# Get a source filename from the exercise name by replacing -'s with _'s
string(REPLACE "-" "_" file ${exercise})

# Activate Fortran compiler warnings
if(CMAKE_Fortran_COMPILER_ID MATCHES "Intel") # Intel fortran
if(WIN32)
set (CCMAKE_Fortran_FLAG ${CCMAKE_Fortran_FLAGS} "/warn:all")
else()
set (CMAKE_Fortran_FLAGS ${CCMAKE_Fortran_FLAGS} "-warn all")
endif()
endif()
if(CMAKE_Fortran_COMPILER_ID MATCHES "GNU") # GFrotran
set (CMAKE_Fortran_FLAGS ${CCMAKE_Fortran_FLAGS} "-std=f2008 -W -Wall -Wextra -pedantic -fbacktrace")
endif()

# Configure to run all the tests?
if(${EXERCISM_RUN_ALL_TESTS})
add_definitions(-DEXERCISM_RUN_ALL_TESTS)
set(exercise_f90 ${file}_build_all.f90)
else()
# if building in exercise folder add testlib
set(testlib_path ${CMAKE_CURRENT_SOURCE_DIR}/testlib)
if (NOT EXISTS ${testlib_path})
if (EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/../../../testlib) # in git repo locally?
file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/../../../testlib DESTINATION ${CMAKE_CURRENT_SOURCE_DIR} PATTERN *)
else() # get from git with http
message("Downloading testlib from https://raw.githubusercontent.com/exercism/fortran/master")
file(MAKE_DIRECTORY ${testlib_path} )
file(DOWNLOAD https://raw.githubusercontent.com/exercism/fortran/master/testlib/TesterMain.f90 ${testlib_path}/TesterMain.f90 SHOW_PROGRESS)
file(DOWNLOAD https://raw.githubusercontent.com/exercism/fortran/master/testlib/CMakeLists.txt ${testlib_path}/CMakeLists.txt SHOW_PROGRESS)
endif()
endif()
# add lib to build
add_subdirectory(testlib)
include_directories(testlib ${CMAKE_CURRENT_BINARY_DIR}/testlib)
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${file}.f90)
set(exercise_f90 ${file}.f90)
endif()
endif()

# Get test_files if exercise needs it
file(GLOB test_files test_files/*.*)
foreach(test_file ${test_files})
message("Copying ${test_file} to binary directory for example ${file}")
configure_file(${test_file} . COPYONLY)
endforeach()

# Build executable from sources and headers
add_executable(${exercise} ${exercise_f90} ${file}_test.f90 )

target_link_libraries(${exercise} TesterMain)

include(CTest)

add_test (${exercise}_test ${exercise} )

# Run the tests on every build
#add_custom_target(test_${exercise} ALL DEPENDS ${exercise} COMMAND ${exercise})

13 changes: 13 additions & 0 deletions exercises/practice/darts/darts.f90
@@ -0,0 +1,13 @@
module darts
implicit none

contains

function score(x, y) result(points)
real, intent(in):: x, y
integer :: points

points = int(x + y) ! Replace this line with your implementation
end function score

end module darts
49 changes: 49 additions & 0 deletions exercises/practice/darts/darts_test.f90
@@ -0,0 +1,49 @@
! The tests were created from https://github.com/exercism/problem-specifications/blob/main/exercises/darts/canonical-data.json

program darts_test_main
use TesterMain
use darts
implicit none

! Test 1: Missed target
call assert_equal(0, score(-9.0, 9.0), "Missed target")

! Test 2: On the outer circle
call assert_equal(1, score(0.0, 10.0), "On the outer circle")

! Test 3: On the middle circle
call assert_equal(5, score(-5.0, 0.0), "On the middle circle")

! Test 4: On the inner circle
call assert_equal(10, score(0.0, -1.0), "On the inner circle")

! Test 5: Exactly on center
call assert_equal(10, score(0.0, 0.0), "Exactly on center")

! Test 6: Near the center
call assert_equal(10, score(-0.1, -0.1), "Near the center")

! Test 7: Just within the inner circle
call assert_equal(10, score(0.7, 0.7), "Just within the inner circle")

! Test 8: Just outside the inner circle
call assert_equal(5, score(0.8, -0.8), "Just outside the inner circle")

! Test 9: Just within the middle circle
call assert_equal(5, score(-3.5, 3.5), "Just within the middle circle")

! Test 10: Just outside the middle circle
call assert_equal(1, score(-3.6, -3.6), "Just outside the middle circle")

! Test 11: Just within the outer circle
call assert_equal(1, score(-7.0, 7.0), "Just within the outer circle")

! Test 12: Just outside the outer circle
call assert_equal(0, score(7.1, -7.1), "Just outside the outer circle")

! Test 13: Asymmetric position between the inner and middle circles
call assert_equal(5, score(0.5, -4.0), "Asymmetric position between the inner and middle circles")

call test_report()

end program