-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhangman_oo.py
135 lines (108 loc) · 3.66 KB
/
hangman_oo.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
import getWord
import random
import re
def getRandomWord():
with open("engmix.txt", "rb") as f:
lines = f.readlines()
decodedLines = []
for l in lines:
try:
x = l.decode("utf-8")
decodedLines.append(x)
except:
pass
return random.choice(decodedLines)
class WordManager:
def __init__(self, chars):
# chars "antidisestablishmentarism"
# self.tuples = [["a", False], ["b", True]]
self.letterStatus = [[x, False] for x in chars]
self.guessedLetters = []
print(self.letterStatus)
def __str__(self):
return str(self.letterStatus)
def toGameString(self):
# return " ".join(<list>)
# --> ["d", False]
return " ".join([i if j else "_" for i,j in self.letterStatus])
#return "b _ n _ n _ "
def userHasWon(self):
for i in range(len(self.letterStatus)):
if self.letterStatus[i][1] == False:
return False
return True
def updateLetter(self, guessedLetter):
isLetterInWord = False
self.guessedLetters.append(guessedLetter)
for i in range(len(self.letterStatus)):
if self.letterStatus[i][0] == guessedLetter:
isLetterInWord = True
self.letterStatus[i][1] = True
return isLetterInWord
def alreadyGuessed(self,guess):
return guess in self.guessedLetters
def initializeWordManager():
"""Initialize WordManager and return it."""
mikeWord = getWord.getRandomWord()
print("'{}'".format(mikeWord))
newWord = WordManager(mikeWord)
print(newWord.toGameString())
return newWord
def playGame(w):
"""Run through a game loop for one game.
input arguments:
- w: WordManager containing word for the game
return value:
- None.
"""
wrongGuesses = 0
while True:
print(wrongGuesses)
while True:
guessedLetters = input("Guess a letter")
# what makes input correct? one letter. not previously guessed.
# match or extract information from strings
#
# prompt the user for a name: validate the input... all letters,
# first was capital, . read a file of comma-separated numbers,
# find stretches of numbers, find comma separating them, etc.
# or more complex: read formatted data, search for patterns in
# text files.
#
# validate that an input is a single letter.
#
# pick a letter and write it here: _
# "[a-z]"
# [a-z] foo
# [a-z] not match ""
# [a-z] not match "1a"
# re.match returns a MatchObject which allows us to ask questions
# about the match
flag = re.match("[a-z]$", guessedLetters)
if w.alreadyGuessed(guessedLetters):
print("you already guessed this letter, guess again")
elif flag:
break
else:
print("Only guess a single letter")
isCorrect = w.updateLetter(guessedLetters) # delegate this to WordManager
if isCorrect:
print ("Awesome!!!")
else:
wrongGuesses += 1
if wrongGuesses >= 3:
print("You Lose")
break
else:
print ("too bad, guess again")
if w.userHasWon():
isWin = True
print("You Win")
break
def main():
while True:
w = initializeWordManager()
playGame(w)
if theydontwanttoplayagain:
break
main()