def test_knight_moves(): BOARD = copy(BASIC_BOARD) BOARD[4, 4] = KNIGHT_ID env = ChessEnvV2(opponent="none", initial_board=BOARD) moves = env.get_possible_moves(attack=True) env.render_moves(moves) expected_attacks = set( [ (5, 5), (6, 5), (5, 4), (5, 1), (5, 7), (2, 3), (6, 3), (5, 6), (3, 6), (5, 3), (3, 2), (2, 5), (5, 2), ] ) squares_attacked = set([tuple(move[1]) for move in moves]) print(squares_attacked) assert squares_attacked == expected_attacks
def test_bishop_moves(): BOARD = copy(BASIC_BOARD) BOARD[4, 4] = BISHOP_ID env = ChessEnvV2(opponent="none", initial_board=BOARD) moves = env.get_possible_moves(attack=True) env.render_moves(moves) expected_attacks = set( [ (3, 3), (3, 5), (5, 3), (6, 2), (5, 5), (6, 6), (5, 3), (5, 1), (5, 4), (5, 2), (5, 5), (5, 3), (5, 6), (5, 4), (5, 7), (5, 5), ] ) squares_attacked = set([tuple(move[1]) for move in moves]) assert squares_attacked == expected_attacks
def test_benchmark(): env = ChessEnvV2(opponent="none", log=False) num_episodes = 10 num_steps = 100 total_steps = 0 start = time.time() for e in range(num_episodes): env.reset() for step in range(num_steps): total_steps += 1 moves = env.possible_moves if not moves: break idx = np.random.choice(np.arange(len(moves))) move = moves[idx] action = env.move_to_action(move) state, reward, done, info = env.step(action) if done: break end = time.time() diff = end - start print("Total time (s)", diff) print("Total episodes", num_episodes) print("Total steps", total_steps) print("Time per episode (s)", diff / num_episodes) print("Time per step (s)", diff / total_steps) # assert that it's less than 10 seconds assert diff < 1
def test_queen_capture_moves(): BOARD = copy(BASIC_BOARD) BOARD[5, 3] = QUEEN_ID env = ChessEnvV2(opponent="none", initial_board=BOARD) moves = env.get_possible_moves() env.render_moves(moves) expected_attacks = set([ (4, 4), (6, 2), (5, 5), (7, 1), (4, 3), (3, 1), (5, 4), (2, 0), (5, 1), (5, 7), (4, 2), (4, 5), (3, 3), (5, 0), (5, 6), (3, 5), (5, 2), ]) squares_attacked = set([tuple(move[1]) for move in moves]) assert squares_attacked == expected_attacks
def test_queen_side_castle(): BOARD = copy(BASIC_BOARD) BOARD[7, 0] = ROOK_ID BOARD[7, 4] = KING_ID env = ChessEnvV2(opponent="none", initial_board=BOARD) moves = env.get_castle_moves(player=env.current_player) env.render_moves(moves) assert moves == [CASTLE_QUEEN_SIDE_WHITE]
def test_pawn_basic_moves(): BOARD = copy(BASIC_BOARD) BOARD[6, 0] = PAWN_ID env = ChessEnvV2(opponent="none", initial_board=BOARD) moves = env.get_possible_moves() env.render_moves(moves) expected_attacks = set([(4, 0), (5, 0)]) squares_attacked = set([tuple(move[1]) for move in moves]) assert squares_attacked == expected_attacks
def test_king_moves_2(): BOARD = copy(BASIC_BOARD) BOARD[3, 4] = KING_ID env = ChessEnvV2(opponent="none", initial_board=BOARD) moves = env.get_possible_moves() env.render_moves(moves) king_is_checked = env.white_king_is_checked expected_attacks = set([(2, 4), (4, 3), (2, 3), (4, 5), (2, 5)]) squares_attacked = set([tuple(move[1]) for move in moves]) assert squares_attacked == expected_attacks assert not king_is_checked
def test_king_basic_moves(): BOARD = copy(BASIC_BOARD) BOARD[7, 7] = 0 BOARD[4, 4] = KING_ID env = ChessEnvV2(opponent="none", initial_board=BOARD) moves = env.get_possible_moves() env.render_moves(moves) expected_attacks = set([(5, 5), (3, 4), (4, 3), (5, 4), (4, 5), (3, 3), (5, 3), (3, 5)]) squares_attacked = set([tuple(move[1]) for move in moves]) assert squares_attacked == expected_attacks
def test_king_capture_moves(): BOARD = copy(BASIC_BOARD) BOARD[4, 3] = KING_ID BOARD[3, 2] = -PAWN_ID BOARD[2, 5] = -PAWN_ID BOARD[3, 5] = 0 BOARD[6, 3] = 0 BOARD[6, 4] = 0 BOARD[6, 5] = 0 env = ChessEnvV2(opponent="none", initial_board=BOARD) env.render() moves = env.get_possible_moves() env.render_moves(moves) expected_attacks = set([(3, 2), (3, 3), (5, 2), (5, 3), (5, 4)]) squares_attacked = set([tuple(move[1]) for move in moves]) assert squares_attacked == expected_attacks
def test_pawn_basic_moves(): BOARD = copy(BASIC_BOARD) BOARD[6, 0] = PAWN_ID BOARD[1, 0] = -PAWN_ID env = ChessEnvV2(opponent="none", initial_board=BOARD) # player_1 actions = env.get_possible_actions() env.step(actions[0]) # player_2 actions = env.get_possible_actions() env.step(actions[0]) # player_3 actions = env.get_possible_actions() env.step(actions[0]) # player_4 actions = env.get_possible_actions() env.step(actions[0]) EXPECTED_BOARD = copy(BASIC_BOARD) EXPECTED_BOARD[4, 0] = PAWN_ID EXPECTED_BOARD[3, 0] = -PAWN_ID assert (np.array(env.state["board"]) == np.array(EXPECTED_BOARD)).all()
def test_rook_basic_moves(): BOARD = copy(BASIC_BOARD) BOARD[4, 4] = ROOK_ID env = ChessEnvV2(opponent="none", initial_board=BOARD) moves = env.get_possible_moves() env.render_moves(moves) expected_attacks = set([ (7, 4), (2, 4), (4, 0), (0, 4), (3, 4), (4, 3), (5, 4), (4, 6), (6, 4), (1, 4), (4, 2), (4, 5), (4, 1), (4, 7), ]) squares_attacked = set([tuple(move[1]) for move in moves]) assert squares_attacked == expected_attacks