-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbowling_game.rb
More file actions
55 lines (41 loc) · 813 Bytes
/
Copy pathbowling_game.rb
File metadata and controls
55 lines (41 loc) · 813 Bytes
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
class BowlingGame
NUMBER_OF_FRAMES = 10
NUMBER_OF_PINS = 10
def initialize
@rolls = []
end
def roll(pins)
@rolls << pins
end
def score
NUMBER_OF_FRAMES.times.map{ score_frame }.reduce(:+)
end
private
def score_frame
score_strike || score_spare || score_normal
end
def is_strike?
next_roll == NUMBER_OF_PINS
end
def is_spare?
next_two_rolls == NUMBER_OF_PINS
end
def score_strike
is_strike? && (score_roll + next_two_rolls)
end
def score_spare
is_spare? && (score_normal + next_roll)
end
def score_normal
score_roll + score_roll
end
def score_roll
@rolls.shift
end
def next_roll
@rolls[0]
end
def next_two_rolls
@rolls[0..1].reduce(:+)
end
end