-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
138 lines (116 loc) · 3.07 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
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
# Hangman game
# -----------------------------------
import random
WORDLIST_FILENAME = "words.txt"
asciiPics = [
'''
+---+
|
|
|
|
|
=========''',
'''
+---+
| |
|
|
|
|
=========''', '''
+---+
| |
O |
|
|
|
=========''', '''
+---+
| |
O |
| |
|
|
=========''', '''
+---+
| |
O |
/| |
|
|
=========''', '''
+---+
| |
O |
/|\ |
|
|
=========''', '''
+---+
| |
O |
/|\ |
/ |
|
=========''', '''
+---+
| |
O |
/|\ |
/ \ |
|
=========''']
def loadWords():
print("Loading word list from file...")
inFile = open(WORDLIST_FILENAME, 'r')
line = inFile.readline()
wordlist = line.split()
print(" ", len(wordlist), "words loaded.")
return wordlist
def chooseWord(wordlist: list):
return random.choice(wordlist)
wordlist = loadWords()
def isWordGuessed(secretWord: str, lettersGuessed: list[str]):
return True if len(secretWord) == len(''.join([letter if letter in secretWord else '' for letter in lettersGuessed])) else False
def getGuessedWord(secretWord: str, lettersGuessed: list):
return ''.join([ character if character in lettersGuessed else '_ ' if character != secretWord[-1] or len(secretWord) == 1 else ' _' for character in secretWord])
def getAvailableLetters(lettersGuessed: list):
return ''.join([letter if letter not in lettersGuessed else '' for letter in 'abcdefghijklmnopqrstuvwxyz'])
def hangman(secretWord: str):
availableLetters = 8
leftLetters = 0
guessedLetters = []
pic_step = 0
print('Welcome to the game Hangman!\nI am thinking of a word that is', len(secretWord), 'letters long.')
while leftLetters < availableLetters and not isWordGuessed(secretWord, guessedLetters):
if isWordGuessed(secretWord, guessedLetters):
print('Congratulations, you won!')
return
print('------------')
if pic_step != 0:
print(asciiPics[round(pic_step) - 1])
print("You have", availableLetters - leftLetters, "left.")
print("Available letters:", getAvailableLetters(guessedLetters))
letter = input('Please guess a letter:')
if not len(letter):
continue
letter = letter[0].lower()
if letter in guessedLetters:
print("Oops! You've already guessed that letter:", getGuessedWord(secretWord, guessedLetters))
else:
guessedLetters.append(letter)
if letter not in secretWord:
print('Oops! That letter is not in my word:', getGuessedWord(secretWord, guessedLetters))
pic_step+=1
leftLetters+=1
if letter in secretWord:
print("Good guess:", getGuessedWord(secretWord, guessedLetters))
print('------------')
if isWordGuessed(secretWord, guessedLetters):
print('Congratulations, you won!')
else:
print(asciiPics[round(pic_step) - 1])
print('Sorry, you ran out of guesses. The word was', secretWord + '.')
secretWord = chooseWord(wordlist).lower()
hangman(secretWord)