Created
February 17, 2014 01:04
-
-
Save benneuman/9042973 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rspec' | |
def get_score(game) | |
bonus_tally = [] | |
counter = 0 | |
while counter < game.length | |
if game[counter] == 10 | |
scores_to_add = [game[counter + 1], game[counter + 2]] | |
bonus_tally += scores_to_add unless scores_to_add.any?(&:nil?) | |
counter += 1 | |
elsif game[counter] + game[counter + 1] == 10 | |
bonus_tally << game[counter + 2] | |
counter += 2 | |
else | |
counter += 2 | |
end | |
end | |
game += bonus_tally | |
game.reject(&:nil?).reduce(:+) | |
end | |
# game.each_index do |index| | |
# if index % 2 == 0 | |
# frame = game[index] + game[index + 1] unless game[index + 1].nil? | |
# added_tallies << game[index + 2] if frame == 10 | |
# added_tallies << game[index + 3] if game[index] == 10 | |
# end | |
# end | |
# puts tally | |
# puts added_tallies | |
# tally += added_tallies.reject(&:nil?).reduce(:+) unless added_tallies.empty? | |
# tally | |
# end | |
describe "Bowling game" do | |
context "with all gutters" do | |
it "scores zero" do | |
game = [0] * 20 | |
get_score(game).should == 0 | |
end | |
end | |
context "with one pin knocked down on every roll" do | |
it "scores twenty" do | |
game = [1] * 20 | |
get_score(game).should == 20 | |
end | |
end | |
context "with a spare in the first frame followed by three pins followed by all misses" do | |
it "scores sixteen" do | |
game = [8, 2, 3] + [0] * 17 | |
get_score(game).should == 16 | |
end | |
end | |
context "with a strike in the first frame followed by three pins followed by four pins followed by all misses" do | |
it "scores 24" do | |
game = [10, 3, 4] + [0] * 16 | |
get_score(game).should == 24 | |
end | |
end | |
context "a perfect game (12 strikes)" do | |
it "scores 300" do | |
game = [10] * 12 | |
get_score(game).should == 300 | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment