🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of glennj

glennj's solution

to Darts in the Tcl Track

Published at Mar 03 2020 · 0 comments
Instructions
Test suite
Solution

Note:

This exercise has changed since this solution was written.

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

Darts is a game where players throw darts to a target.

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

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

Running the tests

To run the test suite, execute one of the following commands:

tclsh darts.test            # Will stop testing after the first failure.
RUN_ALL=1 tclsh darts.test  # Will run all tests and report all failures.

Feedback, Issues, Pull Requests

The exercism/tcl repository on GitHub is the home for all of the Tcl exercises on Exercism.

If you have feedback about an exercise, or want to help implementing a new one, head over there and create an issue. We'll do our best to help you!

Source

Inspired by an exercise created by a professor Della Paolera in Argentina

darts.test

#!/usr/bin/env tclsh
set version 2.2.0
package require tcltest
namespace import ::tcltest::*
source "darts.tcl"

proc fail_fast {} {
    return [expr {
        ![info exists ::env(RUN_ALL)]
        || [string is boolean -strict $::env(RUN_ALL)]
        && !$::env(RUN_ALL)
    }]
}

proc failed {} {
    return [expr {$::tcltest::numTests(Failed) > 0}]
}

if {[fail_fast]} {
    proc test args {
        if {[failed]} {::tcltest::configure -skip *}
        uplevel [list ::tcltest::test {*}$args]
    }
}

proc cleanupTests {} {
    set failed [failed]
    uplevel 1 ::tcltest::cleanupTests
    if {$failed} {exit 1}
}

if {$::argv0 eq [info script]} {

    set cases {
        darts-1 "Missed target" -9 9 0
        darts-2 "On the outer circle" 0 10 1
        darts-3 "On the middle circle" -5 0 5
        darts-4 "On the inner circle" 0 -1 10
        darts-5 "Exactly on centre" 0 0 10
        darts-6 "Near the centre" -0.1 -0.1 10
        darts-7 "Just within the inner circle" 0.7 0.7 10
        darts-8 "Just outside the inner circle" 0.8 -0.8 5
        darts-9 "Just within the middle circle" -3.5 3.5 5
        darts-10 "Just outside the middle circle" -3.6 -3.6 1
        darts-11 "Just within the outer circle" -7.0 7.0 1
        darts-12 "Just outside the outer circle" 7.1 -7.1 0
        darts-13 "Asymmetric position between the inner and middle circles" 0.5 -4 5
    }

    foreach {name description x y value} $cases {
        test $name $description -body {
            score $x $y
        } -returnCodes ok -result $value
    }

    cleanupTests
}
proc score {x y} {
    set distance [expr {hypot($x, $y)}]
    if {$distance <=  1} {return 10}
    if {$distance <=  5} {return  5}
    if {$distance <= 10} {return  1}
    return 0
}

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?