-
Notifications
You must be signed in to change notification settings - Fork 492
Expand file tree
/
Copy pathframe.rb
More file actions
68 lines (58 loc) · 1.2 KB
/
frame.rb
File metadata and controls
68 lines (58 loc) · 1.2 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
class Frame
attr_accessor :rolls, :bonus_points
def initialize(io=Kernel)
@io = io
@rolls = []
@bonus_points = 0
end
def play_regular_frame
roll_first_ball
if @rolls.first == 10
print_strike_message
return
end
roll_second_ball
print_spare_message if @rolls.inject(:+) == 10
end
def play_last_frame
roll_first_ball
if @rolls.first == 10
print_strike_message
roll_bonus_ball
roll_second_bonus_ball
return
end
roll_second_ball
if @rolls.inject(:+) == 10
print_spare_message
roll_bonus_ball
end
end
private
def roll_first_ball
@io.puts "Roll first ball:"
roll = @io.gets.chomp.to_i
@rolls << roll
end
def print_strike_message
@io.puts "Strike!"
end
def roll_second_ball
@io.puts "Roll second ball:"
roll = @io.gets.chomp.to_i
@rolls << roll
end
def print_spare_message
@io.puts "Spare!"
end
def roll_bonus_ball
@io.puts "Roll bonus ball:"
bonus_roll = @io.gets.chomp.to_i
@rolls << bonus_roll
end
def roll_second_bonus_ball
@io.puts "Roll second bonus ball:"
bonus_roll = @io.gets.chomp.to_i
@rolls << bonus_roll
end
end