diff --git a/src/movegen.c b/src/movegen.c index 5d40d147..f90b74c5 100644 --- a/src/movegen.c +++ b/src/movegen.c @@ -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; +} diff --git a/src/movegen.h b/src/movegen.h index 367bf646..31303d8b 100644 --- a/src/movegen.h +++ b/src/movegen.h @@ -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); diff --git a/src/search.c b/src/search.c index 57aebc22..11976080 100644 --- a/src/search.c +++ b/src/search.c @@ -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 && (!pos->checkers || LegalMoveCount(pos) > 0)) return DrawScore(pos); // If we are at max depth, return static eval @@ -260,8 +264,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 && (!pos->checkers || LegalMoveCount(pos) > 0)) return DrawScore(pos); // Max depth reached