Skip to content

Commit c39f836

Browse files
committed
add hangman game tutorial
1 parent 1c59af2 commit c39f836

File tree

4 files changed

+2208
-0
lines changed

4 files changed

+2208
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ This is a repository of all the tutorials of [The Python Code](https://www.thepy
172172
- [How to Organize Files by Extension in Python](https://www.thepythoncode.com/article/organize-files-by-extension-with-python). ([code](python-standard-library/extension-separator))
173173
- [How to Split a String In Python](https://www.thepythoncode.com/article/split-a-string-in-python). ([code](python-standard-library/split-string))
174174
- [How to Print Variable Name and Value in Python](https://www.thepythoncode.com/article/print-variable-name-and-value-in-python). ([code](python-standard-library/print-variable-name-and-value))
175+
- [How to Make a Hangman Game in Python](https://www.thepythoncode.com/article/make-a-hangman-game-in-python). ([code](python-standard-library/hangman-game))
175176

176177
- ### [Using APIs](https://www.thepythoncode.com/topic/using-apis-in-python)
177178
- [How to Automate your VPS or Dedicated Server Management in Python](https://www.thepythoncode.com/article/automate-veesp-server-management-in-python). ([code](general/automating-server-management))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# [How to Make a Hangman Game in Python](https://www.thepythoncode.com/article/make-a-hangman-game-in-python)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
from string import ascii_letters
2+
import os
3+
import random
4+
5+
class Hangman:
6+
7+
def __init__(self):
8+
with open("./words.txt", "r") as file:
9+
words = file.read().split("\n")
10+
self.secret_word = random.choice(words)
11+
self.guessed_word = "*" * len(self.secret_word)
12+
13+
self.incorrect_guess_limit = 6
14+
self.incorrect_guesses = 0
15+
self.wrong_guesses = []
16+
self.gallow_pieces = [
17+
"------",
18+
"| |",
19+
"| ",
20+
"| ",
21+
"| ",
22+
"|"
23+
]
24+
self.gallow = "\n".join(self.gallow_pieces)
25+
self.man_pieces = [
26+
" \\",
27+
"/",
28+
" \\",
29+
" |",
30+
"/",
31+
"O",
32+
]
33+
34+
def greet_user(self):
35+
print("Hangman\n")
36+
37+
def show_list_of_wrong_guesses(self):
38+
# show the list of wrong guesses
39+
print(f"Wrong guesses: {', '.join(self.wrong_guesses)}\n\n")
40+
41+
def take_guess(self) -> str:
42+
# take user guess
43+
while True:
44+
guess = input("Guess a letter:\n>>> ")
45+
if len(guess) == 1 and guess in ascii_letters:
46+
break
47+
else:
48+
print("Invalid input")
49+
return guess
50+
51+
def is_out_of_guesses(self) -> bool:
52+
# check if user is out of guesses
53+
return self.incorrect_guesses == self.incorrect_guess_limit
54+
55+
def check_guess(self, guess_letter: str):
56+
# check guess, if correct, update guessed word
57+
# if wrong, update gallow
58+
if guess_letter in self.secret_word:
59+
self._correct_guess(guess_letter)
60+
else:
61+
self._wrong_guess(guess_letter)
62+
63+
def _correct_guess(self, guess_letter: str):
64+
# find all index positions of the guess letter in the secret word
65+
index_positions = [index for index, item in enumerate(self.secret_word) if item == guess_letter]
66+
for i in index_positions:
67+
# update guessed word
68+
self.guessed_word = self.guessed_word[0:i] + guess_letter + self.guessed_word[i+1:]
69+
70+
def _wrong_guess(self, guess_letter: str):
71+
# update gallow
72+
row = 2
73+
if self.incorrect_guesses > 0 and self.incorrect_guesses < 4:
74+
row = 3
75+
elif self.incorrect_guesses >= 4:
76+
row = 4
77+
self.gallow_pieces[row] = self.gallow_pieces[row] + self.man_pieces.pop()
78+
self.gallow = "\n".join(self.gallow_pieces)
79+
# update wrong guesses
80+
if guess_letter not in self.wrong_guesses:
81+
self.wrong_guesses.append(guess_letter)
82+
self.incorrect_guesses += 1
83+
84+
def main():
85+
hangman = Hangman()
86+
87+
while True:
88+
# greet user and explain mechanics
89+
os.system('cls' if os.name=='nt' else 'clear')
90+
hangman.greet_user()
91+
# show gallow and the hidden word
92+
print(hangman.gallow, "\n")
93+
print("Secret word: ", hangman.guessed_word)
94+
# show the list of wrong guesses
95+
hangman.show_list_of_wrong_guesses()
96+
# check if user is out of guesses
97+
if hangman.is_out_of_guesses():
98+
print(f"Secret word is: {hangman.secret_word}")
99+
print("You lost")
100+
break
101+
elif hangman.guessed_word == hangman.secret_word:
102+
print("YOU WIN!!!")
103+
break
104+
else:
105+
# take user guess
106+
guess = hangman.take_guess()
107+
# check guess
108+
hangman.check_guess(guess)
109+
110+
if __name__ == "__main__":
111+
main()

0 commit comments

Comments
 (0)