-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhangman_game.py
More file actions
158 lines (142 loc) · 5.55 KB
/
hangman_game.py
File metadata and controls
158 lines (142 loc) · 5.55 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
import random
class HangmanGame:
def __init__(self):
self.word_list = self.load_word_list() # List of words for the game
self.max_attempts = 6 # Maximum number of incorrect guesses allowed
self.word_to_guess = "" # The word the player has to guess
self.guessed_letters = [] # List to store the letters guessed so far
self.incorrect_guesses = 0 # Tracks the number of incorrect guesses
self.display_word = "" # The word to display with guessed letters and underscores
self.game_over = False
def load_word_list(self):
"""Load a list of words from a text file. If not available, use a predefined list."""
try:
with open('words.txt', 'r') as file:
return [line.strip() for line in file.readlines()]
except FileNotFoundError:
# Fallback to a hardcoded list if the file isn't found
return ["python", "hangman", "developer", "algorithm", "challenge", "machine", "learning"]
def choose_word(self):
"""Randomly choose a word from the word list."""
self.word_to_guess = random.choice(self.word_list)
self.display_word = "_" * len(self.word_to_guess) # Initialize display with underscores
def display_hangman(self):
"""Display the graphical hangman representation based on incorrect guesses."""
hangman_graphics = [
'''
------
| |
|
|
|
|
=========''', # 0 incorrect guesses
'''
------
| |
O |
|
|
|
=========''', # 1 incorrect guess
'''
------
| |
O |
| |
|
|
=========''', # 2 incorrect guesses
'''
------
| |
O |
/| |
|
|
=========''', # 3 incorrect guesses
'''
------
| |
O |
/|\\ |
|
|
=========''', # 4 incorrect guesses
'''
------
| |
O |
/|\\ |
/ |
|
=========''', # 5 incorrect guesses
'''
------
| |
O |
/|\\ |
/ \\ |
|
=========''' # 6 incorrect guesses
]
print(hangman_graphics[self.incorrect_guesses])
def get_guess(self):
"""Get the player's guess, ensuring it's a single letter and hasn't been guessed before."""
while True:
guess = input(f"Current word: {self.display_word}\nGuessed letters: {', '.join(self.guessed_letters)}\nGuess a letter: ").lower()
if len(guess) != 1 or not guess.isalpha():
print("Please enter a valid letter.")
elif guess in self.guessed_letters:
print("You've already guessed that letter. Try a different one.")
else:
return guess
def update_display_word(self, guess):
"""Update the display word with the correct guess."""
new_display_word = list(self.display_word)
for i, letter in enumerate(self.word_to_guess):
if letter == guess:
new_display_word[i] = guess
self.display_word = "".join(new_display_word)
def check_game_over(self):
"""Check if the game is over, either by win or loss."""
if "_" not in self.display_word:
print(f"Congratulations! You've guessed the word '{self.word_to_guess}' correctly!")
return True
elif self.incorrect_guesses >= self.max_attempts:
print(f"Game over! The word was '{self.word_to_guess}'.")
return True
return False
def play_game(self):
"""Main game loop for Hangman."""
self.choose_word()
while not self.game_over:
self.display_hangman()
guess = self.get_guess()
self.guessed_letters.append(guess)
if guess in self.word_to_guess:
print(f"Good guess! The letter '{guess}' is in the word.")
self.update_display_word(guess)
else:
print(f"Oops! The letter '{guess}' is not in the word.")
self.incorrect_guesses += 1
self.game_over = self.check_game_over()
print(f"Game Over. Your final word was '{self.word_to_guess}'.")
self.ask_for_replay()
def ask_for_replay(self):
"""Ask the player if they want to play again."""
play_again = input("Do you want to play again? (yes/no): ").lower()
if play_again == "yes":
self.reset_game()
self.play_game()
else:
print("Thank you for playing! Goodbye.")
def reset_game(self):
"""Reset game variables for a new round."""
self.incorrect_guesses = 0
self.guessed_letters = []
self.game_over = False
self.display_word = ""
if __name__ == "__main__":
game = HangmanGame()
game.play_game()