-
Notifications
You must be signed in to change notification settings - Fork 0
/
dolphin_race.rb
288 lines (221 loc) · 7.9 KB
/
dolphin_race.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
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#Dolphin/Penguin racing game from third week of learning ruby. Designed to be ran in terminal.
class Dolphin
attr_accessor :name, :gender, :age
def initialize
@distance_to_finish = 60
@opponent_distance_to_finish = 60
puts "What do you want your dolphin named?"
name = gets.chomp
puts "Is it male or female?"
gender = gets.chomp
x = rand(30)
puts "It's age is #{x}"
if x < 10
puts "That is quite young for a dolphin"
elsif x < 20
puts "That is a middle aged dolphin"
else
puts "That is an old dolphin"
end
start
end
def start
puts "Here are the rules:"
puts "You can either jump, swim, or surf your way towards the finish. If you choose to *jump* you will move anywhere from 5 to 15 yards forward. If you choose to *swim* you will move anywhere from 8 to 12 yards forward. If you choose to *surf* you will move anywhere from 1 to 20 yards forward. Surfing is the riskiest. Swimming is the safest. Jumping is inbetween.
"
puts "Are you ready to start the race?"
ans = gets.chomp
if ans.downcase == "yes"
puts "3...2...1...GO!"
choice
elsif ans.downcase == "no"
puts "okay :("
exit
else
puts "Please say yes"
start
end
end
def choice
puts "Do you want it to jump, swim or surf?"
command = gets.chomp
if command.downcase == "jump"
jump
elsif command.downcase == "swim"
swim
elsif command.downcase == "surf"
surf
else
puts "Please type jump, swim, or surf"
choice
end
end
def jump
puts "***************************************************"
jump_array = ["Your dolphin jumped high in the air and did a front-flip", "Your dolphin flew above the water and did a back-flip.", "Your dolphin got out of the water and started jumping on a trampoline towards the finish."]
puts jump_array.sample
@distance_to_finish -= rand(5..15)
@opponent_distance_to_finish -=rand(5..15)
if @distance_to_finish <= 0
finish_race_win
elsif @opponent_distance_to_finish <= 0
finish_race_lose
end
puts "Only #{@distance_to_finish} yards to the finish line left. Your penguin opponent has #{@opponent_distance_to_finish} yards left."
choice
end
def swim
puts "***************************************************"
swim_array = ["Your dolphin swam quickly ahead towards the finish.", "Your dolphin pumped his tail and accellerated forwards."]
puts swim_array.sample
@distance_to_finish -= rand(8..12)
@opponent_distance_to_finish -= rand(5..15)
if @distance_to_finish <= 0
finish_race_win
elsif @opponent_distance_to_finish <= 0
finish_race_lose
end
puts "Only #{@distance_to_finish} yards to the finish line left. Your penguin opponent has #{@opponent_distance_to_finish} yards left."
choice
end
def surf
puts "***************************************************"
surf_array = ["Your dolphin caught a wave and was propelled forwards.", "Your dolphin hopped on a surfboard and moved quickly ahead."]
puts surf_array.sample
@distance_to_finish -= rand(1..20)
@opponent_distance_to_finish -= rand(5..15)
if @distance_to_finish <= 0
finish_race_win
elsif @opponent_distance_to_finish <= 0
finish_race_lose
end
puts "Only #{@distance_to_finish} yards to the finish line left. Your penguin opponent has #{@opponent_distance_to_finish} yards left."
choice
end
def finish_race_win
puts "and... The race is over. YOU WON!!! Dolphins for life!!"
exit
end
def finish_race_lose
puts "and... The race is over. You lost to a penguin! HAHAH!"
exit
end
end
#########################################################################
class Penguin
attr_accessor :name, :gender, :age
def initialize
@distance_to_finish = 100
@opponent_distance_to_finish = 100
puts "What do you want your penguin named?"
name = gets.chomp
puts "Is it male or female?"
gender = gets.chomp
x = rand(30)
puts "It's age is #{x}"
if x < 10
puts "That is quite young for a penguin"
elsif x < 20
puts "That is a middle aged penguin"
else
puts "That is an old penguin"
end
start
end
def start
puts "Here are the rules:"
puts "You can either waddle, dive, or slide your way towards the finish. If you choose to *dive* you will move anywhere from 5 to 15 yards forward. If you choose to *waddle* you will move anywhere from 8 to 12 yards forward. If you choose to *slide* you will move anywhere from 1 to 20 yards forward. Sliding is the riskiest. Waddling is the safest. Diving is inbetween.
"
puts "Are you ready to start the race?"
ans = gets.chomp
if ans.downcase == "yes"
puts "3...2...1...GO!"
choice
elsif ans.downcase == "no"
puts "okay :("
exit
else
puts "Please say yes"
start
end
end
def choice
puts "Do you want it to waddle, dive or slide?"
command = gets.chomp
if command.downcase == "waddle"
waddle
elsif command.downcase == "dive"
dive
elsif command.downcase == "slide"
slide
else
puts "Please type waddle, dive or slide..."
choice
end
end
def waddle
puts "***************************************************"
waddle_array = ["Your penguin awkwardly waddled forwards in a quick burst.", "Your penguin hopped on a surfboard and moved quickly ahead."]
puts waddle_array.sample
@distance_to_finish -= rand(8..12)
@opponent_distance_to_finish -= rand(5..15)
if @distance_to_finish <= 0
finish_race_win
elsif @opponent_distance_to_finish <= 0
finish_race_lose
end
puts "Only #{@distance_to_finish} yards to the finish line left. Your dolphin opponent has #{@opponent_distance_to_finish} yards left."
choice
end
def dive
puts "***************************************************"
dive_array = ["Your penguin climbed up a nearby iceburg and dove deeply forwards.", "In a risky move your penguin did an awesome cannonball and quickly approached the finish line."]
puts dive_array.sample
@distance_to_finish -= rand(5..15)
@opponent_distance_to_finish -= rand(5..15)
if @distance_to_finish <= 0
finish_race_win
elsif @opponent_distance_to_finish <= 0
finish_race_lose
end
puts "Only #{@distance_to_finish} yards to the finish line left. Your dolphin opponent has #{@opponent_distance_to_finish} yards left."
choice
end
def slide
puts "***************************************************"
slide_array = ["Your penguin hopped up on to the ice and slid towards the finish.", "Your penguin slid quickly ahead."]
puts slide_array.sample
@distance_to_finish -= rand(1..20)
@opponent_distance_to_finish -= rand(5..15)
if @distance_to_finish <= 0
finish_race_win
elsif @opponent_distance_to_finish <= 0
finish_race_lose
end
puts "Only #{@distance_to_finish} yards to the finish line left. Your dolphin opponent has #{@opponent_distance_to_finish} yards left."
choice
end
def finish_race_win
puts "and... The race is over. YOU WON!!! Penguins for life!!"
exit
end
def finish_race_lose
puts "and... The race is over. You lost to a dolphin! HAHAH!"
exit
end
end
##################Start_of_Program#####################################
puts "***************************************************"
puts "It's a race between dolphins and penguins to decide which is better. Do you want to be a dolphin or a penguin?"
def begin_race
answer = gets.chomp
if answer.downcase == "dolphin"
Dolphin.new
elsif answer.downcase == "penguin"
Penguin.new
else
puts "Please type either dolphin or penguin..."
begin_race
end
end
begin_race