-
Notifications
You must be signed in to change notification settings - Fork 0
/
hangman.py
54 lines (46 loc) · 1.34 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
'''
This is the entry-point to the Hangman game.
'''
from hangman_solver import hangman_solver as solver
# def __init__(self, solution):
# self.solution = solution
# self.lives = 11
def check_result(pattern, letter, solution):
'''
Returns the new pattern based on the given letter.
'''
new_pattern = []
if letter not in solution:
return pattern, -1
else:
life = 0
for index, x in enumerate(solution):
if x == letter:
new_pattern.append(x)
else:
new_pattern.append(pattern[index])
return ''.join(new_pattern), life
def main():
lives = 11
solution = raw_input('Please enter random word:')
solver_obj = solver(r'./dictionary.txt')
s = solver_obj.begin_solving(len(solution))
pattern, next_letter = s.next()
while (1):
new_pattern, life = check_result(pattern, next_letter, solution)
lives += life
print 'Result:', new_pattern
if '_' not in new_pattern:
print "You have guessed the word!"
break
if lives < 0:
print 'No more lives left!'
break
else:
print lives,"lives left"
raw_input()
s.next()
pattern, next_letter = s.send(new_pattern)
s.close()
if __name__ == "__main__":
main()