chess.js
chess.js copied to clipboard
Threefold repetition checking
in_threefold_repetition() returns true when the same position has repeated 3+ times, but if after that I make absolutely another moves, the opponent too, in_threefold_repetition() still returns true.
I'm not sure that this behavior is by the chess rules. I think the result should be false when the position has changed
Hi @imilo. Thanks for the report. I think you're correct. Using FIDE rule 9.2 as a reference:
The game is drawn, upon a correct claim by a player having the move, when the same
position for at least the third time (not necessarily by a repetition of moves):
- is about to appear, if he first writes his move, which cannot be changed, on his
scoresheet and declares to the arbiter his intention to make this move, or
- has just appeared, and the player claiming the draw has the move.
So maybe we should change the code so in_threefold_repetition() returns true if and only if the current position has been repeated three times?
@jhlywa , if that question was for me, that's a tricky question...
For example, if I make a position that has appeared 3+ times -> in_threefold_repetition() will return True a that moment. But my opponent can make his new unique move very quickly -> position will be changed -> in_threefold_repetition() will return False for me. So I will lose my chance to claim for a draw.
chess.js should handle this somehow. JavaScript could remember that I can claim for a draw until my next move, so we don't care about the opponent's move. These are my thoughts, may be you have a better solution
@imilo you make a good point. I think we should change in_threefold_repetition() to return true only if the current_position has appeared for at least the third time. That way we match FIDE rules. @jhlywa if you're OK with this I'll submit a PR for your approval.
@imilo I like your suggestion that Javascript can remember that you can claim for a draw until your next move. That's not the FIDE rule, but you can make it that way for your own chess site even if chess.js is strictly following the FIDE rules. You could do it like this:
if (players_turn) {
if (game.in_threefold_repetition()) {
// show "Claim Draw" button to the player
} else {
// check and see if our last move created a threefold repetition so that we could have claimed a draw.
var move = game.pop() // undo the opponent's move
if (game.in_threefold_repetition()) {
// show "Claim Draw" button to the player
}
game.make_move(move)
}
}
This bug propagates to chess.game_over() and chess.in_draw() returning true, as others have said it should check for the current position vs older positions.
This should be labeled as a bug in my opinion.