-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHangman.py
189 lines (134 loc) · 4.49 KB
/
Hangman.py
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
## Hangman game
## Probe your lexical skills trying to beat the Hangman
## Created by Kelvin Rodriguez
## Hangman Class
import string
class Hangman():
'''Hangman class
word: the numbers of tries for the word to be guessed
'''
## Doll representing the state of the player
doll = ['''
**
* *
**
||
/||\\
/ || \\
/ \\
/ \\
''','''
**
* *
**
||
||\\
|| \\
/ \\
/ \\
''','''
**
* *
**
||
||
||
/ \\
/ \\
''','''
**
* *
**
||
||
||
\\
\\
''','''
**
* *
**
||
||
||
''','''
|
** |
* *|
**||
||
||
''']
def __init__(self,word, fix_len = 0):
# Get secret word
self._secret_word = word
# Max number of trials
self.count = len(word)+3
# Shows the letters guessed otherwise '_'
self.guessed_word = '_ '*len(self._secret_word)
# The letters that have been entered by the player
self.letters_guessed = set()
def isTheWordGuessed(self):
''' Returns boolean: True if the word was discovered, False otherwise
Test if the guess is equal to the secret_word
It iterates over every letter in the letters guessed list
and if they are in the secret word then sums all the numbers of its appearance'''
guessed = self.guessed_word.replace(" ","").replace("_","")
return guessed == self._secret_word
def newGuessedWord(self):
'''Change the guessed_word to its new values replacements'''
new_guessed_word = []
sw = self._secret_word
for i in range(len(sw)):
if sw[i] in self.letters_guessed:
new_guessed_word.append(sw[i]+' ')
else:
new_guessed_word.append('_ ')
self.guessed_word = ''.join(new_guessed_word)
def areThereTrials(self):
'''Returns true if there are trials left, false otherwise '''
return self.count <= 0
def status(self):
'''Returns an ASCII doll representing the status of the game'''
return self.doll[max(0, 5-self.count)]
def guessedWord(self):
'''Returns the guessed letter and not guessed underlined'''
return self.guessed_word
def lettersGuessed(self):
'''Returns the letters that have been guessed'''
return ' '.join(self.letters_guessed)
def isValidInput(self, letter):
'''Returns true if the letter is not empty string or not an alphabet letter'''
return True if len(letter) == 1 and letter in string.lowercase else False
def wasGuessed(self, letter):
'''Returns True if the letter is in letters_guessed, False otherwise'''
return True if letter in self.letters_guessed else False
def guessWholeWord(self,word):
'''Returns True if the word was guessed, False otherwise
This call costs 3 trials
'''
self.count -= 3
if len(word) != self._secret_word: return False
if word == self._secret_word:
self.letters_guessed.union(set(word))
return True
return True if word == self._secret_word else False
def guessLetter(self,letter):
'''Returns True if the letter is in secret word, False otherwise
letter: the letter
'''
self.letters_guessed.add(letter)
self.count -= 1
return letter in self._secret_word
def getSecretWord(self):
'''Return secret word'''
return self._secret_word
def getWordLength(self):
'''Return an integer representing the length of the word'''
return len(self._secret_word)
def getLettersGuessed(self):
'''Return string with the letters guessed, underscores otherwise'''
return self.guessed_word
def getTrialsLeft(self):
'''Return interger representing the number if trials left'''
return self.count