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

Figure out if the position is checkmate, drawn (repetition) #13

Open
ZelphirKaltstahl opened this issue Apr 6, 2018 · 2 comments
Open

Comments

@ZelphirKaltstahl
Copy link

How would you figure out if a player has won in a position?

@nozaq
Copy link
Owner

nozaq commented Apr 8, 2018

@ZelphirKaltstahl When you call Position#make_move() and the resulting position after the move is invalid, it returns MoveError. You can detect checkmate and repetition using this result as follows.

1. Checkmate

If you get MoveError::InCheck from make_move(), this means that the move is illegal since the king is still in check even after making a move. Given the king is already in check, you can detect the player is checkmated by trying every possible moves and check if no legal move exist.
Position#move_candidates() could be used to produce possible moves though, it's necessary to call it with each piece on the board. Currently the only way to get each piece on the board is to iterate through all squares and call [Position#piece_at()])(https://nozaq.github.io/shogi-rs/shogi/position/struct.Position.html#method.piece_at).
I'm thinking to implement an easier way to iterate over pieces of the particular player, which hopefully improve the performance of iteration.

2. Repetition

This case is simple. If you get MoveError::Repetition after making a move, that means the game should be drawn because of repetition.
Note that there is a special case in repetition situation called Perpetual Check(see https://en.wikipedia.org/wiki/Shogi). MoveError::PerpetualCheckWin and MoveError::PerpetualCheckLose exist for this case.

Hope this would help, please let me know if you have other questions :)

@ZelphirKaltstahl
Copy link
Author

ZelphirKaltstahl commented Apr 8, 2018

Thanks for your response. I already thought of trying all moves, but was hoping there was something better. It should still be OKish performance, but of course a more efficient way would be great. I would also need to try with inserting pieces everywhere, unless I involve some logic about what piece puts the king in check and squares where it can be etc.

Thanks for the hint about perpetual check as well, I only recently learned about that on https://www.gnu.org/software/gnushogi/manual/The-rules-of-shogi.html .

nozaq pushed a commit that referenced this issue Apr 8, 2018
nozaq pushed a commit that referenced this issue Apr 14, 2018
nozaq pushed a commit that referenced this issue Jun 3, 2018
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