-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathHangman.py
More file actions
308 lines (281 loc) · 8.77 KB
/
Copy pathHangman.py
File metadata and controls
308 lines (281 loc) · 8.77 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
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# =============================================================================
# Title: Hangman
# Author: Ryan J. Slater
# Date: Tue Aug 21 15:25:11 2018
# =============================================================================
import urllib.request
import requests
import random as rand
wordList = requests.get("http://svnweb.freebsd.org/csrg/share/dict/words?view=co&content-type=text/plain").content.splitlines()
for i in range(len(wordList)):
wordList[i] = str(wordList[i])
wordList[i] = wordList[i][2:len(wordList[i])-1]
wordList[i] = wordList[i].lower()
i, stop = 0, len(wordList)
while i < stop:
for j in range(len(wordList[i])):
if wordList[i][j] not in 'abcdefghijklmnopqrstuvwxyz':
wordList.pop(i)
i -= 1
stop = len(wordList)
break
i += 1
def getRandomWord(numPlayers, minWordLength, maxWordLength):
while True:
word = wordList[rand.randint(0, len(wordList))]
if len(word) >= minWordLength and len(word) <= maxWordLength:
return word
def printNoose(num):
if num == 0:
print(' ')
print(' ')
print(' ')
print(' ')
print(' ')
print(' ')
print(' ')
print(' ')
print(' _____')
elif num == 1:
print(' ')
print(' ')
print(' ')
print(' ')
print(' ')
print(' ')
print(' ')
print(' ')
print(' _/|\_')
elif num == 2:
print(' ')
print(' |')
print(' |')
print(' |')
print(' |')
print(' |')
print(' |')
print(' |')
print(' _/|\_')
elif num == 3:
print(' ____')
print(' \|')
print(' |')
print(' |')
print(' |')
print(' |')
print(' |')
print(' |')
print(' _/|\_')
elif num == 4:
print(' ____')
print(' | \|')
print(' |')
print(' |')
print(' |')
print(' |')
print(' |')
print(' |')
print(' _/|\_')
elif num == 5:
print(' ____')
print(' | \|')
print(' O |')
print(' |')
print(' |')
print(' |')
print(' |')
print(' |')
print(' _/|\_')
elif num == 6:
print(' ____')
print(' | \|')
print(' O |')
print(' | |')
print(' |')
print(' |')
print(' |')
print(' |')
print(' _/|\_')
elif num == 7:
print(' ____')
print(' | \|')
print(' O |')
print(' | |')
print(' | |')
print(' |')
print(' |')
print(' |')
print(' _/|\_')
elif num == 8:
print(' ____')
print(' | \|')
print(' O |')
print(' /| |')
print(' | |')
print(' |')
print(' |')
print(' |')
print(' _/|\_')
elif num == 9:
print(' ____')
print(' | \|')
print(' O |')
print(' /|\ |')
print(' | |')
print(' |')
print(' |')
print(' |')
print(' _/|\_')
elif num == 10:
print(' ____')
print(' | \|')
print(' O |')
print(' /|\ |')
print(' | |')
print(' / |')
print(' |')
print(' |')
print(' _/|\_')
elif num >= 11:
print(' ____')
print(' | \|')
print(' O |')
print(' /|\ |')
print(' | |')
print(' / \ |')
print(' |')
print(' |')
print(' _/|\_')
def checkWin(guesses, word):
win = True
for i in word:
if i not in guesses:
win = False
break
if win:
return 'w'
incorrectGuessCount = 0
for i in guesses:
if i not in word:
incorrectGuessCount += 1
if incorrectGuessCount == 11:
return 'l'
return ''
def printGuessedLetters(guesses, word):
incorrectGuesses = []
for letter in guesses:
if letter not in word:
incorrectGuesses.append(letter)
incorrectGuesses.sort()
print('Guessed letters: ', end='')
if len(incorrectGuesses) > 0:
for i in range(len(incorrectGuesses)):
print(incorrectGuesses[i], end='')
if i + 1 < len(incorrectGuesses):
print(', ', end='')
print('\n')
def define(search):
definition = ''
with urllib.request.urlopen('http://dictionary.reference.com/browse/' + str(search) + '?s=t') as response:
html = response.read()
html = str(html)
for i in range(len(html)):
if html[i:i+7] == 'content':
for j in range(i, i+10000000):
if html[j] == '"':
for k in range(j+14+len(search), j+1000000):
if html[k] == '.':
break
if k == j+1:
definition = definition + html[k].upper()
else:
definition = definition + html[k]
break
break
return definition
def Hangman():
print(500*'\n')
numPlayers = input('How many players? ').lower()
while True:
if numPlayers in ['one', 'two', '1', '2']:
break
else:
numPlayers = input('How many players? ').lower()
if numPlayers == 'one' or numPlayers == '1':
numPlayers = 1
else:
numPlayers = 2
minWordLength, maxWordLength = 4, 8
changeLengths = input('Change min and max word lengths? (current: 4, 8) [y/N] ').lower()
if 'y' in changeLengths:
changeLengths = True
else:
changeLengths = False
if changeLengths:
while True:
minWordLength = input('Min word length (0 < x < 11): ')
try:
minWordLength = int(float(minWordLength))
if minWordLength > 0 and minWordLength < 11:
break
else:
print('Enter an integer between 1 and 10')
except:
print('Enter an integer between 1 and 10')
while True:
maxWordLength = input('Max word length (x > ' + str(minWordLength) + '): ')
try:
maxWordLength = int(float(maxWordLength))
if maxWordLength > minWordLength:
break
else:
print('Enter an integer > ' + str(minWordLength))
except:
print('Enter an integer > ' + str(minWordLength))
word = ''
if numPlayers == 1:
word = getRandomWord(numPlayers, minWordLength, maxWordLength)
else:
pass
guesses = []
incorrectGuessCount = 0
while incorrectGuessCount < 11:
print(500*'\n')
printNoose(incorrectGuessCount)
printGuessedLetters(guesses, word)
for i in word:
if i in guesses:
print(i, end=' ')
else:
print('', end='_ ')
print()
while True:
guess = input('Guess a letter: ').lower()
if guess in 'abcdefghijklmnopqrstuvwxyz' and guess not in guesses and len(guess) == 1:
guesses.append(guess)
guesses.sort()
break
else:
if guess not in 'abcdefghijklmnopqrstuvwxyz' or len(guess) > 1:
print('Enter a letter')
elif guess in guesses:
print('You\'ve already guessed that letter!')
if guess not in word:
incorrectGuessCount += 1
if checkWin(guesses, word) == 'w':
print('\nYou win')
break
elif checkWin(guesses, word) == 'l':
print('\nYou lose')
break
print(500*'\n')
printNoose(11)
print('The word was: ' + word)
try:
print('Definition: ' + define(word))
except:
print('Unable to find definition')
if __name__ == '__main__':
Hangman()