-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
68 lines (56 loc) · 2.09 KB
/
main.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
# This is a hangman game
import os.path
import random
class HangmanGame:
def __init__(self):
self.guessedWord = []
self.chosenWord = ""
self.dictionary = []
def readDictionary(self, filePath):
file = open(filePath, 'r')
self.dictionary = file.read().splitlines()
print("Added ", len(self.dictionary), "words to the dictionary")
file.close()
def chooseWord(self, length = 5):
temp = self.dictionary.copy()
self.dictionary.clear()
for word in temp:
if len(word) == length:
self.dictionary.append(word)
print("There are ", len(self.dictionary), "words of length ", length)
self.chosenWord = self.dictionary[random.randint(0, len(self.dictionary))]
self.guessedWord.clear()
self.guessedWord.extend([False] * length)
def makeGuess(self, guess):
for index in range(0, len(self.chosenWord)):
if self.chosenWord[index] == guess:
self.guessedWord[index] = True
return self.guessedWord.count(True)
if not os.path.isfile("dictionary.txt"):
print("Dictionary file not found. Program ended")
quit()
else:
print("Reading dictionary file")
game = HangmanGame()
game.readDictionary("dictionary.txt")
length = int(input("How long of a word do you want? "))
game.chooseWord(length)
numCorrect = 0
lastNumCorrect = 0
numGuesses = length
while numCorrect != length and numGuesses != 0:
numCorrect = game.makeGuess(input(str(numGuesses) + " guesses left\nWhat character do you want? "))
if numCorrect == lastNumCorrect:
numGuesses -= 1
else:
lastNumCorrect = numCorrect
for index in range(0, len(game.chosenWord)):
if game.guessedWord[index]:
print(game.chosenWord[index], end = '')
else:
print('_', end = '')
print('\n')
if numCorrect == length:
print("Congratulations!! The word is ", game.chosenWord)
else:
print("Sorry, the word was ", game.chosenWord)