-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
185 lines (161 loc) · 4.61 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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import requests
# Datamuse API endpoint for searching random words
endpoint = "https://random-word-api.herokuapp.com/word"
params = {
"number": 1, # Set number of requested words. If it exceeds the maximum stored amount, it will just return all of them.
"lang": 'en', # Set language of requested words. Go to Github to get more info on how to add your own language.
}
response = requests.get(endpoint, params=params)
if response.status_code == 200:
data = response.json()
if data:
random_word = data[0]
print(random_word)
else:
print("Word not found.")
else:
print("Error executing request:", response.status_code)
# Function to get a random word from a list
def get_word():
return random_word.upper()
# Function to get the player's state
def display_hangman(tries):
stages = [ # final state: head, torso, both arms, both legs
'''
--------
| |
| O
| \\|/
| |
| / \\
-
''',
# head, torso, both arms, one leg
'''
--------
| |
| O
| \\|/
| |
| /
-
''',
# head, torso, both arms
'''
--------
| |
| O
| \\|/
| |
|
-
''',
# head, torso and one arm
'''
--------
| |
| O
| \\|
| |
|
-
''',
# head and torso
'''
--------
| |
| O
| |
| |
|
-
''',
# head
'''
--------
| |
| O
|
|
|
-
''',
# initial state
'''
--------
| |
|
|
|
|
-
'''
]
return stages[tries]
def display_info(word, tries):
print(f"Your man looks like this:\n{display_hangman(tries)}")
print(f"You have {tries} attempts left. The word looks like this: {''.join(word)}")
# Main game logic
def play(word):
word_completion = ["_"] * len(word)
guessed_letters = []
guessed_words = []
tries = 6
print("Hey, let's play a guessing game!")
display_info(word_completion, tries)
while True:
if tries <= 0:
print(f"\nI'm sorry, but you lost!\n{display_hangman(tries)}")
print(f"The word was: {word}")
break
if "_" not in word_completion:
print(f"Congratulations! You guessed the word in {6 - tries} tries!")
display_info(word_completion, tries)
break
s = input("Enter a word or letter: ").upper()
flag = False
for i in s:
flag = False
if i not in "ABCDEFGHIJKLMNOPQRSTUVWXYZ" and i in "1234567890 !#$%&*+-=?@^_,.'\"":
print("\nInvalid input! Try again!")
flag = True
break
if flag:
continue
if s in guessed_letters or s in guessed_words:
print(f"\nYou have already entered '{s}'!")
continue
elif len(s) > 1:
if s == word:
print(f"Congratulations! You guessed the word in {6 - tries} tries!")
display_info(word_completion, tries)
break
else:
tries -= 1
guessed_words.append(s)
print("\nI'm sorry, but you guessed wrong!")
display_info(word_completion, tries)
else:
if s in word:
guessed_letters.append(s)
word_completion = [s if word[i] == s else word_completion[i] for i in range(len(word))]
print(f"\nCongratulations, you guessed the letter '{s}'")
display_info(word_completion, tries)
else:
tries -= 1
guessed_letters.append(s)
print("\nI'm sorry, but you guessed wrong!")
display_info(word_completion, tries)
return word
def main():
while True:
used_words = []
word = get_word()
while word in used_words:
word = get_word()
used_words.append(play(word))
if input("\nDo you want to play again? (yes/no): ").lower() != "yes":
print("Goodbye! Come play some more!")
break
if __name__ == '__main__':
main()