-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhangman.py
123 lines (106 loc) · 2.88 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
import sys,os,random
class Hangman:
def __init__(self):
"""Constructor to create the appropriate game variables"""
self.word = self.generateWord()
self.counter = 0
self.gameOver = False
self.correctLetters = []
self.incorrectLetters = []
def run(self):
"""Driver function for the game. Prints the board while the
player is playing and checks for winstate after each guess"""
self.clearScreen()
while(not self.gameOver):
self.printBoard()
guess = input("Enter the letter to guess: ")
if(len(guess) == 1):
self.tryGuess(guess)
self.checkWin()
print("Thanks for playing!")
def generateWord(self):
"""Grabs a random word from the supplied dictionary"""
filename = sys.path[0] + '/dictionary.txt'
words = open(filename).read().splitlines()
word = random.choice(words)
return word
def tryGuess(self, guess):
"""Handles a single-letter guess for a letter in the word"""
if guess in self.word:
if guess not in self.correctLetters:
self.correctLetters.append(guess)
else:
if guess not in self.incorrectLetters:
self.counter += 1
self.incorrectLetters.append(guess)
def checkWin(self):
"""Checks if the user has either run
out of guesses or guessed the word"""
self.clearScreen()
if self.counter == 6:
self.printHangman();
self.gameOver = True
print("You Lose")
print("The word was", self.word)
elif (self.allLettersGuessed()):
self.printHangman();
self.gameOver = True
print("You win!")
print("The word was", self.word)
def printBoard(self):
"""Prints the guessed letters and fills in
successfully guessed blanks in the word"""
self.printHangman()
print("Word: " ,end="")
for c in self.word:
if c in self.correctLetters:
print(c,end=" ")
else:
print("_ ",end="")
print("")
print("Guesses: " ,end="")
for c in self.incorrectLetters:
print(c,end="")
print("")
def allLettersGuessed(self):
"""Returns true if all letters in the word are successfully guessed"""
for c in self.word:
if c not in self.correctLetters:
return False
return True
def printHangman(self):
"""Prints the hangman figure to indicate guesses left"""
print(" |--|")
print(" | |")
if(self.counter>0):
print(" O |")
else:
print(" |")
if(self.counter > 3):
print(" /|\\ |")
elif(self.counter > 2):
print(" /| |")
elif(self.counter > 1):
print(" | |")
else:
print(" |")
if(self.counter > 5):
print(" / \\ |")
elif(self.counter > 4):
print(" / |")
else:
print(" |")
print(" |")
print("___________")
print("")
def clearScreen(self):
"""Clears the screen to refresh the board"""
os.system('cls')
def main():
"""Main function. Will generate hangman games until the user decides to stop"""
playAgain = "y"
while playAgain == "y":
hm = Hangman()
hm.run()
playAgain = input("Play Again? y/n:")
main()