Skip to content

Commit

Permalink
Properly handle checkmate occuring exactly when reaching the 50 move …
Browse files Browse the repository at this point in the history
…rule (#754)

Elo   | 0.18 +- 2.84 (95%)
Conf  | 8.0+0.08s Threads=1 Hash=32MB
Games | N: 21244 W: 5800 L: 5789 D: 9655
Penta | [369, 2456, 4950, 2489, 358]
http://chess.grantnet.us/test/38610/

Bench: 26587524
  • Loading branch information
TerjeKir authored Dec 21, 2024
1 parent 3adc2e2 commit c554b7d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
7 changes: 7 additions & 0 deletions src/movegen.c
Original file line number Diff line number Diff line change
Expand Up @@ -187,3 +187,10 @@ void GenLegalMoves(Position *pos, MoveList *list) {
}
pos->nodes = 0;
}

int LegalMoveCount(Position *pos) {
MoveList list;
list.count = list.next = 0;
GenLegalMoves(pos, &list);
return list.count;
}
1 change: 1 addition & 0 deletions src/movegen.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,4 @@ void GenNoisyMoves(const Position *pos, MoveList *list);
void GenQuietMoves(const Position *pos, MoveList *list);
void GenAllMoves(const Position *pos, MoveList *list);
void GenLegalMoves(Position *pos, MoveList *list);
int LegalMoveCount(Position *pos);
21 changes: 14 additions & 7 deletions src/search.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,12 @@ static int Quiescence(Thread *thread, Stack *ss, int alpha, int beta) {
return alpha;
}

// Position is drawn
if (IsRepetition(pos) || pos->rule50 >= 100)
// Position is drawn by repetition
if (IsRepetition(pos))
return DrawScore(pos);

// Position is drawn by 50 move rule
if (pos->rule50 >= 100 && (!inCheck || LegalMoveCount(pos) > 0))
return DrawScore(pos);

// If we are at max depth, return static eval
Expand Down Expand Up @@ -244,7 +248,8 @@ static int AlphaBeta(Thread *thread, Stack *ss, int alpha, int beta, Depth depth
ss->doubleExtensions = (ss-1)->doubleExtensions;

const bool pvNode = alpha != beta - 1;
const bool root = ss->ply == 0;
const bool root = ss->ply == 0;
const bool inCheck = pos->checkers;

// Check time situation
if (OutOfTime(thread) || loadRelaxed(ABORT_SIGNAL))
Expand All @@ -260,8 +265,12 @@ static int AlphaBeta(Thread *thread, Stack *ss, int alpha, int beta, Depth depth
return alpha;
}

// Position is drawn
if (IsRepetition(pos) || pos->rule50 >= 100)
// Position is drawn by repetition
if (IsRepetition(pos))
return DrawScore(pos);

// Position is drawn by 50 move rule
if (pos->rule50 >= 100 && (!inCheck || LegalMoveCount(pos) > 0))
return DrawScore(pos);

// Max depth reached
Expand Down Expand Up @@ -325,8 +334,6 @@ static int AlphaBeta(Thread *thread, Stack *ss, int alpha, int beta, Depth depth
}
}

const bool inCheck = pos->checkers;

// Do a static evaluation for pruning considerations
int eval = ss->staticEval = inCheck ? NOSCORE
: lastMoveNullMove ? -(ss-1)->staticEval + 2 * Tempo
Expand Down

0 comments on commit c554b7d

Please sign in to comment.