-
Notifications
You must be signed in to change notification settings - Fork 0
/
hangman.rb
285 lines (263 loc) · 5.29 KB
/
hangman.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
require 'yaml'
def word()
flag = true
while(flag)
line = File.readlines('5desk.txt').sample
if line.length >= 5 and line.length <= 12
flag = false
break
end
end
return line
end
def person()
hangman = ["""
-----
| |
|
|
|
|
|
|
|
--------
""",
"""
-----
| |
| 0
|
|
|
|
|
|
--------
""",
"""
-----
| |
| 0
| -+-
|
|
|
|
|
|
--------
""",
"""
-----
| |
| 0
| /-+-
|
|
|
|
|
--------
""",
"""
-----
| |
| 0
| /-+-/
|
|
|
|
|
--------
""",
"""
-----
| |
| 0
| /-+-/
| |
|
|
|
|
--------
""",
"""
-----
| |
| 0
| /-+-/
| |
| |
|
|
|
--------
""",
"""
-----
| |
| 0
| /-+-/
| |
| |
| |
|
|
--------
""",
"""
-----
| |
| 0
| /-+-/
| |
| |
| |
| |
|
--------
""",
"""
-----
| |
| 0
| /-+-/
| |
| |
| | |
| |
|
--------
""",
"""
-----
| |
| 0
| /-+-/
| |
| |
| | |
| | |
|
--------
""",
]
return hangman
end
def ld(guesses, ind, word, already)
puts "Would you like to reload your game? (yes/no): "
ld = gets.chomp
if ld.downcase == "yes"
guesses = YAML.load(File.read("guesses.yml"))
ind = YAML.load(File.read("ind.yml"))
word = YAML.load(File.read("word.yml"))
already = YAML.load(File.read("already.yml"))
end
return guesses, ind, word, already
end
def save(guesses, ind, word, already)
puts "Would you like to save the game?: "
save = gets.chomp
if save.downcase == "yes"
File.open("guesses.yml", "w") { |file| file.write(guesses.to_yaml)}
File.open("ind.yml", "w") { |file| file.write(ind.to_yaml)}
File.open("word.yml", "w") { |file| file.write(word.to_yaml)}
File.open("already.yml", "w") { |file| file.write(already.to_yaml)}
end
end
def guess_word(word)
puts "What is your guess?: "
guess = gets.chomp
if(guess.downcase == word) #if the user guesses right
puts "Congratulation! You guessed the right word! The word was " + word + "."
else #if the user guesses wrong
puts "Unfortunately, you guessed the wrong word. The word was " + word + "."
end
return false, false
end
def guess_letter(already, letters, guesses, ind)
puts "Which letter would you like to guess?: "
guess = gets.chomp
if already.include? guess #if the user has already guessed this letter
puts "You have already guessed this letter."
ourinput = false
game = true
elsif !already.include? guess #if the user hasn't guessed this letter
already << guess #add this letter to the list of guessed letters
if letters.include? guess.downcase #if the letter is right
indexes = []
(0..letters.length-1).each do |i|
if letters[i] == guess.downcase
indexes << i
end
end
(0..indexes.length-1).each do |i|
guesses[indexes[i]] = guess.downcase #add this letter to all of the indexes of guesses
end
if !guesses.difference(letters).any? #if the user guessed all the letters
puts "Congratulation! You guessed the right word! The word was " + word + "."
ourinput = false
game = false #end the game
else
puts "Congratulations, you guessed a letter right!"
ourinput = false
game = true
end
else #if the user guesses a wrong letter
puts "Unfortunately, you guessed wrong!"
ind +=1 #ind increments by 1
ourinput = false
game = true
end
end
return already, guesses, ind, ourinput, game
end
def lose(ind, hangman)
if ind > 9 #if the user uses up all their guesses
puts hangman[10] #print the final hangman state
puts "Unfortunately, you made too many wrong guesses. The word was " + word + "."
return false, false
end
return false, true
end
game = true
ind = 0
hangman = person()
word = word()
word = word.delete!("\n").downcase
guesses = []
letters = word.chars #splits the random word into a list and assigns it to "letters"
already = []
(0..letters.length-1).each do |i|
guesses.append("_")
end
if (File.file?("guesses.yml") and File.file?("ind.yml") and File.file?("word.yml") and File.file?("already.yml"))
guesses, ind, word, already = ld(guesses, ind, word, already)
letters = word.chars
end
puts """Welcome to Hangman! A word will be chosen at random, and you must try to guess it by guessing one letter at a time.
Everytime you guess a letter wrong, an extra body part will be added to the hangman. Once every body part appears on the hangman, you lose the game.
At any point in the game you can choose to guess the entire word, but if you guess it wrong you will automatically lose."""
while game == true
ourinput = true
puts hangman[ind] #print the hangman at whichever state
p guesses
while ourinput == true
save(guesses, ind, word, already)
puts "Would you like to try guessing the entire word?: "
yesno = gets.chomp
if yesno.downcase == "yes"
ourinput, game = guess_word(word)
elsif yesno.downcase == "no"
already, guesses, ind, ourinput, game = guess_letter(already, letters, guesses, ind)
if(game == true)
ourinput, game = lose(ind, hangman)
end
elsif yesno != "yes" or yesno != "no" and game == true #if the user doesn't enter yes or no
puts "Please enter 'yes' or 'no'"
end
end
end