forked from 4dsolutions/Python5
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhangman2.py
More file actions
executable file
·220 lines (191 loc) · 5.26 KB
/
hangman2.py
File metadata and controls
executable file
·220 lines (191 loc) · 5.26 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
#!/usr/bin/env python3.4
"""
Adapted from hangman.py by Al Sweigart
https://inventwithpython.com/chapter9.html
Most excellent gallows pix by Joshua Yoeger
see https://github.com/joshuayoerger/hangman
"""
import sqlite3 as sql
import os
import random
HANGMANPICS = ["""
========@
|| //
||//
||/
||
__||______________
/ || /|
/_________________/ /
|_________________|/
""", """
========@
|| // |
||//
||/
||
__||______________
/ || /|
/_________________/ /
|_________________|/
""", """
========@
|| // |
||// O
||/
||
__||______________
/ || /|
/_________________/ /
|_________________|/
""", """
========@
|| // |
||// O
||/ /
||
__||______________
/ || /|
/_________________/ /
|_________________|/
""", """
========@
|| // |
||// O
||/ /|
||
__||______________
/ || /|
/_________________/ /
|_________________|/
""", """
========@
|| // |
||// O
||/ /|\\
||
__||______________
/ || /|
/_________________/ /
|_________________|/
""", """
========@
|| // |
||// O
||/ /|\\
|| /
__||______________
/ || /|
/_________________/ /
|_________________|/
""", """
========@
|| // |
||// 0
||/ /|\\
|| / \\
__||______________
/ || /|
/_________________/ /
|_________________|/
"""]
class Hangman:
def __init__(self, words):
self.words = words
self.secretWord = random.choice(self.words)
self.missedLetters = ''
self.correctLetters = ''
self.alreadyGuessed = ''
def __str__(self): # print(hm) triggers this method
display = ("\n"
"H A N G M A N \n"
"{picture}\n"
"Guessed: {blanks}\n"
" Missed: {missed}\n")
content = {}
content['picture'] = HANGMANPICS[len(self.missedLetters)]
content['missed'] = ""
for letter in self.missedLetters:
content['missed'] += letter + " "
blanks = list('_' * len(self.secretWord))
for i in range(len(self.secretWord)): # replace blanks
if self.secretWord[i] in self.correctLetters:
blanks[i] = self.secretWord[i]
blanks = "".join(blanks)
content["blanks"] = ""
for letter in blanks: # masked secret word
content['blanks'] += letter + " " # spaces between each letter
return display.format(**content) # keyword arguments to format()
def getGuess(self):
"""
Accept a letter (case sensitive) or space
"""
while True:
guess = input("Guess a letter (or 'exit'): ")
if guess == 'exit':
break
elif len(guess) != 1:
print('Please enter a single letter or space.')
elif guess in self.alreadyGuessed:
print('You have already guessed that letter. Choose again.')
elif not guess.isalpha() and not guess in " ()/": # OK symbols
print('Please enter a LETTER or SPACE.')
else:
break
return guess
def playAgain():
"""
This function returns True if the player wants to
play again, otherwise it returns False.
"""
return input('Do you want to play again? (yes or no) ').lower().startswith('y')
# get key terms from SQLite database as what to play hangman with.
dbpath = os.path.join(os.path.split(__file__)[0], "glossary.db") # db in same dir
db = sql.connect(dbpath) # connect
curs = db.cursor() # grab a cursor
curs.execute("SELECT * FROM Glossary") # SQL to retrieve all defined terms
words = [ ]
for word in curs.fetchall():
words.append(word[0])
db.close() # done with the database
hm = Hangman(words)
gameIsDone = False
while not gameIsDone:
print(hm)
# Let the player type in a letter.
guess = hm.getGuess()
if guess == 'exit':
print("Thanks for playing")
break
if guess in hm.secretWord:
hm.correctLetters += guess
hm.alreadyGuessed += guess
# Check if the player has won
foundAllLetters = True
for i in range(len(hm.secretWord)):
if hm.secretWord[i] not in hm.correctLetters:
foundAllLetters = False
continue
if foundAllLetters:
print(hm)
print('Yes! The secret word is "{}"! '
'You have won!'.format(hm.secretWord))
gameIsDone = True
else:
hm.missedLetters += guess
hm.alreadyGuessed += guess
# Check if player has guessed too many times and lost
if len(hm.missedLetters) == len(HANGMANPICS) - 1:
print(hm)
print('You have run out of guesses!\n'
+ 'After {}'.format(len(hm.missedLetters))
+ ' missed guesses and {}'.format(len(hm.correctLetters))
+ ' correct guesses, the word was "{}"'.format(hm.secretWord))
gameIsDone = True
# Ask the player if they want to play again (but only if the game is done).
if gameIsDone:
if playAgain():
hm = Hangman(words)
gameIsDone = False
else:
break