def test_make_move_legal_move_pve(self): board = chess.Board( 'rn2kb1r/pp1qpppp/2ppbn2/1B6/3PP3/2N2N2/PPP2PPP/R1BQK2R w KQkq - 0 6' ) game = Game() game.board = board game.player1 = FakeDiscordUser(id=1) game.player2 = FakeDiscordUser(id=2) game.current_player = game.player1 game.cpu_level = 0 chess_bot = Chess() chess_bot.games.append(game) chess_bot.stockfish_limit['time'] = 1 result = asyncio.run(chess_bot.make_move(game, 'b5d3')) self.assertIsInstance(result, Game) if chess_bot.is_stockfish_enabled(): self.assertEqual(len(result.board.move_stack), 2) self.assertEqual(result.current_player, game.player1) updated_chess_game = self.db_session.query(ChessGame).filter_by( player1_id=result.player1.id).first() updated_game_from_db = Game.from_chess_game_model(updated_chess_game) self.assertEqual(updated_game_from_db.board.move_stack, result.board.move_stack)
def test_is_pve_game_stockfish_disabled(self): game = Game() game.cpu_level = 0 chess_bot = Chess() chess_bot.stockfish_path = "" chess_bot.games.append(game) self.assertFalse(chess_bot.is_pve_game(game))
def test_is_pve_game_pvp_game(self): game = Game() game.cpu_level = None chess_bot = Chess() chess_bot.stockfish_path = "valid" chess_bot.games.append(game) self.assertFalse(chess_bot.is_pve_game(game))
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 test_make_move_finish_game_pve_player_wins(self): board = chess.Board() board.push_san("e4") board.push_san("g5") board.push_san("d4") board.push_san("f5") game = Game() game.board = board game.player1 = FakeDiscordUser(id=1) game.player2 = FakeDiscordUser(id=2) game.current_player = game.player1 game.cpu_level = 20 chess_bot = Chess() chess_bot.games.append(game) result = asyncio.run(chess_bot.make_move(game, 'Qh5')) self.assertIsInstance(result, Game) self.assertEqual(len(chess_bot.games), 0) self.assertEqual( self.db_session.query(ChessGame).filter_by(result=1).count(), 1) self.assertTrue(chess_bot.is_game_over(result)) self.assertEqual(result.result, '1-0')