-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathmain.py
More file actions
172 lines (149 loc) · 3.62 KB
/
main.py
File metadata and controls
172 lines (149 loc) · 3.62 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
import random
import os
WORDS = {
'a': "anchor",
'b': "buffer",
'c': "canyon",
'd': "dazzle",
'e': "ember",
'f': "fossil",
'g': "gadget",
'h': "harvest",
'i': "ignite",
'j': "jungle",
'k': "karate",
'l': "ladder",
'm': "marble",
'n': "nectar",
'o': "oracle",
'p': "puzzle",
'q': "quiver",
'r': "rocket",
's': "silver",
't': "tunnel",
'u': "urgent",
'v': "valley",
'w': "wonder",
'x': "xylophone",
'y': "yodel",
'z': "zealot",
}
HANGMAN_STAGES = [
r"""
+---+
| |
|
|
|
|
=========
""",
r"""
+---+
| |
O |
|
|
|
=========
""",
r"""
+---+
| |
O |
| |
|
|
=========
""",
r"""
+---+
| |
O |
/| |
|
|
=========
""",
r"""
+---+
| |
O |
/|\ |
|
|
=========
""",
r"""
+---+
| |
O |
/|\ |
/ |
|
=========
""",
r"""
+---+
| |
O |
/|\ |
/ \ |
|
=========
"""
]
MAX_WRONG_GUESSES = 6
def clear_screen():
"""Clears the terminal screen for a cleaner interface."""
os.system('cls' if os.name == 'nt' else 'clear')
def display_board(word, guessed_letters, wrong_guesses):
"""Displays the current state of the game board."""
clear_screen()
print("--- Hangman ---")
print(HANGMAN_STAGES[wrong_guesses])
# Display the word with guessed letters revealed
display_word = " ".join(
[letter if letter in guessed_letters else "_" for letter in word]
)
print(f"Word: {display_word}")
# Show guessed letters that are not in the word
incorrectly_guessed = sorted([g for g in guessed_letters if g not in word])
print(f"Incorrect Guesses: {' '.join(incorrectly_guessed)}\n")
def play_game():
"""Main function to run a single Hangman game session."""
word = random.choice(list(WORDS.values()))
guessed_letters = {word[0]} # Automatically reveal the first letter
wrong_guesses = 0
clear_screen()
print("--- Welcome to Hangman! ---")
print("You have 6 wrong guesses to find the word.")
input("\nPress Enter to start...")
while wrong_guesses < MAX_WRONG_GUESSES:
display_board(word, guessed_letters, wrong_guesses)
# Check for win condition
if all(letter in guessed_letters for letter in word):
print(f"Congratulations! You guessed the word: '{word}'")
return # End the game
guess = input("Guess a letter: ").lower()
# Input validation
if not guess.isalpha() or len(guess) != 1:
print("\nInvalid input. Please enter a single letter.")
input("Press Enter to continue...")
continue
if guess in guessed_letters:
print("\nYou already guessed that letter!")
input("Press Enter to continue...")
continue
guessed_letters.add(guess)
if guess not in word:
wrong_guesses += 1
print(f"\nWrong! '{guess}' is not in the word.")
if wrong_guesses < MAX_WRONG_GUESSES:
input("Press Enter to continue...")
# If the loop finishes, the player has lost the game
display_board(word, guessed_letters, wrong_guesses)
print(f"\nGame over! You ran out of guesses. The word was '{word}'.")
if __name__ == "__main__":
play_game()
print("\nThanks for playing The Hangman!")