-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpracthangman.py
More file actions
239 lines (182 loc) · 5.23 KB
/
practhangman.py
File metadata and controls
239 lines (182 loc) · 5.23 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
235
236
237
238
239
# import required libraries
import pandas as pd # for reading dataset
import random # for random word selection
# hangman drawing stages (from empty to full)
hangman_stages = [
"""
-----
| |
|
|
|
|
---------
""",
"""
-----
| |
O |
|
|
|
---------
""",
"""
-----
| |
O |
| |
|
|
---------
""",
"""
-----
| |
O |
/| |
|
|
---------
""",
"""
-----
| |
O |
/|\\ |
|
|
---------
""",
"""
-----
| |
O |
/|\\ |
/ |
|
---------
""",
"""
-----
| |
O |
/|\\ |
/ \\ |
|
---------
"""
]
# function to filter words based on difficulty level
def choose_words_by_level(words, level):
filtered_words = []
for word in words:
# easy → small words
if level == "easy" and len(word) <= 5:
filtered_words.append(word)
# normal → medium words
elif level == "normal" and 6 <= len(word) <= 9:
filtered_words.append(word)
# hard → long words
elif level == "hard" and len(word) > 9:
filtered_words.append(word)
return filtered_words
# main game function
def hangman():
data = pd.read_csv("vocab/words.csv", header=None)
# convert entire row into a list
words = []
for col in data.columns:
for cell in data[col].dropna().tolist():
for word in str(cell).split(","):
word = word.replace('"', '').strip()
if word != "":
words.append(word)
print("\n🎮 Welcome to Hangman!")
print("Try to guess the word before you run out of tries.\n")
# difficulty selection
level = input("Choose difficulty (easy / normal / hard): ").lower()
# filter words based on level
filtered_words = choose_words_by_level(words, level)
# fallback if no words found
if len(filtered_words) == 0:
print("No words found for this level. Using all words.")
filtered_words = words
# choose random word
word = random.choice(filtered_words).lower()
guessed_letters = [] # store guessed letters
tries = 6 # total attempts
wrong_guesses = 0 # track wrong guesses
hint_used = False # allow only one hint
print("\nThe word has", len(word), "letters.")
game_over = False
# game loop
while not game_over:
guess = input("\nGuess a letter: ").lower()
# input validation
if len(guess) != 1 or not guess.isalpha():
print("Please enter a single valid letter.")
continue
# check repeated guess
if guess in guessed_letters:
print("You already guessed that letter.")
continue
guessed_letters.append(guess)
# build display word
display = ""
for letter in word:
if letter in guessed_letters:
display += letter + " "
else:
display += "_ "
print("\nWord:", display)
# if guess is wrong
if guess not in word:
tries -= 1
wrong_guesses += 1
print("Wrong guess!")
print(hangman_stages[wrong_guesses])
print("Tries left:", tries)
# hint system (only once)
if not hint_used:
hint = input("Do you want a hint? (y/n): ").lower()
if hint == "y":
remaining_letters = []
for letter in word:
if letter not in guessed_letters:
remaining_letters.append(letter)
if len(remaining_letters) > 0:
hint_letter = random.choice(remaining_letters)
print("Hint: new letter added:", hint_letter)
guessed_letters.append(hint_letter)
# update display after hint
display = ""
for letter in word:
if letter in guessed_letters:
display += letter + " "
else:
display += "_ "
print("\nWord:", display)
else:
print("No hints available!")
hint_used = True
else:
print("No hints available")
# win condition
if "_" not in display:
print("\n🎉 Congratulations! You guessed the word:", word)
game_over = True
# lose condition
if tries == 0:
print("\n💀 Game Over! The word was:", word)
game_over = True
# main loop to replay game
def true():
while True:
hangman()
choice = input("\nPlay again? (y/n): ").lower()
if choice != "y":
print("Thanks for playing! 👋")
break
# start the game
true()