Skip to content

Commit 473f136

Browse files
authored
Addition of the Hangman Game (DhanushNehru#356)
* Added Hangman game and README * README changes * README changes * README changes
1 parent f816db5 commit 473f136

File tree

4 files changed

+640
-0
lines changed

4 files changed

+640
-0
lines changed

Hangman-Game/README.md

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
2+
# Hangman-Game
3+
4+
This is a Python implementation of the classic Hangman game. In this game, the player tries to guess a randomly selected word by suggesting letters within a certain number of tries. The player wins if they guess the word correctly before running out of attempts.
5+
6+
7+
8+
## How to Play
9+
1)The computer selects a random word from the provided word list.
10+
11+
2)The player guesses letters or the entire word.
12+
13+
3)For each correct guess, the corresponding letters are revealed in the word.
14+
15+
4)If the player makes an incorrect guess, they lose one of their remaining tries.
16+
17+
5)The game ends when the player either guesses the word or runs out of tries.
18+
## Requirements
19+
Python 3.x
20+
## How to Run
21+
22+
1) Clone the repository:
23+
24+
```bash
25+
git clone https://github.com/your-username/Python-Scripts.git
26+
27+
```
28+
2) Navigate to the Hangman-Game folder:
29+
```bash
30+
cd Python-Scripts/Hangman-Game
31+
```
32+
3) Run the game:
33+
```bash
34+
python hangman_game.py
35+
```
36+
## Files
37+
1)hangman_game.py: The main Python script containing the Hangman game logic.
38+
39+
2)words.py: A Python file that contains a list of words (word_list) from which the game selects the word to guess.
40+
41+
3)README.md: This documentation file.
42+
## Future Improvements
43+
1) Introducing an AI against whom the players can play.
44+
2) Addition of hints
45+
3) Expanding the word list
46+
## Contributions
47+
Contributions are welcome! Feel free to open an issue or submit a pull request to add new features, improve the code, or fix bugs.

Hangman-Game/hangman_game.py

+151
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
import random
2+
from words import word_list
3+
4+
def get_word():
5+
word = random.choice(word_list)
6+
return word.upper()
7+
8+
def play(word):
9+
word_completion = "_" * len(word)
10+
guessed = False
11+
guessed_letters = []
12+
guessed_words = []
13+
tries = 6
14+
print("Let's play Hangman!")
15+
print(display_hangman(tries))
16+
print(word_completion)
17+
print("\n")
18+
19+
while not guessed and tries > 0:
20+
guess = input("Please guess a letter or word:").upper()
21+
if len(guess) == 1 and guess.isalpha():
22+
if guess in guessed_letters:
23+
print("You have already guessed this letter : ", guess)
24+
25+
elif guess not in word:
26+
print(guess, "is not in the word.")
27+
tries = tries - 1
28+
guessed_letters.append(guess)
29+
30+
else:
31+
print("Good job !", guess, "is in the word.")
32+
guessed_letters.append(guess)
33+
word_as_list = list(word_completion) #Find all instances where guess appears in the word so we first convert that into a list
34+
indices = [i for i, letter in enumerate(word) if letter == guess]
35+
for index in indices:
36+
word_as_list[index] = guess
37+
word_completion = "". join(word_as_list)
38+
if "_" not in word_completion:
39+
guessed = True
40+
41+
42+
elif len(guess) == len(word) and guess.isalpha():
43+
if guess in guessed_words:
44+
print("you already guessed this word !")
45+
46+
elif guess != word:
47+
print(guess, "is not the word")
48+
tries = tries - 1
49+
guessed_words.append(guess)
50+
else:
51+
guessed = True
52+
word_completion = word
53+
54+
55+
else:
56+
print("Not a valid guess!")
57+
58+
print(display_hangman(tries))
59+
print(word_completion)
60+
print("\n")
61+
62+
if guessed :
63+
print("Congrats, yoiu guessed the word You win.")
64+
else:
65+
print("sorry you ran out of tries. The word was " + word + " Maybe next time")
66+
67+
def display_hangman(tries):
68+
stages = [ # final state: head, torso, both arms, and both legs
69+
"""
70+
--------
71+
| |
72+
| O
73+
| \\|/
74+
| |
75+
| / \\
76+
-
77+
""",
78+
# head, torso, both arms, and one leg
79+
"""
80+
--------
81+
| |
82+
| O
83+
| \\|/
84+
| |
85+
| /
86+
-
87+
""",
88+
# head, torso, and both arms
89+
"""
90+
--------
91+
| |
92+
| O
93+
| \\|/
94+
| |
95+
|
96+
-
97+
""",
98+
# head, torso, and one arm
99+
"""
100+
--------
101+
| |
102+
| O
103+
| \\|
104+
| |
105+
|
106+
-
107+
""",
108+
# head and torso
109+
"""
110+
--------
111+
| |
112+
| O
113+
| |
114+
| |
115+
|
116+
-
117+
""",
118+
# head
119+
"""
120+
--------
121+
| |
122+
| O
123+
|
124+
|
125+
|
126+
-
127+
""",
128+
# initial empty state
129+
"""
130+
--------
131+
| |
132+
|
133+
|
134+
|
135+
|
136+
-
137+
"""
138+
]
139+
return stages[tries]
140+
141+
142+
def main():
143+
word = get_word()
144+
play(word)
145+
while input("Play again ? (Y/N) ").upper() == "Y":
146+
word = get_word()
147+
play(word)
148+
149+
150+
if __name__ == "__main__" : #our program will run by running our script on the command line
151+
main()

0 commit comments

Comments
 (0)