-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGames.py
555 lines (539 loc) · 28 KB
/
Games.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
import discord
import random
from discord import Forbidden
from random import choice,randint
from discord.ext import commands
green = ":green_square:"
blue = ":blue_square:"
red = ":red_square:"
white = ":white_large_square:"
black = ":black_large_square:"
class Games(commands.Cog):
def __init__(self, bot):
self.bot = bot
@staticmethod
def get_id(ctx: commands.Context) -> int:
"""Get a context's id."""
if ctx.guild:
return ctx.guild.id
return ctx.channel.id
def launch(self) -> None:
"""Launch the bot."""
self.run(self.token)
"""Play battleship !!"""
def __init__(self, bot):
self.bot = bot
self.boat_size = [(5, "Carrier"), (4, "Battleship"), (3, "Cruiser"), (3, "Submarine"), (2, "Destroyer")]
self.column = list("abcdefghij")
def j2_boats(self):
j2, boats = [[0]*10 for _ in range(10)], []
for i, k in self.boat_size:
test = True
while test:
test = False
a, b = [randint(0, 9), randint(0, 9)], randint(0, 1)
if b:
if a[1] <= 10 - i:
if a[1] != 0:
if j2[a[0]][a[1] - 1] != 0:
test = True
if a[0] != 0:
if j2[a[0] - 1][a[1] - 1] != 0:
test = True
if a[0] != 9:
if j2[a[0] + 1][a[1] - 1] != 0:
test = True
if a[1] + i < 10:
if j2[a[0]][a[1] + i] != 0:
test = True
if a[0] != 9:
if j2[a[0] + 1][a[1] + i] != 0:
test = True
if a[0] != 0:
if j2[a[0] - 1][a[1] + i] != 0:
test = True
if not test:
if a[0] == 0:
for j in range(i):
if j2[0][a[1] + j] != 0 or j2[1][a[1] + j] != 0:
test = True
elif a[0] == 9:
for j in range(i):
if j2[9][a[1] + j] != 0 or j2[8][a[1] + j] != 0:
test = True
else:
for j in range(i):
if j2[a[0]][a[1] + j] != 0 or j2[a[0] + 1][a[1] + j] != 0 or j2[a[0] - 1][a[1] + j] != 0:
test = True
else:
test = True
if not test:
boat = []
for j in range(i):
j2[a[0]][a[1] + j] = 1
boat.append((a[0], a[1] + j))
boats.append(boat)
else:
if a[0] <= 10 - i:
if a[0] != 0:
if j2[a[0] - 1][a[1]] != 0:
test = True
if a[1] != 0:
if j2[a[0] - 1][a[1] - 1] != 0:
test = True
if a[1] != 9:
if j2[a[0] - 1][a[1] + 1] != 0:
test = True
if a[0] + i < 10:
if j2[a[0] + i][a[1]] != 0:
test = True
if a[1] != 9:
if j2[a[0] + i][a[1] + 1] != 0:
test = True
if a[1] != 0:
if j2[a[0] + i][a[1] - 1] != 0:
test = True
if not test:
if a[1] == 0:
for j in range(i):
if j2[a[0] + j][0] != 0 or j2[a[0] + j][1] != 0:
test = True
elif a[1] == 9:
for j in range(i):
if j2[a[0] + j][9] != 0 or j2[a[0] + j][8] != 0:
test = True
else:
for j in range(i):
if j2[a[0] + j][a[1]] != 0 or j2[a[0] + j][a[1] + 1] != 0 or j2[a[0] + j][a[1] - 1] != 0:
test = True
else:
test = True
if not test:
boat = []
for j in range(i):
j2[a[0] + j][a[1]] = 1
boat.append((a[0] + j, a[1]))
boats.append(boat)
return (boats, j2)
@staticmethod
def grille_de_tir(d,f):
e=[]
for i in range(10):
for j in range(round(10/d)):
e.append((i, d*j + i%d))
for i in f:
if i in e:
e.remove(i)
return e
def grider(self, grid, player = True):
return '\n'.join([". 1 2 3 4 5 6 7 8 9 10"] + [''.join([green if (grid[i][j] == 1 and player) else white if grid[i][j] == -1 else red if grid[i][j] == -2 else black if grid[i][j] == -3 else blue for j in range(len(grid[i]))] + [" " + self.column[i].upper()]) for i in range(len(grid))])
@staticmethod
def check(coups, b, not_possible):
for i in b:
if i[0] != 0:
not_possible.append((i[0] - 1, i[1]))
if (i[0]-1, i[1]) in coups:
coups.remove((i[0] - 1, i[1]))
if i[1] != 0:
not_possible.append((i[0] - 1, i[1] - 1))
if (i[0] - 1, i[1] - 1) in coups:
coups.remove((i[0] - 1, i[1] - 1))
if i[1] != 9:
not_possible.append((i[0] - 1, i[1] + 1))
if (i[0] - 1, i[1] + 1) in coups:
coups.remove((i[0] - 1, i[1] + 1))
if i[0] != 9:
not_possible.append((i[0] + 1, i[1]))
if (i[0]+1,i[1]) in coups:
coups.remove((i[0] + 1, i[1]))
if i[1] != 0:
not_possible.append((i[0] + 1, i[1] - 1))
if (i[0] + 1, i[1] - 1) in coups:
coups.remove((i[0] + 1, i[1] - 1))
if i[1] != 9:
not_possible.append((i[0] + 1, i[1] + 1))
if (i[0] + 1, i[1] + 1) in coups:
coups.remove((i[0] + 1, i[1] + 1))
return coups, not_possible
@commands.command(aliases=["bs"])
@commands.max_concurrency(1, commands.BucketType.guild)
async def battleship(self, ctx):
"""> Play Battleship against me"""
def check(m):
return m.author == ctx.author and m.channel == ctx.channel and (2 < len(m.content) < 5 or m.content.lower() == "quit")
player_grid = [[0]*10 for _ in range(10)]
player_boats = []
player_state = [1]*len(self.boat_size)
await ctx.send("Welcome to battleship !\nPlease place your boats sending messages formatted like this : [Column][Row][Way]\nColumn : A-J\nRow : 1-10\nWay : h (horizontal) / v (vertical)\n\nYou can send `quit` any time to quit the game")
for size,name in self.boat_size:
await ctx.send(self.grider(player_grid))
await ctx.send(f"Place your {name} (size : {size})")
invalid = True
while invalid:
msg = await self.bot.wait_for('message', check = check)
if msg.content.lower() == "quit":
return await ctx.send("Maybe we can play together another time")
if msg.content.lower()[0] not in self.column or not msg.content[1:-1].isdigit():
await ctx.send("Please input a message correctly formatted")
elif not 0 < int(msg.content[1:-1]) < 11:
await ctx.send("Please input a message correctly formatted")
elif msg.content.lower()[-1] == 'v':
origin = (self.column.index(msg.content.lower()[0]), int(msg.content[1:-1]) - 1)
if origin[0] + size > 9:
await ctx.send("The boat is too long to be placed in such a position")
else:
invalid = False
for i in range(size):
if i + origin[0] > 0:
if origin[1] > 0:
if player_grid[i + origin[0] - 1][origin[1] - 1] != 0:
invalid = True
break
if player_grid[i + origin[0] - 1][origin[1]] != 0:
invalid = True
break
if origin[1] < 9:
if player_grid[i + origin[0] - 1][origin[1] + 1] != 0:
invalid = True
break
if origin[1] > 0:
if player_grid[i + origin[0]][origin[1] - 1] != 0:
invalid = True
break
if player_grid[i + origin[0]][origin[1]] != 0:
invalid = True
break
if origin[1] < 9:
if player_grid[i + origin[0]][origin[1] + 1] != 0:
invalid = True
break
if i + origin[0] < 9:
if origin[1] > 0:
if player_grid[i + origin[0] + 1][origin[1] - 1] != 0:
invalid = True
break
if player_grid[i + origin[0] + 1][origin[1]] != 0:
invalid = True
break
if origin[1] < 9:
if player_grid[i + origin[0] + 1][origin[1] + 1] != 0:
invalid = True
break
if not invalid:
player_boats.append([(origin[0] + i, origin[1]) for i in range(size)])
for i in range(size):
player_grid[origin[0] + i][origin[1]] = 1
else:
await ctx.send("There's already something there")
elif msg.content.lower()[-1] == "h":
origin = (self.column.index(msg.content.lower()[0]), int(msg.content[1:-1]) - 1)
if origin[1]+size>10:
await ctx.send("The boat is too long to be placed in such a position")
else:
invalid = False
for i in range(size):
if i + origin[1] > 0:
if origin[0] > 0:
if player_grid[origin[0] - 1][origin[1] + i - 1] != 0:
invalid = True
break
if player_grid[origin[0]][origin[1] + i - 1] != 0:
invalid = True
break
if origin[0] < 9:
if player_grid[origin[0] + 1][origin[1] + i - 1] != 0:
invalid = True
break
if origin[0] > 0:
if player_grid[origin[0] - 1][origin[1] + i] != 0:
invalid = True
break
if player_grid[origin[0]][origin[1] + i] != 0:
invalid = True
break
if origin[0] < 9:
if player_grid[origin[0] + 1][origin[1] + i] != 0:
invalid = True
break
if i + origin[1] < 9:
if origin[0] > 0:
if player_grid[origin[0] - 1][origin[1] + i + 1] != 0:
invalid = True
break
if player_grid[origin[0]][origin[1] + i + 1] != 0:
invalid = True
break
if origin[0] < 9:
if player_grid[origin[0] + 1][origin[1] + i + 1] != 0:
invalid = True
break
if not invalid:
player_boats.append([(origin[0],origin[1]+i) for i in range(size)])
for i in range(size):
player_grid[origin[0]][origin[1] + i] = 1
else:
await ctx.send("There's already something there")
else:
await ctx.send("Please input a correctly formatted message")
enemy_boats, enemy_grid = self.j2_boats()
dist = 2
must_fire = []
not_possible = []
enemy_state = [1]*len(self.boat_size)
enemy_fire = self.grille_de_tir(dist,not_possible)
def check(m):
return m.author == ctx.author and m.channel == ctx.channel and (1 < len(m.content) < 4 or m.content.lower() == "quit")
while True:
await ctx.send(self.grider(player_grid))
await ctx.send(self.grider(enemy_grid, False))
await ctx.send("You can now fire upon me !")
player_turn = True
while player_turn:
msg = await self.bot.wait_for("message", check = check)
if msg.content.lower() == "quit":
return await ctx.send("Maybe we can play together another day")
if msg.content.lower()[0] not in self.column or not msg.content[1:].isdigit():
await ctx.send("Please input a message correctly formatted")
elif not 0 < int(msg.content[1:]) < 11:
await ctx.send("Please input a message correctly formatted")
else:
hit = (self.column.index(msg.content.lower()[0]), int(msg.content[1:]) - 1)
if enemy_grid[hit[0]][hit[1]] >= 0:
player_turn = False
enemy_grid[hit[0]][hit[1]] -= 1 + 2*enemy_grid[hit[0]][hit[1]]
for b in enemy_boats:
if (hit[0],hit[1]) in b:
t = True
for c in b:
if enemy_grid[c[0]][c[1]] == 1:
t = False
if t:
await ctx.send(f"You sunk my {self.boat_size[enemy_boats.index(b)][1]} !")
for j in b:
enemy_grid[j[0]][j[1]] =- 3
enemy_state[enemy_boats.index(b)] = 0
if sum(enemy_state) == 0:
return await ctx.send("You won !!")
break
else:
await ctx.send("You cannot fire twice in the same place !")
if must_fire == []:
fire = choice(enemy_fire)
enemy_fire.remove(fire)
not_possible.append(fire)
await ctx.send(f"{self.column[fire[0]].upper()}{fire[1] + 1}")
if player_grid[fire[0]][fire[1]]:
player_grid[fire[0]][fire[1]] = -2
must_fire.append(1)
must_fire.append(fire)
if fire[0] != 0:
must_fire.append((fire[0]-1, fire[1]))
if fire[0] != 9:
must_fire.append((fire[0] + 1, fire[1]))
if fire[1] != 0:
must_fire.append((fire[0], fire[1]-1))
if fire[1] != 9:
must_fire.append((fire[0], fire[1]+1))
else:
player_grid[fire[0]][fire[1]] = -1
elif must_fire[0] == 1:
fire = choice(must_fire[2:])
while fire in not_possible:
fire = choice(must_fire[2:])
await ctx.send(f"{self.column[fire[0]].upper()}{fire[1]+1}")
not_possible.append(fire)
must_fire.remove(fire)
if player_grid[fire[0]][fire[1]]:
player_grid[fire[0]][fire[1]] =- 2
original = must_fire[1]
must_fire.clear()
sunk = True
for i in range(5):
if (fire[0], fire[1]) in player_boats[i]:
test = True
for c in player_boats[i]:
if player_grid[c[0]][c[1]] == 1:
test = False
if test:
player_state[i] = 0
if i == 4:
dist = 3
enemy_fire = self.grille_de_tir(dist, not_possible)
if dist == 3 and player_state[3] == player_state[2] == 0:
dist = 4
enemy_fire = self.grille_de_tir(dist, not_possible)
if dist == 4 and not player_state[1]:
dist = 5
enemy_fire = self.grille_de_tir(dist, not_possible)
sunk = False
enemy_fire,not_possible=self.check(enemy_fire, player_boats[i], not_possible)
await ctx.send(f"I sunk your {self.boat_size[i][1]}")
for j in player_boats[i]:
player_grid[j[0]][j[1]] = -3
if sum(player_state) == 0:
return await ctx.send("I won !")
if sunk:
must_fire.append(2)
must_fire.append(original)
if fire[1] == original[1]:
if fire[0] == 0:
must_fire[0] = 3
must_fire.append((original[0] + 1, original[1]))
elif fire[0] == 9:
must_fire[0] = 3
must_fire.append((original[0] - 1, original[1]))
elif original[0] == 0:
must_fire[0] = 3
must_fire.append((fire[0] + 1, original[1]))
elif original[0] == 9:
must_fire[0] = 3
must_fire.append((fire[0] - 1, original[1]))
elif fire[0] == original[0] + 1:
must_fire.append((fire[0] + 1, original[1]))
must_fire.append((original[0] - 1, original[1]))
else:
must_fire.append((fire[0]-1, original[1]))
must_fire.append((original[0] + 1, original[1]))
else:
if fire[1] == 0:
must_fire[0] = 3
must_fire.append((original[0], original[1] + 1))
elif fire[1] == 9:
must_fire[0] = 3
must_fire.append((original[0], original[1] - 1))
elif original[1] == 0:
must_fire[0] = 3
must_fire.append((original[0], fire[1] + 1))
elif original[1] == 9:
must_fire[0] = 3
must_fire.append((original[1], fire[1] - 1))
elif fire[1] == original[1] + 1:
must_fire.append((original[0], fire[1] + 1))
must_fire.append((original[0], original[1] - 1))
else:
must_fire.append((original[0], fire[1] - 1))
must_fire.append((original[0], original[1] + 1))
else:
player_grid[fire[0]][fire[1]] =- 1
elif must_fire[0] == 2:
fire = choice(must_fire[2:])
while fire in not_possible:
fire = choice(must_fire[2:])
await ctx.send(f"{self.column[fire[0]].upper()}{fire[1]+1}")
must_fire.remove(fire)
not_possible.append(fire)
if fire in enemy_fire:
enemy_fire.remove(fire)
if player_grid[fire[0]][fire[1]]:
player_grid[fire[0]][fire[1]] = -2
for i in range(5):
if (fire[0], fire[1]) in player_boats[i]:
test = True
for c in player_boats[i]:
if player_grid[c[0]][c[1]] == 1:
test = False
if test:
player_state[i] = 0
if i == 4:
dist = 3
enemy_fire = self.grille_de_tir(dist, not_possible)
if dist == 3 and player_state[3] == player_state[2] == 0:
dist = 4
enemy_fire = self.grille_de_tir(dist, not_possible)
if dist == 4 and not player_state[1]:
dist = 5
enemy_fire = self.grille_de_tir(dist, not_possible)
enemy_fire, not_possible = self.check(enemy_fire, player_boats[i], not_possible)
await ctx.send(f"I sunk your {self.boat_size[i][1]}")
must_fire.clear()
for j in player_boats[i]:
player_grid[j[0]][j[1]] = -3
if sum(player_state) == 0:
return await ctx.send("I won !")
else:
if fire[0]<must_fire[1][0]:
if fire[0] == 0:
must_fire[0] = 3
else:
must_fire.append((fire[0] - 1, fire[1]))
elif fire[0] > must_fire[1][0]:
if fire[0] == 0:
must_fire[0] = 3
else:
must_fire.append((fire[0] + 1, fire[1]))
elif fire[1] < must_fire[1][1]:
if fire[1] == 0:
must_fire[0] = 3
else:
must_fire.append((fire[0], fire[1] - 1))
elif fire[1] > must_fire[1][1]:
if fire[0] == 0:
must_fire[0] = 3
else:
must_fire.append((fire[0], fire[1] + 1))
else:
player_grid[fire[0]][fire[1]] = -1
must_fire[0] = 3
elif must_fire[0] == 3:
fire = must_fire[2]
await ctx.send(f"{self.column[fire[0]].upper()}{fire[1] + 1}")
not_possible.append(fire)
if fire in enemy_fire:
enemy_fire.remove(fire)
player_grid[fire[0]][fire[1]] = -2
for i in range(5):
if (fire[0], fire[1]) in player_boats[i]:
test = True
for c in player_boats[i]:
if player_grid[c[0]][c[1]] == 1:
test = False
if test:
player_state[i] = 0
if i == 4:
dist = 3
enemy_fire = self.grille_de_tir(dist, not_possible)
if dist == 3 and player_state[3] == player_state[2] == 0:
dist = 4
enemy_fire = self.grille_de_tir(dist, not_possible)
if dist == 4 and not player_state[1]:
dist = 5
enemy_fire = self.grille_de_tir(dist, not_possible)
enemy_fire, not_possible = self.check(enemy_fire, player_boats[i], not_possible)
must_fire.clear()
await ctx.send(f"I sunk your {self.boat_size[i][1]}")
for j in player_boats[i]:
player_grid[j[0]][j[1]] = -3
if sum(player_state) == 0:
return await ctx.send("I won !")
if must_fire != []:
must_fire.remove(fire)
if fire[0] < must_fire[1][0]:
must_fire.append((fire[0] - 1, fire[1]))
elif fire[0] > must_fire[1][0]:
must_fire.append((fire[0]+1, fire[1]))
elif fire[1] < must_fire[1][1]:
must_fire.append((fire[0], fire[1] - 1))
elif fire[1] > must_fire[1][1]:
must_fire.append((fire[0], fire[1] + 1))
@commands.command(aliases=["slots"])
@commands.guild_only()
async def slot(self, ctx):
"""> Play slot machine game"""
emojis = [":apple:", ":tangerine:", ":pear:", ":lemon:", ":watermelon:", ":grapes:", ":strawberry:", ":cherries:"]
a = random.choice(emojis)
b = random.choice(emojis)
c = random.choice(emojis)
slotmachine = f"{a} {b} {c}"
if (a == b == c):
name=":tada: Congrats you won! :tada:"
value=f"{slotmachine}"
else:
name="You lost"
value=f"{slotmachine}"
embed = discord.Embed(title="Slotmachine", colour=ctx.author.colour)
embed.add_field(name=name, value=f"> {value}")
embed.set_footer(icon_url=ctx.author.avatar.url, text=ctx.author.name)
#embed.set_footer()
await ctx.send(embed=embed)
async def setup(bot):
await bot.add_cog(Games(bot))