Darts game

Objective: Calculate the points scored in Darts game.

Darts is a game where players throw darts at a board like the one in the image. For this exercise we will define a set as the set of 10 throws per player. A game will be 3 sets per player.

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

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

Tasks

Given the following type:

type DartThrow { x: number y: number }
  1. Write a function throwDart(range: number): DartThrow to produce a random throw within a range.
  2. Use it to generate the data of a singe player set of 10 throws with an 80% chance to hit the board.
  3. Calculate the total score of the player.

Bonus

  1. Write a function throwDartWithChance(insideBoardChance: number): DartThrow that can be used to generate a throw with a particular chance to hit the board.