-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
231 lines (209 loc) · 5.97 KB
/
main.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
import discord
from discord.ext import commands
import random
game = "hangman"
client = commands.Bot(command_prefix='/')
file = open('words.txt', 'r')
list_of_words = []
for line in file:
list_of_words.append(line.strip())
@client.event
async def on_ready():
print('ready')
@client.command()
async def online(ctx):
await ctx.send("online")
# --- hangman ---
async def update_keyword(keyword_hidden, keyword, msg):
tmp_keyword_string = keyword_hidden
for i in range(len(keyword)):
if msg == keyword[i]:
tmp_keyword_string[i] = keyword[i]
else:
tmp_keyword_string[i] = keyword_hidden[i]
return ''.join(str(e) for e in tmp_keyword_string) + ' (' + str(len(keyword)) + ')'
async def update_graph(attempt):
if attempt == 0:
return '''> > gallows combs:
>
>
>
>
>
>
> '''
elif attempt == 1:
return '''> > gallows combs:
>
>
>
>
>
> /
> '''
elif attempt == 2:
return '''> > gallows combs:
>
>
>
>
>
> / \
> '''
elif attempt == 3:
return '''> > gallows combs:
>
> |
> |
> |
> |
> /_\
> '''
elif attempt == 4:
return '''> >gallows combs:
> \_\_\_\_\_\_
> |
> |
> |
> |
> /_\
> '''
elif attempt == 5:
return '''> > gallows combs:
> \_\_\_\_\_\_
> |/
> |
> |
> |
> /_\
> '''
elif attempt == 6:
return '''> > gallows combs:
> \_\_\_\_\_\_
> |/ |
> |
> |
> |
> /_\
> '''
elif attempt == 7:
return '''> > gallows combs:
> \_\_\_\_\_\_
> |/ |
> | O
> |
> |
> /_\
> '''
elif attempt == 8:
return '''> > gallows combs:
> \_\_\_\_\_\_
> |/ |
> | O
> | |
> |
> /_\
> '''
elif attempt == 9:
return '''> > gallows combs:
> \_\_\_\_\_\_
> |/ |
> | O
> | /|
> |
> /_\
> '''
elif attempt == 10:
return '''> > gallows combs:
> \_\_\_\_\_\_
> |/ |
> | O
> | /|\\
> |
> /_\
> '''
elif attempt == 11:
return '''> > gallows combs:
> \_\_\_\_\_\_
> |/ |
> | O
> | /|\\
> | /
> /_\
> '''
elif attempt == 12:
return '''> > gallows combs:
> \_\_\_\_\_\_
> |/ |
> | O
> | /|\\
> | / \\
> /_\
> '''
async def check_game_state(keyword_hidden, keyword, attempt):
if attempt < 12 and keyword == keyword_hidden:
print('1')
return True, False
elif attempt >= 12 and keyword != keyword_hidden:
print('2')
return False, True
else:
print('3')
return False, False
def check(m):
return len(m.content) == 1
async def hangmen(ctx):
keyword_as_string = list_of_words[random.randint(0, len(list_of_words))].lower()
keyword = list(keyword_as_string)
keyword_hidden = []
for i in range(len(keyword)):
keyword_hidden.append('\_')
attempt = 0
used_letters = ['\n> ', 'Use letters: ']
pic_str = await update_graph(attempt)
keyword_string = ''.join(str(e) for e in keyword_hidden) + f' ({str(len(keyword))})'
await ctx.send(f'''
> > Rules:
> - always just one letter, even if you already know the word
> no extra attempts are counted
> - lower case only
> - no numbers
> - instead of ö, ü, ä always ae, ue, oe\n {pic_str} {keyword_string}
there are 58109 possible answers''')
for i in range(len(keyword_as_string) + 12):
win, lose = await check_game_state(keyword_hidden, keyword, attempt)
print(keyword)
if win:
message = f'> GG, won with {str(attempt)} failed attempts!'
break
elif lose:
message = f'> Nice try, unfortunately not good enough, the word was: {keyword_as_string}'
await ctx.send(message)
break
else:
msg = await client.wait_for('message', check=check)
msg_str = msg.content.lower()
if msg_str.isalpha():
if msg_str in used_letters:
await ctx.send('>The letter has already been used.')
else:
if msg_str not in keyword:
attempt += 1
pic_str = await update_graph(attempt)
elif msg_str in keyword:
pic_str = await update_graph(attempt)
keyword_string = await update_keyword(keyword_hidden, keyword, msg_str)
used_letters.append(msg_str)
used_letters_string = ''.join(str(e) for e in used_letters)
await ctx.send(pic_str + keyword_string + used_letters_string)
else:
await ctx.send('> There are no digits or special characters.')
# --- commands ---
@client.command(pass_context=True, aliases=['gr', 'gallows combs'])
async def hangman(ctx):
await hangmen(ctx)
# --- events ---
@client.event
async def on_message(message):
await client.process_commands(message)
client.run("bot token")