def test_get_all_boards_png_three_games(self): chess_bot = Chess() board1 = chess.Board() board1.push_san("e4") board1.push_san("e5") game1 = Game() game1.color_schema = "blue" game1.board = board1 chess_bot.games.append(game1) board2 = chess.Board() board2.push_san("Nf3") board2.push_san("d6") game2 = Game() game2.color_schema = "wood" game2.board = board2 chess_bot.games.append(game2) board3 = chess.Board() board3.push_san("Nf3") game3 = Game() game3.color_schema = "green" game3.board = board3 chess_bot.games.append(game3) image_bytesio = asyncio.run(chess_bot.get_all_boards_png()) with open( os.path.join('tests', 'support', 'get_all_boards_png_three_games.png'), 'rb') as f: self.assertEqual(image_bytesio.read(), f.read())
def test_get_all_boards_png_one_game(self): chess_bot = Chess() board1 = chess.Board() board1.push_san("e4") board1.push_san("e5") board1.push_san("Bc4") game1 = Game() game1.color_schema = "green" game1.board = board1 chess_bot.games.append(game1) image_bytesio = asyncio.run(chess_bot.get_all_boards_png()) with open( os.path.join('tests', 'support', 'get_all_boards_png_one_game.png'), 'rb') as f: self.assertEqual(image_bytesio.read(), f.read())
def new_game(self, user1, user2, color_schema=None, cpu_level=None): player1, player2 = convert_users_to_players(user1, user2) current_players_pairs = map(lambda x: [x.player1, x.player2], self.games) given_players_pairs = [player1, player2] if given_players_pairs in current_players_pairs: raise GameAlreadyInProgress() game = Game() game.board = chess.Board() game.player1 = player1 game.player2 = player2 game.current_player = player1 game.result = game.board.result() game.color_schema = color_schema game.cpu_level = cpu_level self.games.append(game) return game
def build_animated_sequence_gif(self, game: Game, game_move: int, sequence: list) -> BytesIO: """ Builds an animated GIF for illustrating a given game's possible variation :param game: Game to be used for variation :type game: Game :param game_move: Number of game's move for the start of variation :type game_move: int :param sequence: Variation's sequence of move :type sequence: str[] :return: Animated gif's bytesIO :rtype: BytesIO """ game_moves = game.board.move_stack new_board = chess.Board() new_game = Game() new_game.board = new_board new_game.color_schema = game.color_schema for move in game_moves[:game_move]: new_board.push(move) first_gif_frame = Image.open(self.build_png_board(new_game)) gif_frames = [] for variant_move in sequence: variant_chess_move = self._parse_str_move(new_game, variant_move) if not variant_chess_move: return new_board.push(variant_chess_move) gif_frame = Image.open(self.build_png_board(new_game)) gif_frames.append(gif_frame) bytesio = BytesIO() first_gif_frame.save(bytesio, format='gif', save_all=True, append_images=gif_frames, duration=1000, loop=0) bytesio.seek(0) return bytesio
def build_puzzle(self, puzzle_dict): try: puzzle_id = puzzle_dict["data"]["id"] first_move = puzzle_dict["data"]["blunderMove"] fen = puzzle_dict["data"]["fenBefore"] correct_sequence = puzzle_dict["data"]["forcedLine"] board = Board(fen) board.push_san(first_move) game = Game() game.board = board game.color_schema = "default" self.puzzles[puzzle_id] = { "id": puzzle_id, "game": game, "correct_sequence": correct_sequence } return self.puzzles[puzzle_id] except KeyError: return {"error": "Missing required keys"} except: return {"error": "Invalid FEN or move"}