-
Notifications
You must be signed in to change notification settings - Fork 1
/
hangman.py
276 lines (252 loc) · 10.3 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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
import os
try:
import discord
from discord.ext import commands
except ImportError:
os.system("pip install discord.py")
try:
import discord
from discord.ext import commands
except:
print("You need python 3.7.9")
exit()
bot = commands.Bot(command_prefix="b.", self_bot=False, bot=True)
token = "token here
@bot.event
async def on_ready():
print("The Bot is online.")
await bot.change_presence(status=discord.Status.online)
await bot.change_presence(activity=discord.Game(name="b.helpMe for the help menu"))
@bot.command()
async def helpMe(ctx):
embed1 = discord.Embed(colour=discord.Colour.blue())
embed1.set_author(name="Commands:")
embed1.add_field(name=" b.play <word>", value="starts a game", inline=False)
embed1.add_field(name=" b.stop", value="stops the game", inline=False)
embed1.add_field(name=" b.guessWord <word>", value="to guess a word", inline=False)
embed1.add_field(name=" b.guess <letter>", value="to guess a letter", inline=False)
embed1.add_field(name="[!]", value="Warning", inline=False)
embed1.add_field(name="Developer: ", value="🔥𝕣𝕠𝕠𝕥𝕪🔥#8465", inline=False)
await ctx.send(embed=embed1)
"""
7 lives
"""
players = []
host = ""
word = ""
guessWord1 = ""
lives = []
alive = ":hearts:"
@bot.command()
async def play(ctx, Gword: str):
global host
global lives
global players
global word
global guessWord1
Gword = Gword.lower()
if host == "":
if Gword is not None and Gword != "":
await ctx.message.delete()
host = ctx.author
word = Gword
JustStr = ""
i = 0
while i < len(word):
JustStr += "-"
i += 1
guessWord1 = JustStr
lives = [":hearts:", ":hearts:", ":hearts:", ":hearts:", ":hearts:", ":hearts:",
":hearts:"]
await ctx.author.send(guessWord1)
l = ""
for live in lives:
l += " " + live
await ctx.send(l)
await ctx.send("**Host** >> " + str(host))
else:
await ctx.send("**[!]**You have to give a word and the message will be deleted.")
else:
await ctx.send("**[!]**Finish this game.")
@bot.command()
async def stop(ctx):
global host
global lives
global players
global word
global guessWord1
if ctx.author == host:
host = ""
lives.clear()
players.clear()
word = ""
guessWord1 = ""
await ctx.send("The game just stopped.")
else:
await ctx.send("**[!]**You need to be the host of the game.")
@bot.command()
async def guess(ctx, letter: str):
global host
global players
global lives
global word
global guessWord1
letter = letter.lower()
if ctx.author == host:
await ctx.send("**[!]**You cant be a guesser!")
else:
if word == "":
await ctx.send("**[!]**There is no game started.")
else:
if letter is None or letter == "":
await ctx.send("**[!]**You have to give a letter you think it could be in the word.")
else:
if ctx.author not in players:
players.append(ctx.author)
if letter in guessWord1:
await ctx.send("**[!]** **" + letter + "** is already guessed.")
else:
if letter in word:
# New letter found
# pypi
# p-p-
# -i--
# ---y
numOfLetters = 0
for char in word:
if char == letter:
numOfLetters += 1
if numOfLetters > 0:
# -------
# p
# p------
# t
# p--t---
newGuessWord = ""
if numOfLetters == 1:
index = word.index(letter)
indexNow = 0
for char in guessWord1:
if indexNow == index:
newGuessWord += letter
else:
newGuessWord += char
indexNow += 1
if numOfLetters > 1:
lotIndex = []
index2 = -1
for letters in word:
if letters == letter:
index2 += 1
lotIndex.append(index2)
indexNow = -1
for char in guessWord1:
if indexNow in lotIndex:
newGuessWord += letter
else:
newGuessWord += char
indexNow += 1
guessWord1 = newGuessWord
l = ""
for live in lives:
l += " " + live
await ctx.send(guessWord1)
if guessWord1 == word:
await ctx.send("<@"+str(ctx.author.id)+"> **IS** the **WINNER**")
await ctx.send("**Host** >> " + str(host))
for player in players:
await ctx.send("**Guesser** >> " + str(player))
host = ""
lives.clear()
players.clear()
word = ""
guessWord1 = ""
else:
await ctx.send(l)
await ctx.send("There IS **" + letter + "** in the word.")
await ctx.send("**Host** >> " + str(host))
for player in players:
await ctx.send("**Guesser** >> " + str(player))
else:
# This letter is not in the word
# remove one live
if len(lives) == 1 or len(lives) == 0:
await ctx.send(host+" **IS** the **WINNER**")
for player in players:
await ctx.send("**Guesser** >> " + str(player))
host = ""
lives.clear()
players.clear()
word = ""
guessWord1 = ""
else:
lives.pop(0)
l = ""
for live in lives:
l += " " + live
await ctx.send(guessWord1)
await ctx.send(l)
await ctx.send("There is NO **" + letter + "** in the word.")
await ctx.send("**Host** >> " + str(host))
for player in players:
await ctx.send("**Guesser** >> " + str(player))
@bot.command()
async def guessWord(ctx, wordG: str):
global word
global guessWord1
global players
global lives
global alive
global host
wordG = wordG.lower()
if ctx.author == host:
await ctx.send("**[!]**You cant be a guesser!")
else:
if word is None:
await ctx.send("**[!]**There is no game started.")
else:
if wordG is None or word == "":
await ctx.send("**[!]**You have to give a word you think it is.")
else:
if ctx.author not in players:
players.append(ctx.author)
if word == wordG:
# the guesser wins
host = None
lives.clear()
players.clear()
word = None
guessWord1 = None
await ctx.send("That is **CORRECT**!")
await ctx.send(str(ctx.author) + " is the winner!")
await ctx.author.send(str(ctx.author) + " just guessed the word!")
else:
livesLeft = 0
for live in lives:
if live == alive:
livesLeft += 1
if livesLeft == 1 or livesLeft == 0:
# the guessers lose
host = None
lives.clear()
players.clear()
word = None
guessWord1 = None
await ctx.send("You ran out of lives! Your game just finished.")
await ctx.send("The **HOST** is the **WINNER** > " + str(host))
await ctx.author.send("You just won the game!")
await ctx.author.send("**Word** > " + str(word))
else:
await ctx.send("Nine try, but that is **INCORRECT**! Sorry.")
lives.pop(0)
l = ""
for live in lives:
l += " " + live
await ctx.send(l)
await ctx.send("**Host** >> " + str(host))
for player in players:
await ctx.send("**Guesser** >> " + str(player))
try:
bot.run(token)
except:
print("The bot can't staty or the token is invalid")