-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.py
54 lines (51 loc) · 1.83 KB
/
game.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
import discord
from discord.ext import commands # gets the bot commands archive
import asyncio
import random
from board import Board
import img
import helper
async def run(bot: commands.Bot, ctx: commands.Context, FEN: str, id):
b = Board(FEN)
if random.randint(0, 1) == 0:
b.white.setid(ctx.author.id)
b.black.setid(id)
else:
b.white.setid(id)
b.black.setid(ctx.author.id)
turn = 0
current_id = b.white.id
moves = []
b.switch_player()
while True:
if turn:
def check(message):
if current_id == message.author.id:
return helper.check_valid(message.content)
try:
message = await bot.wait_for('message', timeout=600.0, check=check)
if message.content == "resign":
await ctx.send(f"<@{current_id}> has resigned!")
return
else:
move = helper.process_move(message.content, b)
if not move:
await ctx.send("Invalid move, please try again.")
continue
else:
b.move(move)
except asyncio.TimeoutError:
await ctx.send("Game has timed out.")
return
result = b.advance_turn()
b.update_check(result)
turn += 1
current_id = b.white.id if b.rules["move"] == 'w' else b.black.id
img.draw_board(b, moves)
file_ = discord.File("./assets/RunningBoard.jpg", filename="board.png")
if result == '#':
winner = b.black.id if b.rules["move"] == 'w' else b.white.id
await ctx.send(f"<@{winner}> has won!", file=file_)
return
else:
await ctx.send(f"<@{current_id}>, it's your turn!", file=file_)