forked from Ada-C9/Word-Guess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathword_guess_game.rb
308 lines (275 loc) · 13.5 KB
/
word_guess_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
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
# We color the program in certain messages and use features like blink and bold in them also.
require 'colorize'
puts "WELCOME TO DISNEY WORD GUESS"
puts "Words Theme: Walt Disney".colorize(:blue)
# one class contain different methods for the game
class WordGuess
def initialize
@words_low = %w[minnie mickey donald daisy pluto goofy alice olaf anna ariel]
@words_medium = %w[tigger cinderella bambi pocahontas eeyor rabbit mulan belle dumbo]
@words_high = %w[esmeralda quasimodo anastasia mowgli anastasia drizella kocoum maleficent]
@letters_guessed = []
@guesses_counter = 0
selection_level
@word = @word_array.sample
@letters_correct = Array.new(@word.length, "_" )
ascii_image
letters_input
end
# This method will select the level of the game and according to the level, the program
# will select an arrays of words.
# The user will have 4 trials to enter a level. We did this to have an end of the program
# in case the user keeps entering the wrong level input.
def selection_level
levels = %w[low medium high]
puts "Please enter a level: low, medium or high?"
level = nil
until level == "low" || level == "medium" || level == "high"
level = gets.chomp.downcase
if level == "low"
@word_array = @words_low
elsif level == "medium"
@word_array = @words_medium
elsif level == "high"
@word_array = @words_high
elsif level != "low" && level != "hard" && level != "medium"
puts "Invalid level. Please enter again"
level = gets.chomp.downcase
end
attempt = 0
while !levels.include?(level)
if attempt < 3
attempt += 1
puts "Please enter a valid level. You have #{(-attempt + 3) + 1} more attempts.\n"
level = gets.chomp
else
puts "Too many attempts..start the program again"
exit
end
end
end
end
# This method hanbles inappropriate user input.
# This method validates that the input of letter from the user is a letter and not any other character.
# We did this to don't count inputs as wrong guesses if the user inputs numbers, symbols etc.
def validate_input_letter(input)
until !input.match(/[^a-z]/)
puts "\nPlease return a valid letters from A to Z.\n"
input = gets.chomp.downcase
end
end
# In this method, the user inputs a letter. The method can validate input is a letter,
# and also can tell if the letter has been already used.
def letters_input
@guesses_counter = 0
while @guesses_counter < 5
puts "Guess a letter!"
puts "Word: #{@letters_correct.join("")}"
letter = gets.chomp
validate_input_letter(letter)
if letter.length > 1
if letter == @word
you_win
exit
end
end
#This loop helps the user to don't be penalized for guessing the same letter more than once.
while @letters_guessed.include? letter do
puts "You have already tried this letter, try another one: "
letter = gets.chomp
validate_input_letter(letter)
end
@letters_guessed << letter
if @word.include? letter
@word.split(//).each_index do |i|
if @word[i] == letter
@letters_correct[i] = letter
end
end
ascii_image
else
@guesses_counter += 1
puts "Keep trying!!".colorize(:orange).blink
ascii_image
end
if @word.split(//) == @letters_correct
you_win
exit
end
puts "Letters you have tried --> #{@letters_guessed.join("")}".colorize(:purple)
end
end
def ascii_image
if @guesses_counter == 1
puts "
_____________________________________________
_________________¶¶¶¶¶¶¶¶¶¶¶_________________
____________¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶____________
__________¶¶¶¶¶¶_____________¶¶¶¶¶¶__________
________¶¶¶¶¶___________________¶¶¶¶¶________
_______¶¶¶¶_______________________¶¶¶¶_______
______¶¶¶___________________________¶¶¶______
_____¶¶¶______¶¶¶¶¶_______¶¶¶¶¶______¶¶¶_____
____¶¶¶______¶¶¶¶¶¶_______¶¶¶¶¶¶______¶¶¶____
"
elsif @guesses_counter == 2
puts "
_____________________________________________
_________________¶¶¶¶¶¶¶¶¶¶¶_________________
____________¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶____________
__________¶¶¶¶¶¶_____________¶¶¶¶¶¶__________
________¶¶¶¶¶___________________¶¶¶¶¶________
_______¶¶¶¶_______________________¶¶¶¶_______
______¶¶¶___________________________¶¶¶______
_____¶¶¶______¶¶¶¶¶_______¶¶¶¶¶______¶¶¶_____
____¶¶¶______¶¶¶¶¶¶_______¶¶¶¶¶¶______¶¶¶____
___¶¶¶_______¶¶¶¶¶¶_______¶¶¶¶¶¶_______¶¶¶___
___¶¶¶________¶¶¶¶_________¶¶¶¶________¶¶¶___
___¶¶___________________________________¶¶___
___¶¶___________________________________¶¶___
___¶¶______¶¶___________________¶¶______¶¶___
___¶¶¶_____¶¶___________________¶¶_____¶¶¶___
___¶¶¶______¶¶_________________¶¶______¶¶¶___
____¶¶¶______¶¶¶_____________¶¶¶______¶¶¶____
"
elsif @guesses_counter == 3
puts "
_____________________________________________
_________________¶¶¶¶¶¶¶¶¶¶¶_________________
____________¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶____________
__________¶¶¶¶¶¶_____________¶¶¶¶¶¶__________
________¶¶¶¶¶___________________¶¶¶¶¶________
_______¶¶¶¶_______________________¶¶¶¶_______
______¶¶¶___________________________¶¶¶______
_____¶¶¶______¶¶¶¶¶_______¶¶¶¶¶______¶¶¶_____
____¶¶¶______¶¶¶¶¶¶_______¶¶¶¶¶¶______¶¶¶____
___¶¶¶_______¶¶¶¶¶¶_______¶¶¶¶¶¶_______¶¶¶___
___¶¶¶________¶¶¶¶_________¶¶¶¶________¶¶¶___
___¶¶___________________________________¶¶___
___¶¶___________________________________¶¶___
___¶¶______¶¶___________________¶¶______¶¶___
___¶¶¶_____¶¶___________________¶¶_____¶¶¶___
___¶¶¶______¶¶_________________¶¶______¶¶¶___
____¶¶¶______¶¶¶_____________¶¶¶______¶¶¶____
_____¶¶¶______¶¶¶¶¶¶_____¶¶¶¶¶¶______¶¶¶_____
______¶¶¶______¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶______¶¶¶______
_______¶¶¶¶________¶¶¶¶¶¶¶________¶¶¶¶_______
________¶¶¶¶¶___________________¶¶¶¶¶________
___¶¶¶¶___¶¶¶¶¶¶_____________¶¶¶¶¶¶___¶¶¶¶___
__¶¶¶¶¶¶¶____¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶____¶¶¶¶¶¶¶¶_
__¶¶¶___¶¶¶______¶¶¶¶¶¶¶¶¶¶¶______¶¶¶¶___¶¶¶_
__¶¶_____¶¶¶¶___________________¶¶¶¶______¶¶_
You are loosing! "
elsif @guesses_counter == 4
puts "
_____________________________________________
_________________¶¶¶¶¶¶¶¶¶¶¶_________________
____________¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶____________
__________¶¶¶¶¶¶_____________¶¶¶¶¶¶__________
________¶¶¶¶¶___________________¶¶¶¶¶________
_______¶¶¶¶_______________________¶¶¶¶_______
______¶¶¶___________________________¶¶¶______
_____¶¶¶______¶¶¶¶¶_______¶¶¶¶¶______¶¶¶_____
____¶¶¶______¶¶¶¶¶¶_______¶¶¶¶¶¶______¶¶¶____
___¶¶¶_______¶¶¶¶¶¶_______¶¶¶¶¶¶_______¶¶¶___
___¶¶¶________¶¶¶¶_________¶¶¶¶________¶¶¶___
___¶¶___________________________________¶¶___
___¶¶___________________________________¶¶___
___¶¶______¶¶___________________¶¶______¶¶___
___¶¶¶_____¶¶___________________¶¶_____¶¶¶___
___¶¶¶______¶¶_________________¶¶______¶¶¶___
____¶¶¶______¶¶¶_____________¶¶¶______¶¶¶____
_____¶¶¶______¶¶¶¶¶¶_____¶¶¶¶¶¶______¶¶¶_____
______¶¶¶______¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶______¶¶¶______
_______¶¶¶¶________¶¶¶¶¶¶¶________¶¶¶¶_______
________¶¶¶¶¶___________________¶¶¶¶¶________
___¶¶¶¶___¶¶¶¶¶¶_____________¶¶¶¶¶¶___¶¶¶¶___
__¶¶¶¶¶¶¶____¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶____¶¶¶¶¶¶¶¶_
__¶¶¶___¶¶¶______¶¶¶¶¶¶¶¶¶¶¶______¶¶¶¶___¶¶¶_
__¶¶_____¶¶¶¶___________________¶¶¶¶______¶¶_
___¶¶¶_____¶¶¶¶¶____________¶¶¶¶¶¶______¶¶¶__
__¶¶¶________¶¶¶¶¶¶¶____¶¶¶¶¶¶¶¶_________¶¶¶_
¶¶¶______________¶¶¶¶¶¶¶¶¶¶¶______________¶¶¶
¶¶¶___¶¶¶¶¶¶________¶¶¶¶________¶¶¶¶¶¶¶___¶¶¶
_¶¶¶¶¶¶____¶¶¶¶¶¶___________¶¶¶¶¶____¶¶¶¶¶¶¶_
_____________¶¶¶¶¶¶_______¶¶¶¶¶¶_____________
___________¶¶¶¶¶¶___________¶¶¶¶¶____________
_______¶¶¶¶¶¶¶______¶¶¶¶¶_______¶¶¶¶¶________
------>> You will loose!"
elsif @guesses_counter == 5
puts "
_____________________________________________
_________________¶¶¶¶¶¶¶¶¶¶¶_________________
____________¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶____________
__________¶¶¶¶¶¶_____________¶¶¶¶¶¶__________
________¶¶¶¶¶___________________¶¶¶¶¶________
_______¶¶¶¶_______________________¶¶¶¶_______
______¶¶¶___________________________¶¶¶______
_____¶¶¶______¶¶¶¶¶_______¶¶¶¶¶______¶¶¶_____
____¶¶¶______¶¶¶¶¶¶_______¶¶¶¶¶¶______¶¶¶____
___¶¶¶_______¶¶¶¶¶¶_______¶¶¶¶¶¶_______¶¶¶___
___¶¶¶________¶¶¶¶_________¶¶¶¶________¶¶¶___
___¶¶___________________________________¶¶___
___¶¶___________________________________¶¶___
___¶¶______¶¶___________________¶¶______¶¶___
___¶¶¶_____¶¶____________________¶¶_____¶¶¶___
___¶¶¶______¶¶_________________¶¶______¶¶¶___
____¶¶¶______¶¶¶_____________¶¶¶______¶¶¶____
_____¶¶¶______¶¶¶¶¶¶_____¶¶¶¶¶¶______¶¶¶_____
______¶¶¶______¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶______¶¶¶______
_______¶¶¶¶________¶¶¶¶¶¶¶________¶¶¶¶_______
________¶¶¶¶¶___________________¶¶¶¶¶________
___¶¶¶¶___¶¶¶¶¶¶_____________¶¶¶¶¶¶___¶¶¶¶___
__¶¶¶¶¶¶¶____¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶____¶¶¶¶¶¶¶¶_
__¶¶¶___¶¶¶______¶¶¶¶¶¶¶¶¶¶¶______¶¶¶¶___¶¶¶_
__¶¶_____¶¶¶¶___________________¶¶¶¶______¶¶_
___¶¶¶_____¶¶¶¶¶____________¶¶¶¶¶¶______¶¶¶__
__¶¶¶________¶¶¶¶¶¶¶____¶¶¶¶¶¶¶¶_________¶¶¶_
¶¶¶______________¶¶¶¶¶¶¶¶¶¶¶______________¶¶¶
¶¶¶___¶¶¶¶¶¶________¶¶¶¶________¶¶¶¶¶¶¶___¶¶¶
_¶¶¶¶¶¶____¶¶¶¶¶¶___________¶¶¶¶¶____¶¶¶¶¶¶¶_
_____________¶¶¶¶¶¶_______¶¶¶¶¶¶_____________
___________¶¶¶¶¶¶___________¶¶¶¶¶____________
_______¶¶¶¶¶¶¶______¶¶¶¶¶_______¶¶¶¶¶________
__¶¶¶¶¶¶¶¶¶______¶¶¶¶¶¶¶¶¶¶¶______¶¶¶¶¶¶¶¶¶__
¶¶¶¶¶¶_________¶¶¶¶¶_____¶¶¶¶¶__________¶¶¶¶¶
¶¶¶__________¶¶¶¶__________¶¶¶¶¶__________¶¶¶
¶¶_________¶¶¶¶______________¶¶¶¶¶________¶¶¶
¶¶¶¶¶______¶¶__________________¶¶¶_______¶¶¶¶
_¶¶¶¶¶____¶¶¶___________________¶¶¶____¶¶¶¶¶_
_____¶¶¶¶¶¶¶_____________________¶¶¶¶¶¶¶¶____
--------->>>> Game Over!!!
".colorize(:red).bold.blink
puts "THE WORD WAS: #{@word}".bold.colorize(:blue)
end
end
# This method displays the image and win message when the user wins.S
def you_win
puts "
_____________________________________________
_________________¶¶¶¶¶¶¶¶¶¶¶_________________
____________¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶____________
__________¶¶¶¶¶¶_____________¶¶¶¶¶¶__________
________¶¶¶¶¶___________________¶¶¶¶¶________
_______¶¶¶¶_______________________¶¶¶¶_______
______¶¶¶___________________________¶¶¶______
_____¶¶¶______¶¶¶¶¶_______¶¶¶¶¶______¶¶¶_____
____¶¶¶______¶¶¶¶¶¶_______¶¶¶¶¶¶______¶¶¶____
___¶¶¶_______¶¶¶¶¶¶_______¶¶¶¶¶¶_______¶¶¶___
___¶¶¶________¶¶¶¶_________¶¶¶¶________¶¶¶___
___¶¶___________________________________¶¶___
___¶¶___________________________________¶¶___
___¶¶______¶¶___________________¶¶______¶¶___
___¶¶¶_____¶¶___________________¶¶_____¶¶¶___
___¶¶¶______¶¶_________________¶¶______¶¶¶___
____¶¶¶______¶¶¶_____________¶¶¶______¶¶¶____
_____¶¶¶______¶¶¶¶¶¶_____¶¶¶¶¶¶______¶¶¶_____
______¶¶¶______¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶______¶¶¶______
_______¶¶¶¶________¶¶¶¶¶¶¶________¶¶¶¶_______
________¶¶¶¶¶___________________¶¶¶¶¶________
__________¶¶¶¶¶¶_____________¶¶¶¶¶¶__________
____________¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶¶______________
----->> You WIN!!!".colorize(:green).blink
end
end
WordGuess.new