forked from ayushagarwal001/Hacktoberfest2022
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hangman.py
206 lines (163 loc) · 6.76 KB
/
hangman.py
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
import random
class Figure():
def __init__(self, name: str) -> None:
self.base = f"""
---------------
| |
|
|
|
|
^^^^^^^^^^^^^^^
\"{name} isn't present yet. Try to get all guesses correctly to not hurt {name} at all!\"
"""
self.hang1 = f"""
---------------
| |
| O
|
|
|
^^^^^^^^^^^^^^^
\"OOF! {name}'s head is here , that oughta hurt\"
"""
self.hang2 = f"""
---------------
| |
| O
| |
|
|
^^^^^^^^^^^^^^^
\"If you bring your head closer to the screen , you can hear {name}'s screams\"
"""
self.hang3 = f"""
---------------
| |
| O
| |\\
|
|
^^^^^^^^^^^^^^^
\"NOOOO! not {name}'s right hand , he needed it for a lot of stuff. lot of stuff.....\"
"""
self.hang4 = f"""
---------------
| |
| O
| /|\\
|
|
^^^^^^^^^^^^^^^
\"Welp , you can still try to save {name}'s legs.... walking is kind of a important thing you know\"
"""
self.hang5 = f"""
---------------
| |
| O
| /|\\
| /
|
^^^^^^^^^^^^^^^
\"You might as well let {name} die at this point....\"
"""
self.hang6 = f"""
---------------
| |
| O
| /|\\
| / \\
|
^^^^^^^^^^^^^^^
\"Remember, {name}'s blood is on YOUR HANDS!\"
"""
self.win = f"""
---------------
|
|
| O
| \\|/
| / \\
^^^^^^^^^^^^^^^
\"{name} IS ALIVE , HOOOORAAAYYYYYYY!!!!!!!!!!!\"
"""
self.hanglist = [self.base, self.hang1, self.hang2,
self.hang3, self.hang4, self.hang5]
def __iter__(self):
self.currenthang = 0
return self
def __next__(self):
if self.currenthang < len(self.hanglist):
x = self.hanglist[self.currenthang]
self.currenthang += 1
return x
raise StopIteration
def __str__(self) -> str:
return self.hanglist[self.currenthang]
class Word():
def __init__(self) -> None:
self.word_list = ["absence", "abuse", "account", "accident", "beneath", "bend", "benefit", "biology", "bitter", "candidate", "campaign", "camera", "capacity", "capture", "deaf", "daughter", "deal", "decorate", "dialogue", "economy", "finance", "educate", "efficient",
"supportive", "elderly", "flight", "folk", "flame", "frustration", "garbage", "gather", "gentle", "global", "hilarious", "intelligence", "jazz", "knife", "longevity", "momument", "nonsense", "nobody", "turmeric", "utilize", "sashimi", "reconfigure", "wheat", "yellowish", "zone"]
def random_word(self):
return random.choice(self.word_list)
def add_word(self, name):
if name not in self.word_list:
self.word_list.append(name)
def show_words(self):
print("\n---------------")
for i, j in enumerate(self.word_list):
print("{:>3} | {:<3}".format(i+1, j))
print("---------------\n")
if __name__ == "__main__":
game_on = True
print("\n"*100)
print("""
██╗░░██╗░█████╗░███╗░░██╗░██████╗░███╗░░░███╗░█████╗░███╗░░██╗
██║░░██║██╔══██╗████╗░██║██╔════╝░████╗░████║██╔══██╗████╗░██║
███████║███████║██╔██╗██║██║░░██╗░██╔████╔██║███████║██╔██╗██║
██╔══██║██╔══██║██║╚████║██║░░╚██╗██║╚██╔╝██║██╔══██║██║╚████║
██║░░██║██║░░██║██║░╚███║╚██████╔╝██║░╚═╝░██║██║░░██║██║░╚███║
╚═╝░░╚═╝╚═╝░░╚═╝╚═╝░░╚══╝░╚═════╝░╚═╝░░░░░╚═╝╚═╝░░╚═╝╚═╝░░╚══╝""")
print("""\nWelcome to Hangman. A simple game for kids that teaches children that saying the wrong word can lead to someone's DEATH.
The rules for playing this game are pretty much non existent.
You will be given a word at random and only shown the number of letters it consists,
you need to guess all the letters in the word in 7 or less number of tries.
""")
hangname = input(
"\nLet's start by entering a name for our hangman , to add a bit of \"emotional value\" : ").capitalize()
while game_on:
hangman = Figure(hangname)
hangiter = iter(hangman)
wordz = Word()
chosen = wordz.random_word()
blank = ["_"]*len(chosen)
correct_messages = iter(["First correct guess!\n", "Second correct guess! ,keep going man\n", "Third correct guess! , you are killing it man\n",
"Fourth correct guess! go go go\n", "Fift correct guess! , you are on fire\n", "Sixth correct guess! damn\n", "Seventh correct guess! when will this end?\n", "Eight correct guess! bruh how long is this word\n"])
print(hangman.__next__())
while "".join(blank) != chosen:
print(
f"Number of letters left to guessed = { blank.count('_') }")
print("".join(blank))
player_guess = input("\nPlayer enter your guess : ")
if player_guess in chosen and player_guess != "":
index_pos = [i for i, j in enumerate(
chosen) if player_guess == j]
for i in index_pos:
blank[i] = list(chosen)[i]
print(next(correct_messages))
else:
try:
print(hangiter.__next__())
except StopIteration:
print(hangman.hang6)
print("-----GAME OVER-----")
print("The word was :", chosen)
break
if "".join(blank) == chosen:
print(hangman.win)
print(
f"You got the correct answer {chosen} in {hangiter.currenthang-1} wrong guesse(s).")
cont = input("\nDo you want to play one more round? (y/n) : ")
if cont != "y":
break
print("\n"*100)