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

Chess: fix checkmate and add draw #2017

Merged
merged 2 commits into from
Feb 9, 2025

Conversation

hemberger
Copy link
Member

  • Checkmate no longer incorrectly happens when there are valid moves to block check.
  • Two simple draw conditions are now checked to properly end the game in a draw:
    1. Insufficient material (just two kings)
    2. Stalemate

Previously, the condition tested was that if the King couldn't make a
legal move, then it was checkmate. This is wrong. Now, we check if any
piece can make a legal move (since a non-King piece could block the
checkmate).

Also moved the `isCheckmated` method from ChessGame to Board, since it
only depends on the board state (modulo player turn), not the game
itself.
Introduce the concept of a Draw into the Chess library. Clarify the
Board::isCheckmated method to properly distinguish between checkmate and
stalemate (both occur when no valid moves can be made by a player).

Handle ending the game and updating the HOF when a Draw occurs.

Note that we do not handle the following Draw conditions:
* Draw by repetition
* Draw by choice
* Draw by insufficient material (beyond just two Kings)

These are more complex and would require significant changes.

Implementation note: The Board::clear method has been added to simplify
setting up test board states. It adds some undesired complexity to the
class, because setting an arbitrary position requires input about the
castling and en passant statuses. Exposing this interface can lead to
invalid game states (e.g. no Rook, but the castling state is still set).
I considered adding a FEN parser, but decided it was too much work just
to set up a couple test board states.
Copy link

codecov bot commented Feb 8, 2025

Codecov Report

Attention: Patch coverage is 38.46154% with 48 lines in your changes missing coverage. Please review.

Project coverage is 31.94%. Comparing base (f348b02) to head (5f1fe48).
Report is 3 commits behind head on main.

Files with missing lines Patch % Lines
src/lib/Smr/Chess/ChessGame.php 0.00% 48 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##               main    #2017      +/-   ##
============================================
+ Coverage     31.74%   31.94%   +0.20%     
- Complexity     4205     4226      +21     
============================================
  Files           125      125              
  Lines         12815    12855      +40     
============================================
+ Hits           4068     4107      +39     
- Misses         8747     8748       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@hemberger hemberger merged commit c6bc737 into smrealms:main Feb 9, 2025
9 checks passed
@hemberger hemberger deleted the chess-mate-and-draw branch February 9, 2025 09:44
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.

1 participant