-
Notifications
You must be signed in to change notification settings - Fork 2
/
Hangman.py
170 lines (154 loc) · 5.08 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
import Guesser
import Executioner
import WordList
def end_game(guesser, executioner):
executioner.end_game()
def play():
guesser = Guesser.Guesser()
executioner = Executioner.Executioner()
guesser.solve(executioner)
return executioner.did_guesser_win
def repeat(trials):
guesser_wins = 0
guesser = Guesser.Guesser()
executioner = Executioner.Executioner()
for i in range(1, trials):
guesser.solve(executioner)
if executioner.did_guesser_win:
guesser_wins += 1
executioner.reset()
print 'Out of', trials, 'trials, the guesser won', guesser_wins, \
'times and the executioner won', trials - guesser_wins, 'times.'
def repeat2(trials):
guesser_wins = 0
guesser = Guesser.Guesser()
executioner = Executioner.Executioner()
highest_to_guess = 0
for i in range(1, trials):
guesser.solve(executioner)
if executioner.did_guesser_win:
guesser_wins += 1
if executioner.man + len(executioner.word) > highest_to_guess:
highest_to_guess = executioner.man + len(executioner.word)
print 'highest to guess: ' + str(highest_to_guess)
executioner.reset()
print 'Out of', trials, 'trials, the highest number of guesses was ' + str(highest_to_guess)
def play_hangman_executioner():
# This is rather primitive, and people might not trust the program.
guesser = Guesser.Guesser()
executioner = Executioner.Executioner()
wins = 0
losses = 0
while True:
word = raw_input('What is your word? ').strip()
if (word == '-q'):
end_game(guesser, executioner)
return None
while len(Guesser.wordlist.find_words(word)) == 0:
word = raw_input('That is not a word. What is your word? ').strip()
executioner.reset(word)
guesser.solve(executioner)
if executioner.did_guesser_win:
losses += 1
else:
wins += 1
print 'You have', wins, 'wins and', losses, 'losses'
print
def play_hangman_guesser():
executioner = Executioner.Executioner()
while True:
executioner.reset()
incorrect = ''
while executioner.is_playing:
print "word:", executioner.word
print "guessed letters:", incorrect
print "body parts remaining:", executioner.body_parts - executioner.man
while True:
letter = raw_input('What is your guess? ').strip()
if (letter == '-q'):
end_game(guesser, executioner)
return None
if len(letter) > 1:
print "That is more than just one letter. Please try again."
elif ord(letter) < ord('a') or ord(letter) > ord('z'):
print "You must enter a lowercase letter. Please try again."
else:
break
if executioner.guess(letter) != None:
break
if not letter in executioner.not_incorrect and not letter in incorrect:
incorrect += letter
print
def play_hangman_both():
guesser = Guesser.Guesser()
executioner = Executioner.Executioner()
wins = 0
losses = 0
myfile = open('results.txt', 'a')
while True:
word = raw_input('What is your word? ').strip()
if (word == '-q'):
end_game(guesser, executioner)
return None
while len(Guesser.wordlist.find_words(word)) == 0:
addp = raw_input('That is not a word. ' +
'Do you want to add it to the dictionary? (y/n) ').strip()
if addp == 'y':
print 'Adding...'
Guesser.wordlist.add_word(word)
Guesser.wordlist = WordList.WordList()
Executioner.wordlist = WordList.WordList()
print 'added.'
word = raw_input('What is your word? ').strip()
if (word == '-q'):
end_game(guesser, executioner)
return None
executioner.reset(word)
guesser.solve(executioner)
if executioner.did_guesser_win:
losses += 1
myfile.write("Computer | " + (executioner._word + ' ' * (14 - len(executioner._word))) + " | computer\n")
else:
myfile.write("Computer | " + (executioner._word + ' ' * (14 - len(executioner._word))) + " | human\n")
wins += 1
print 'You have', wins, 'wins and', losses, 'losses'
print
executioner.reset()
incorrect = ''
while executioner.is_playing:
print "word:", executioner.word
print "guessed letters:", incorrect
print "body parts remaining:", executioner.body_parts - executioner.man
while True:
letter = raw_input('What is your guess? ').strip()
if (letter == '-q'):
end_game(guesser, executioner)
return None
if len(letter) > 1:
print "That is more than just one letter. Please try again."
elif len(letter) == 0:
print "You didn't enter anything. Please try again."
elif ord(letter) < ord('a') or ord(letter) > ord('z'):
print "You must enter a lowercase letter. Please try again."
else:
break
if executioner.guess(letter) != None:
break
if not letter in executioner.not_incorrect and not letter in incorrect:
incorrect += letter
print
if executioner.did_guesser_win:
wins += 1
myfile.write("Human | " + (executioner._word + ' ' * (14 - len(executioner._word))) + " | human\n")
else:
myfile.write("Human | " + (executioner._word + ' ' * (14 - len(executioner._word))) + " | computer\n")
losses += 1
print 'You have', wins, 'wins and', losses, 'losses'
print
myfile.close()
#repeat2(1000)
#play_hangman_executioner()
#play_hangman_guesser()
play_hangman_both()
#ex = Executioner.Executioner()
#ex.all_words_for_count()