forked from Mrinank-Bhowmick/python-beginner-projects
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
234 lines (206 loc) · 6.71 KB
/
main.py
File metadata and controls
234 lines (206 loc) · 6.71 KB
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import random
from RandomWords import easy_wordlist, medium_wordlist, hard_wordlist
DIFFICULTY_LEVEL = "medium"
def hangman(tries):
"""
List of Symbolic representation of losing the tries / for wrong attempts
(THE HANGMAN)
"""
stages = [
"""
# final state
--------
| |
| O
| \\|/
| ||
| // \\
-
""",
"""
# head, torso, four legs, four arms
--------
| |
| O
| \\|/
| |
| // \\
-
""",
# head, torso, both arms, and both legs
"""
--------
| |
| O
| \\|/
| |
| / \\
-
""",
# head, torso, both arms, and one leg
"""
--------
| |
| O
| \\|/
| |
| /
-
""",
# head, torso, and both arms
"""
--------
| |
| O
| \\|/
| |
|
-
""",
# head, torso, and one arm
"""
--------
| |
| O
| \\|
| |
|
-
""",
# head and torso
"""
--------
| |
| O
| |
| |
|
-
""",
# head
"""
--------
| |
| O
|
|
|
-
""",
# initial empty state
"""
--------
| |
|
|
|
|
-
""",
]
return stages[tries]
def get_word():
"""
easy_wordlist (list): list of words (strings) - for easy difficulty level
medium_wordlist (list): list of words (strings) - for medium difficulty level
hard_wordlist (list): list of words (strings) - for hard difficulty level
Returns a word from wordlist at random
"""
if DIFFICULTY_LEVEL == "easy":
word = random.choice(easy_wordlist)
elif DIFFICULTY_LEVEL == "medium":
word = random.choice(medium_wordlist)
elif DIFFICULTY_LEVEL == "hard":
word = random.choice(hard_wordlist)
else:
word = random.choice(medium_wordlist)
return word.upper()
def choose_difficulty():
global DIFFICULTY_LEVEL
DIFFICULTY_LEVEL = input("Choose difficulty level (easy, medium, hard): ").lower()
if DIFFICULTY_LEVEL == "easy":
return (
8 # we can adjust the number of initial guesses for each difficulty level
)
elif DIFFICULTY_LEVEL == "medium":
return 6
elif DIFFICULTY_LEVEL == "hard":
return 4
else:
print("Invalid difficulty level. Defaulting to medium.")
return 6
def play(word, initial_tries):
"""
Starts up an interactive game of Hangman.
* At the start of the game, let the user know how many
letters the secret_word contains and how many guesses s/he starts with.
* The user should start with 6 guesses
* Before each round, you should display to the user how many guesses
s/he has left and the letters that the user has not yet guessed.
* Ask the user to supply one guess per round. Remember to make
sure that the user puts in a letter!
* The user should receive feedback immediately after each guess
about whether their guess appears in the computer's word.
* After each guess, you should display to the user the
partially guessed word so far.
"""
word_completion = "_" * len(word)
guessed = False
guessed_letters = []
guessed_words = []
tries = initial_tries
print("\n-------------Welcome to Hangman-------------\n")
print(hangman(tries))
print(word_completion)
print("\n")
while not guessed and tries > 0:
guess = input("Guess the word:- ").upper()
if len(guess) == 1 and guess.isalpha():
if guess in guessed_letters:
print("You already guessed the letter", guess)
elif guess not in word:
print(guess, "is not in the word.")
tries -= 1
guessed_letters.append(guess)
else:
print("Good job,", guess, "is in the word!")
guessed_letters.append(guess)
word_as_list = list(word_completion)
indices = [i for i, letter in enumerate(word) if letter == guess]
for index in indices:
word_as_list[index] = guess
word_completion = "".join(word_as_list)
if "_" not in word_completion:
guessed = True
elif len(guess) == len(word) and guess.isalpha():
if guess in guessed_words:
print("You already guessed the word", guess)
elif guess != word:
print(guess, "is not the word.")
tries -= 1
guessed_words.append(guess)
else:
guessed = True
word_completion = word
else:
print("An incorrect guess.")
print(hangman(tries))
print(word_completion)
print("\n")
if guessed:
print("Congrats, you guessed the word! You win!")
else:
print("Sorry, you ran out of tries. The word was " + word + ". play again :)")
def main():
"""
* This triggers the game by asking for intialization
"""
difficulty = choose_difficulty()
word = get_word()
play(word, difficulty)
while input("Do you want to play Hangman? (y/n): ").upper() == "Y":
difficulty = choose_difficulty()
word = get_word()
play(word, difficulty)
if __name__ == "__main__":
main()