-
Notifications
You must be signed in to change notification settings - Fork 0
/
bowling_game.rb
56 lines (43 loc) · 896 Bytes
/
bowling_game.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
class BowlingGame
attr_reader :first_in_frame
def initialize
@first_in_frame = 0
end
def pins(rolls)
@rolls = rolls
end
def score
frame = 0
score = 0
while frame < 10
if spare?
score += 10 + bonus_for_spare
@first_in_frame += 2
elsif strike?
score += 10 + bonus_for_strike
@first_in_frame += 1
else
score += standard_frame_score
@first_in_frame += 2
end
frame += 1
end
score
end
private
def spare?
@rolls[first_in_frame] + @rolls[first_in_frame + 1] == 10
end
def strike?
@rolls[first_in_frame] == 10
end
def standard_frame_score
@rolls[first_in_frame] + @rolls[first_in_frame + 1]
end
def bonus_for_spare
@rolls[first_in_frame + 2]
end
def bonus_for_strike
@rolls[first_in_frame + 1] + @rolls[first_in_frame + 2]
end
end