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

Properly handle checkmate occuring exactly when reaching the 50 move rule #754

Merged
merged 1 commit into from
Dec 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading