Ad

Given a remaining number of points to score on a darts board find the next three optimal targets. For how the individual areas are scored see: https://en.wikipedia.org/wiki/Darts#/media/File:Dartboard_heatmap.svg Objective is to reduce a fixed score, commonly 301 or 501, to zero ("checking out") with the final dart landing in either the bullseye or a double segment to win. Not all three darts need to be thrown on the final turn; the game can be finished on any of the three darts. A throw that reduces a player's score below zero, to exactly one, or to zero but not ending with a double or bullseye is known as "going bust". The player's score is reset to its value at the start of that turn, and any remaining throws in the turn are forfeited. When more than one combination for a given score is valid then all of them are accepted.

In case there is more than one combination to check out, all of them should be returned.

Bonus: Some scores can be reached in with more than one combination, for these cases consider to pick the biggest targets, assuming they are easier to hit.

Note: Some scores have more than one combination, in that case combinations with the least amount of throws are to be chosen.

Code
Diff
  • def best_moves(score)
      # return the three best moves to reach 0 from the given score
      if (score.even? && score <= 40) || score == 50
        return [score]
      end
      if score >= 182
        return [60, 60, 60]
      end
      if score == 170
        return [60,60,50]
      end
      if score == 21
      end  
    end
    • def best_moves(score)
    • # return the three best moves to reach 0 from the given score
    • if (score.is_even && score <= 40) || score == 50 do
    • if (score.even? && score <= 40) || score == 50
    • return [score]
    • end
    • if score >= 182 do
    • if score >= 182
    • return [60, 60, 60]
    • end
    • if score == 21 do
    • if score == 170
    • return [60,60,50]
    • end
    • if score == 21
    • end
    • end

Given a remaining number of points to score on a darts board find the next three optimal targets. For how the individual areas are scored see: https://en.wikipedia.org/wiki/Darts#/media/File:Dartboard_heatmap.svg
Objective is to reduce a fixed score, commonly 301 or 501, to zero ("checking out") with the final dart landing in either the bullseye or a double segment to win.
Not all three darts need to be thrown on the final turn; the game can be finished on any of the three darts. A throw that reduces a player's score below zero, to exactly one, or to zero but not ending with a double or bullseye is known as "going bust".
The player's score is reset to its value at the start of that turn, and any remaining throws in the turn are forfeited.
When more than one combination for a given score is valid then all of them are accepted.

Bonus: Some scores can be reached in with more than one combination, for these cases consider to pick the biggest targets, assuming they are easier to hit.
Bonus: Some scores have more than one combination, in that case combinations with the least amount of throws are to be chosen.

def best_moves(score)
  # return the three best moves to reach 0 from the given score
end