Exercise: 🎯


Let's code with Dart-throwing. It is a game where players throw darts to a round target called dart-board, where points can be scored by hitting specific marked areas of the board.

In this task we will consider a 3-circle dartboard that looks like

where the score of a single throw is defined as following,

Task 1.

In the first task, we consider the dartboard specified with the following parameters.

The picture of the dartboard above has been plotted with these parameters. All the circles are centered at (0, 0) which is the origin of dartboard's two dimensional Cartesian coordinate system.

Now let (x,y) denote the coordinates of a point in the dartboard's plane.

Now write a function score(x,y) that takes the coordinates x and y as input and returns the score of a dart landing on the the point (x,y).

function score(x, y)
    missing    
end

Hints. Click the line below 👇

We provide hints in case you are completely stuck.

😃 But many of you may want to solve the tasks without them and may not want the spoilers. So spoiler Alerts! Please click on the hints below only when you want to see them !

HINT 1

Click if you have no idea how to compare

HINT 2

This one is not very spoiling! Maybe you can use them maybe not
x =  a > a1 ? b : c > c1 ? d : e > e1 ? f : g

is a ternary expression equivalent to

if a > a1
    x = b
elseif c > c1
    x = d
elseif e > e1
    x = f
else
    x = g
end

However, if you just want to use if-else and NO ternary it is perfectly fine.

Testing the code

For testing whether your written function is correct or not,

using Test


@testset "Missed target" begin
    @test score(-9, 9) == 0
end

@testset "On the outer circle" begin
    @test score(0, 10) == 1
end

@testset "On the middle circle" begin
    @test score(-5, 0) == 5
end

@testset "On the inner circle" begin
    @test score(0, -1) == 10
end

@testset "Exactly on centre" begin
    @test score(0, 0) == 10
end

@testset "Near the centre" begin
    @test score(-0.1, -0.1) == 10
end

@testset "Just within the inner circle" begin
    @test score(0.7, 0.7) == 10
end

@testset "Just outside the inner circle" begin
    @test score(0.8, -0.8) == 5
end

@testset "Just within the middle circle" begin
    @test score(-3.5, 3.5) == 5
end

@testset "Just outside the middle circle" begin
    @test score(-3.6, -3.6) == 1
end

@testset "Just within the outer circle" begin
    @test score(-7.0, 7.0) == 1
end

@testset "Just outside the outer circle" begin
    @test score(7.1, -7.1) == 0
end

@testset "Asymmetric position between the inner and middle circles" begin
    @test score(0.5, -4) == 5
end

If it passes all the tests than the code should be fine, otherwise try to find out what is wrong and fix that. The testing code uses Test package which is pre-installed so you do not need to add it again.

Task 2.

If the test is successful, I guess you figured out correct formula to compute the score. In this task, we would like to increase the flexibility of our program to calculate the score for any valid values of dartboard parameters r_outer, r_middle and r_inner.

Write another method score(x,y; r_outer = 10, r_middle = 5, r_inner = 1).

Task 3. Problem of Random Thrower

Recall we had the problem of random walker? Now we will have a random thrower.

Let's consider the experiment of throwing a dart on the dartboard. The outcome of the experiment is where the dart lands on. The possible outcomes of the experiment can be any point (x,y)(x,y) on the dartboard plane.

Let's consider the first random thrower, let's call it RT1. Also let's reconsider the dartboard from Task-1 with r_outer = 10, r_middle = 5, r_inner = 1.

Note that we added a green square ([10,10]×[10,10])([-10,10]\times[-10,10]) around the dartboard. We assume when RT1 throws, it will randomly land on a point inside the green boundary and every point inside is equally likely to be the outcome and no points outside can be an outcome of the dart throwing experiment.

So how can we simulate the throws of RT1? One way to model this we could think about the xx cordinates are coming from the distribution Uniform([10,10])Uniform([-10,10]) and yy coordinate are coming from the distribution Uniform([10,10])Uniform([-10,10]). So in essence each point is an outcome of a bivariate Uniform distribution, ranging from 10-10 to 10-10 in each axis.

Let's code this.

3.1. First write a function randpoint_uniform() that returns a sample from Uniform([10,10])Uniform([-10,10]). You are not allowed the distribution package, (Hint: you only need rand() and shape and scale transformation).

3.2. then sample a single throw with

x = randpoint_uniform() 

and

y = randpoint_uniform()

and compute score using the score(x,y).

score(x,y)
# possible outpo

which should output an integer in [0,1,5,10] like before.

3.3. Now you need to generate points which are outcomes of multiple throws. Write a function random_throws(number_of_throws) that returns two arrays of length number_of_throws, one for x values and other for y values. This function should randpoint_uniform() you previously coded.

function random_throws(number_of_throws)
    missing #replace missing with your code goes here
    return xs, ys 
end

Let's look at an example of the use of the function for 3 throws

xs,ys = random_throws(3)

Output:

([7196870765878, -6.260416785392318, -9.499877323367492], 
[1.4924657650798867, -2.9694578823915574, -5.689145838695184])

which gives

xs

Output:

3-element Array{Float64,1}:
  5.557196870765878
 -6.260416785392318
 -9.499877323367492

and

 ys

Output:

3-element Array{Float64,1}:
  1.4924657650798867
 -2.9694578823915574
 -5.689145838695184

3.4. Now suppose you have your data xs and ys for number_of_throws. Now we can compute score to every points for example with loop the following way,

all_scores = zeros(number_of_throws)
for i = 1:number_of_throws
    all_scores[i] = score(xs[i], ys[i])
end

But you should be also able write an easier code using broadcast or map (. operator) with score function in the following way

For example,

score.(xs,ys)

Output:

3-element Array{Int64,1}:
 1
 1
 0

Your task is, for 100 throws

3.5. Let's do some visualization. To do this you are provided a file dartboard.jl for some helper functions for plotting. Add it to your workspace with the following code

include("dartboard.jl")

Now you can plot a dartboard by running the following code

draw_mydartboard(10 ,5, 1)

and you can also throw some darts with the following code

draw_mydartboard(10 ,5, 1)
throw_darts_on_board(xs,ys)

Then you can plot a histogram of the scores with the following code

score_histogram(all_scores)