Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Early termination caused by castling #982

Closed
HongruiTang opened this issue Jul 19, 2023 · 5 comments
Closed

Early termination caused by castling #982

HongruiTang opened this issue Jul 19, 2023 · 5 comments

Comments

@HongruiTang
Copy link
Contributor

HongruiTang commented Jul 19, 2023

Hi Sotetsu,

I found an issue with both kingside and queenside castling in chess when I was playing with some games on Lichess. The problem is the games are always early terminated due to illegal actions after castling. I dived into the code and found the problem is probably in the _apply_move function (line 318).

The original implementation only changed state._board but not state._possible_piece_positions after castling. Although, each move updates state._possible_piece_positions here:

# actually move
    state = state.replace(  # type: ignore
        _board=state._board.at[a.from_].set(EMPTY).at[a.to].set(piece)
    )

due to the fact that castling is a move that involves two submoves, the position of the Rook should also be updated. Otherwise, this part

actions = legal_norml_moves(
        state._possible_piece_positions[0]
    ).flatten()  # include -1

would be affected when checking legal moves.

I also have a question about the variable state._possible_piece_positions. When a piece is captured, the position of the piece will remain the same or should be set as a new value, like -1.

Also, a minor typo is corrected at line 65 in chess.py

Thanks

@sotetsuk
Copy link
Owner

Hi, Thank you for sending a PR! 😄 I really appreciate it 🙏

Could you kindly add a test case in which the current implementation fails but the PR works? 🙏 🥺

We collect buggy test cases here:
https://github.com/sotetsuk/pgx/blob/main/tests/test_chess.py#L659

@HongruiTang
Copy link
Contributor Author

Thanks! I've added a test in https://github.com/sotetsuk/pgx/blob/main/tests/test_chess.py#L659

@sotetsuk
Copy link
Owner

I also have a question about the variable state._possible_piece_positions. When a piece is captured, the position of the piece will remain the same or should be set as a new value, like -1.

This is because _possible_piece_positions is used only to represent the index candidates where pieces may exists.
It can speed up the computation in

    actions = legal_norml_moves(
        state._possible_piece_positions[0]
    ).flatten()  # include -1

but note that as _possible_piece_positions represents candidates, this should also work

    actions = legal_norml_moves(
        jnp.arange(64)
    ).flatten()  # include -1

So even if actually there are no pieces in some _possible_piece_positions, it's ok.

@sotetsuk
Copy link
Owner

#983 is merged. I'll close this issue after a new pgx version is released.
Thank you for your contribution! 🙏 @HongruiTang

@HongruiTang
Copy link
Contributor Author

I also have a question about the variable state._possible_piece_positions. When a piece is captured, the position of the piece will remain the same or should be set as a new value, like -1.

This is because _possible_piece_positions is used only to represent the index candidates where pieces may exists. It can speed up the computation in

    actions = legal_norml_moves(
        state._possible_piece_positions[0]
    ).flatten()  # include -1

but note that as _possible_piece_positions represents candidates, this should also work

    actions = legal_norml_moves(
        jnp.arange(64)
    ).flatten()  # include -1

So even if actually there are no pieces in some _possible_piece_positions, it's ok.

Thanks for the clarification. That makes sense!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants