Skip to content

Improve Draw Rule Handling#553

Open
ANDREA-1720 wants to merge 6 commits into
jhlywa:masterfrom
ANDREA-1720:master
Open

Improve Draw Rule Handling#553
ANDREA-1720 wants to merge 6 commits into
jhlywa:masterfrom
ANDREA-1720:master

Conversation

@ANDREA-1720
Copy link
Copy Markdown

Important

The changes applied by this PR can be described as BREAKING CHANGES as contains changes to the public API as descibed in the Changes sections

Overview

This PR refines the implementation of draw conditions to make them more compliant with official FIDE chess rules.

Changes

  • Manual Drawing

    • Added draw() method to allow players to manually claim a draw.
  • Fifty-Move Rule

    • Split logic into:
    • canDrawByFiftyMoves() → detects when the position qualifies for a draw claim.
    • isDrawByFiftyMoves() → requires explicit claim (_isManuallyDrawn).
  • Threefold Repetition

    • Split logic into:
      • canThreefoldRepetition() → detects when the position qualifies for a claim.
      • isThreefoldRepetition() → requires explicit claim (_isManuallyDrawn).
    • Updated tests (can-threefold-repetition.test.ts) to reflect new semantics.
  • Null Move Handling

    • At an high-level it does not change, only the implementation changes.
    • Added _nullMovesCount to track consecutive null moves ( with "--" SAN).
    • Game considered drawn if canThreefoldRepetition() and 6 consecutive null moves occur.

Note

The handling of manual draws must be implemented by the developer using the library, for example:

  • For draws not triggered by fifty-move or threefold repetition rules, the developer should ensure that both players agree to a draw.
  • For draws due to fifty-move or threefold repetition, it is sufficient that only one player requests the draw for it to be valid.

Why

  • Aligns draw logic with FIDE chess rules (distinction between automatic and claimable draws).

Tests

  • Updated and renamed tests for canThreefoldRepetition().
  • Confirmed compliance with all tests.

@ANDREA-1720 ANDREA-1720 changed the title Improve Draw Rule Handling in Chess Engine Improve Draw Rule Handling Aug 20, 2025
@neofight78
Copy link
Copy Markdown
Collaborator

I'm not necessarily ruling out changes, but I do think some confusion arises from the fact the a the Chess object both represents a game and the current board state. I think perhaps we need documentation here.

The Result header tag is the correct place for storing and retrieving the actual result of the game.

The methods such as isDraw(), isThreefoldRepetition() etc are implemented as you would typically find in a chess engine where draws get automatically get claimed. So they are more about enforcing rules and evaluating the position etc, and not so much about giving the result of a human vs human game.

@meruiden
Copy link
Copy Markdown

this would be awesome, currently very inconsistent with modern chess apps like chess.com and lichess.

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

Successfully merging this pull request may close these issues.

3 participants