Skip to content

Instantly share code, notes, and snippets.

@benneuman
Created March 14, 2014 16:15
Show Gist options
  • Save benneuman/9550945 to your computer and use it in GitHub Desktop.
Save benneuman/9550945 to your computer and use it in GitHub Desktop.
class BowlingGame
attr_accessor :scorecard
def score
@scorecard.size.times.map { |i| frame_score(@scorecard[i, 3]) }.reduce(:+)
end
def frame_score(frames)
if strike_or_spare?(frames.first)
frames.flatten.first(3).reduce(:+)
else
frames.first.reduce(:+)
end
end
def strike_or_spare?(frame)
frame.reduce(:+) == 10
end
end
describe BowlingGame do
let(:game) {BowlingGame.new}
context "#score" do
it "is 0 for game with all gutters" do
game.scorecard = [[0, 0]] * 10
expect(game.score).to eq 0
end
it "is 20 for game with all ones" do
game.scorecard = [[1, 1]] * 10
expect(game.score).to eq 20
end
it "is 16 for game with spare followed by 3" do
game.scorecard = [[3, 7], [3, 0]] + [[0,0]] * 8
expect(game.score).to eq 16
end
it "is 24 for game with strike followed by 3 and 4" do
game.scorecard = [[10], [3,4]] + [[0,0]] * 8
expect(game.score).to eq 24
end
it "is 300 for perfect game" do
game.scorecard = [[10]] * 9 + [[10, 10, 10]]
expect(game.score).to eq 300
end
it "handles one strike" do
game.scorecard = [[10]]
expect(game.score).to eq 10
end
it "handles two strikes" do
game.scorecard = [[10], [10]]
expect(game.score).to eq 30
end
it "handles a random smattering" do
game.scorecard = [[10], [10], [3, 7], [4, 5], [3, 5], [4]]
expect(game.score).to eq 78
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment