def event_menu_game_new(self): dialog = newgamedialog.NewGameDialog() Player = dialog.result self.players[const.WHITE] = Player[const.WHITE](self, const.WHITE) self.players[const.BLACK] = Player[const.BLACK](self, const.BLACK) self.game = chessgame.ChessGame() self.gui.gameDisplay.game = self.game self.update()
def __init__(self, master): self.master = master self.game = chessgame.ChessGame() self.history = [] #the history which is shown in the listbox self.gui = gui.Gui(self) self.gui.gameDisplay.game = self.game self.players = [None, None] self.players[const.WHITE] = players.Human(self, const.WHITE) self.players[const.BLACK] = players.AI(self, const.BLACK) self.game.setBoard(mat.TESTBOARD) self.master.bind("<<turn_complete>>", self.turnComplete)
async def challenge(context, white: Member): timed_out = False black = context.author await context.send('%s, %s has challenged you to a chess game!' % (white.mention, black.mention)) with open('users.json') as f: users = json.load(f) if str(white.id) not in users: await update_data(users, white) elif str(black.id) not in users: await update_data(users, black) white_rating = trueskill.Rating(**users[str(white.id)]['trueskill']) black_rating = trueskill.Rating(**users[str(black.id)]['trueskill']) chess_game = chessgame.ChessGame() file = chess_game.get_png(chessgame.chess.WHITE) file = io.BytesIO(file) file = File(file, filename='board.png') await context.send('Board:', file=file) while True: end = False # White's move while True: await context.send('%s, please enter your move in UCI format (eg. e2e4)' % white.mention) try: move_str = await client.wait_for('message', check=lambda m: (m.author == white and m.channel == context.channel), timeout=300) except asyncio.TimeoutError: end = True timed_out = True break if move_str.content.lower() == 'end': end = True break try: chess_game.player_move(move_str.content.lower()) break except chessgame.InvalidMoveException as e: await context.send(str(e)) if end: white_ended = True break await context.send(chess_game.generate_move_digest(white.display_name)) if chess_game.is_finished(): break # Black's move file = chess_game.get_png(chessgame.chess.BLACK) file = io.BytesIO(file) file = File(file, filename='board.png') await context.send('Board:', file=file) if chess_game.check(): await context.send('Black is in check!') while True: await context.send('%s, please enter your move in UCI format (eg. e2e4)' % black.mention) try: move_str = await client.wait_for('message', check=lambda m: (m.author == black and m.channel == context.channel), timeout=300) except asyncio.TimeoutError: end = True timed_out = True break if move_str.content.lower() == 'end': end = True break try: chess_game.player_move(move_str.content.lower()) break except chessgame.InvalidMoveException as e: await context.send(str(e)) if end: black_ended = True break await context.send(chess_game.generate_move_digest(black.display_name)) if chess_game.is_finished(): break file = chess_game.get_png(chessgame.chess.WHITE) file = io.BytesIO(file) file = File(file, filename='board.png') await context.send('Board:', file=file) if chess_game.result() == '1-0' or black_ended: white_rating, black_rating = trueskill.rate_1vs1(white_rating, black_rating) elif chess_game.result() == '0-1' or white_ended: black_rating, white_rating = trueskill.rate_1vs1(black_rating, white_rating) with open('users.json') as f: users = json.load(f) users[str(white.id)]['trueskill'] = {'mu': white_rating.mu, 'sigma': white_rating.sigma} users[str(black.id)]['trueskill'] = {'mu': black_rating.mu, 'sigma': black_rating.sigma} if timed_out: await context.send('Game timed out. Next time please make a move within 5 minutes.') date_string = f"{datetime.date.today():%Y.%m.%d}" pgn = chess_game.get_pgn('Chess Game', 'Discord', date_string, white.display_name, black.display_name) embed = Embed(title="Chess Game Results", colour=Colour(0xff00), description="```%s```" % pgn) embed.set_author(name="FionaBot", icon_url="https://tinyurl.com/y8p7a8px") embed.add_field(name="White", value='%s\nRating: %0.3f' % (white.display_name, white_rating.mu)) embed.add_field(name="Black", value='%s\nRating: %0.3f' % (black.display_name, black_rating.mu)) embed.add_field(name="Final Score", value=chess_game.result()) await context.send(embed=embed) chess_game.engine.close()
async def black(context, easymode: bool = False): """ Command to start a new game of chess vs AI as black :param context: Command context :return: """ timed_out = False user = context.message.author chess_game = chessgame.ChessGame(difficulty=not easymode) await context.send('Starting new game as black.') file = chess_game.get_png(chessgame.chess.BLACK) file = io.BytesIO(file) file = File(file, filename='board.png') await context.send('Board:', file=file) while True: end = False if chess_game.check(): await context.send('White is in check!') chess_game.ai_move() await context.send(chess_game.generate_move_digest('FionaBot')) file = chess_game.get_png(chessgame.chess.BLACK) file = io.BytesIO(file) file = File(file, filename='board.png') await context.send('Board:', file=file) if chess_game.is_finished(): break if chess_game.check(): await context.send('Black is in check!') while True: await context.send('Please enter your move in UCI format (eg. e2e4)') try: movestr = await client.wait_for('message', check=lambda m: ( m.author == context.author and m.channel == context.channel), timeout=300) except asyncio.TimeoutError: end = True timed_out = True break match = regex.search(UCI_REGEX, movestr.content.lower()) if movestr.content.lower() == 'end': end = True break elif match != None: try: chess_game.player_move(movestr.content.lower()) break except chessgame.InvalidMoveException as e: await context.send(str(e)) if end: break await context.send(chess_game.generate_move_digest(user.display_name)) file = chess_game.get_png(chessgame.chess.BLACK) file = io.BytesIO(file) file = File(file, filename='board.png') await context.send('Board:', file=file) if chess_game.is_finished(): break if timed_out: await context.send('Game timed out. Next time please make a move within 5 minutes.') date_string = f"{datetime.date.today():%Y.%m.%d}" pgn = chess_game.get_pgn('Chess Game', 'Discord', date_string, user.display_name, "FionaBot") embed = Embed(title="Chess Game Results", colour=Colour(0xff00), description="```%s```" % pgn) embed.set_author(name="FionaBot", icon_url="https://tinyurl.com/y8p7a8px") embed.add_field(name="Human Player: Black", value=context.author.display_name) embed.add_field(name="Bot Player: White", value="FionaBot") embed.add_field(name="Final Score", value=chess_game.result()) await context.send(embed=embed) chess_game.engine.close()