Skip to content

Commit

Permalink
Added draw by fifty move rule
Browse files Browse the repository at this point in the history
  • Loading branch information
Eliagiac committed Jan 17, 2024
1 parent c0203f0 commit 0cf0c86
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
6 changes: 2 additions & 4 deletions src/Lunarr/Board.cs
Original file line number Diff line number Diff line change
Expand Up @@ -698,9 +698,7 @@ public uint FourBitCastlingRights()
public void MakeNullMove()
{
PlyCount++;

// A null move is irreversible.
FiftyMovePlyCount = 0;
FiftyMovePlyCount++;

CapturedPieceType = None;

Expand Down Expand Up @@ -753,7 +751,7 @@ public void UnmakeNullMove()
Opponent ^= 1;

PlyCount--;
FiftyMovePlyCount = previousPosition.FiftyMovePlyCount;
FiftyMovePlyCount--;

TT.CalculateCurrentEntryIndex(this);

Expand Down
11 changes: 11 additions & 0 deletions src/Lunarr/Engine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1447,6 +1447,17 @@ public static bool IsMateLossScore(int score)
public static int MatedIn(int ply) => -Checkmate + ply;


/// <summary>
/// After 50 moves (100 ply) if there were no captures or pawn moves the game is a draw, unless the last move delivered checkmate.
/// </summary>
/// <remarks>
/// Since verifying a checkmate would involve generating all legal moves (which is expensive),
/// <see cref="Board.IsInCheck"/> is used instead. This means some draws will not be seen immediately
/// </remarks>
public static bool IsDrawByFiftyMoveRule() =>
t_board.FiftyMovePlyCount > 99 &&
(t_board.FiftyMovePlyCount != 100 || !t_board.IsInCheck(t_board.Friendly));

/// <summary>If a position is reached three times, it's a draw.</summary>
/// <remarks>The current implementation returns true even if the position was only reached twice.</remarks>
public static bool IsDrawByRepetition() => t_board.PositionKeyHistory.Count(other => other == t_board.ZobristKey) >= 2;
Expand Down

0 comments on commit 0cf0c86

Please sign in to comment.