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
Test Cases
Diff
  • # From Ruby 3.0, RSpec is used under the hood.
    # See https://rspec.info/
    # Defaults to the global `describe` for backwards compatibility, but `RSpec.desribe` works as well.
    describe "Example" do
      it "knows how to proceed quickly" do
        expect(best_moves(301)).to eq([60,60,60])
      end
      it "should return the optimal moves" do
        expect(best_moves(50)).to eq([50])
        
        expect(best_moves(170)).to eq([60,60,50])
        expect(best_moves(141)).to eq([60,57,24])
        expect(best_moves(141).last).to be_even
        expect(best_moves(141).last =< 50).to be_true
        # expect(best_moves(5)).to needs to be either [1,4] or [1,2,2]
      end
    end
    
    • # From Ruby 3.0, RSpec is used under the hood.
    • # See https://rspec.info/
    • # Defaults to the global `describe` for backwards compatibility, but `RSpec.desribe` works as well.
    • describe "Example" do
    • it "should return the optimal moves" do
    • it "knows how to proceed quickly" do
    • expect(best_moves(301)).to eq([60,60,60])
    • end
    • it "should return the optimal moves" do
    • expect(best_moves(50)).to eq([50])
    • expect(best_moves(170)).to eq([60,60,50])
    • expect(best_moves(141)).to eq([60,57,24])
    • expect(best_moves(141).last).to be_even
    • expect(best_moves(141).last =< 50).to be_true
    • # expect(best_moves(5)).to needs to be either [1,4] or [1,2,2]
    • end
    • it "" do
    • 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
# From Ruby 3.0, RSpec is used under the hood.
# See https://rspec.info/
# Defaults to the global `describe` for backwards compatibility, but `RSpec.desribe` works as well.
describe "Example" do
  it "should return the optimal moves" do
    expect(best_moves(301)).to eq([60,60,60])
    expect(best_moves(170)).to eq([60,60,50])
    expect(best_moves(141)).to eq([60,57,24])
    expect(best_moves(141).last.even?).to be_true
    # expect(best_moves(5)).to needs to be either [1,4] or [1,2,2]
    # The following is still supported, but new tests should now use them.
    # Test.assert_equals(add(1, 1), 2)
  end
end