forked from makersacademy/bowling-challenge-ruby
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.rb
More file actions
236 lines (210 loc) · 5.3 KB
/
game.rb
File metadata and controls
236 lines (210 loc) · 5.3 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
class Game
attr_reader :score, :frame, :game_rolls
attr_accessor :pins, :frame_rolls, :game_rolls
def initialize
@score = 0
@frame = 1
@pins = 10
@frame_rolls = []
@game_rolls = []
end
def play
while @frame < 10 do
play_frame
end
play_frame_10
@game_rolls # temporary placement until final scores method is written
# final_score
end
def play_frame
play_roll_1
play_roll_2 if @roll_1 < 10
reset_frame
current_score
end
def play_frame_10
play_roll_1_10
play_roll_2_decider
play_roll_3_decider
end
def play_roll_1
puts 'First roll: How many pins did you knock down?'
@roll_1 = gets.chomp.to_i
while @roll_1 > 10 do
puts 'Invalid entry'
puts 'First roll: How many pins did you knock down?'
@roll_1 = gets.chomp.to_i
end
if @roll_1 == 10
puts 'STRIKE!'
puts 'Your score for this frame will be determined by the next frame'
@frame_rolls << @roll_1
@frame_rolls << 0
else
@pins -= @roll_1
puts "#{@pins} pins remaining!"
@frame_rolls << @roll_1
end
end
def play_roll_2
puts 'Second roll: How many pins did you knock down?'
@roll_2 = gets.chomp.to_i
while @roll_2 > @pins do
puts 'Invalid entry'
puts 'Second roll: How many pins did you knock down?'
@roll_2 = gets.chomp.to_i
end
if @roll_2 == @pins
puts 'SPARE!'
puts 'Your score for this frame will be determined by the next frame'
@frame_rolls << @roll_2
else
puts "You hit #{@roll_2} pins!"
@frame_rolls << @roll_2
end
end
def play_roll_1_10
puts 'First roll: How many pins did you knock down?'
@roll_1_10 = gets.chomp.to_i
while @roll_1_10 > 10 do
puts 'Invalid entry'
puts 'First roll: How many pins did you knock down?'
@roll_1_10 = gets.chomp.to_i
end
if @roll_1_10 == 10
puts 'STRIKE!'
@frame_rolls << @roll_1_10
@pins = 10
else
@pins -= @roll_1_10
puts "#{@pins} pins remaining!"
@frame_rolls << @roll_1_10
end
end
def play_roll_2_10
puts 'Second roll: How many pins did you knock down?'
@roll_2_10 = gets.chomp.to_i
while @roll_2_10 > 10 do
puts 'Invalid entry'
puts 'Second roll: How many pins did you knock down?'
@roll_2_10 = gets.chomp.to_i
end
if @roll_2_10 == 10
puts 'STRIKE!'
@pins = 10
@frame_rolls << @roll_2_10
else
@pins -= @roll_2_10
puts "#{@pins} pins remaining!"
@frame_rolls << @roll_2_10
end
end
def play_roll_2_10_a
puts 'Second roll: How many pins did you knock down?'
@roll_2_10_a = gets.chomp.to_i
while @roll_2_10_a > @pins do
puts 'Invalid entry'
puts 'Second roll: How many pins did you knock down?'
@roll_2_10_a = gets.chomp.to_i
end
if @roll_2_10_a == @pins
puts 'SPARE!'
@pins = 10
@frame_rolls << @roll_2_10_a
else
puts "You hit #{@roll_2_10_a} pins!"
@frame_rolls << @roll_2_10_a
@frame_rolls << 0
end
end
def play_roll_3_10
puts 'Bonus roll: How many pins did you knock down?'
@roll_3_10 = gets.chomp.to_i
while @roll_3_10 > 10 do
puts 'Invalid entry'
puts 'Bonus roll: How many pins did you knock down?'
@roll_3_10 = gets.chomp.to_i
end
if @roll_3_10 == 10
puts 'STRIKE!'
@pins -= @roll_3_10
@frame_rolls << @roll_3_10
else
@pins -= @roll_3_10
@frame_rolls << @roll_3_10
end
end
def play_roll_3_10_a
puts 'Bonus roll: How many pins did you knock down?'
@roll_3_10_a = gets.chomp.to_i
while @roll_3_10_a > @pins do
puts 'Invalid entry'
puts 'Bonus roll: How many pins did you knock down?'
@roll_3_10_a = gets.chomp.to_i
end
if @roll_3_10_a == @pins
puts 'SPARE!'
@pins = 0
@frame_rolls << @roll_3_10_a
else
@frame_rolls << @roll_3_10_a
end
end
def current_score
if @game_rolls[-1][0] + @game_rolls[-1][1] == 10
return 'score depends on next frame'
else
frames_scores
end
end
def frames_scores
score = 0
for index in 0..(@game_rolls.length - 1)
if @game_rolls[index][0] == 10
score += (10 + @game_rolls[index + 1].sum)
elsif @game_rolls[index].sum == 10
score += (10 + @game_rolls[index + 1][0])
else
score += @game_rolls[index].sum
end
end
score
end
def final_score
game_score = 0
for index in 0..8
p 'in the index 0 to 8 area'
if @game_rolls[index][0] == 10
game_score += (10 + @game_rolls[index + 1].sum)
elsif @game_rolls[index].sum == 10
game_score += (10 + @game_rolls[index + 1][0])
else
game_score += @game_rolls[index].sum
end
end
p 'about to add in score for final frame'
game_score += @game_rolls[9].sum
end
def reset_frame
@pins = 10
@frame += 1
@game_rolls << @frame_rolls
@frame_rolls = []
@game_rolls
end
def play_roll_2_decider
if @roll_1_10 == 10
play_roll_2_10
else
play_roll_2_10_a
end
end
def play_roll_3_decider
if @roll_1_10 + @roll_2_10 = 20 || @roll_1_10 + @roll_2_10_a = 10
play_roll_3_10
elsif @roll_1_10 + @roll_2_10 > 10
play_roll_3_10_a
else
end
end
end