diff --git a/src/search.cpp b/src/search.cpp index 82f6c6750..2dcfb86d9 100644 --- a/src/search.cpp +++ b/src/search.cpp @@ -1711,32 +1711,28 @@ Value Search::Worker::qsearch(Position& pos, Stack* ss, Value alpha, Value beta) } } - // Step 9. Check for mate + // Step 9. Check for mate and stalemate // All legal moves have been searched. A special case: if we are // in check and no legal moves were found, it is checkmate. - if (ss->inCheck && bestValue == -VALUE_INFINITE) + if (!moveCount) { - assert(!MoveList(pos).size()); - return mated_in(ss->ply); // Plies to mate from the root + if (ss->inCheck) // Checkmate! + { + assert(!MoveList(pos).size()); + return mated_in(ss->ply); // Plies to mate from the root + } + + // Only check for stalemate under specific conditions + Color us = pos.side_to_move(); + if (!(pawn_single_push_bb(us, pos.pieces(us, PAWN)) & ~pos.pieces()) + && !pos.non_pawn_material(us) && type_of(pos.captured_piece()) >= KNIGHT + && !MoveList(pos).size()) + bestValue = VALUE_DRAW; } if (!is_decisive(bestValue) && bestValue > beta) bestValue = (bestValue + beta) / 2; - Color us = pos.side_to_move(); - if (!ss->inCheck && !moveCount && !pos.non_pawn_material(us) - && type_of(pos.captured_piece()) >= ROOK) - { - if (!(pawn_single_push_bb(us, pos.pieces(us, PAWN)) - & ~pos.pieces())) // no pawn pushes available - { - pos.state()->checkersBB = Rank1BB; // search for legal king-moves only - if (!MoveList(pos).size()) // stalemate - bestValue = VALUE_DRAW; - pos.state()->checkersBB = 0; - } - } - // Save gathered info in transposition table. The static evaluation // is saved as it was before adjustment by correction history. ttWriter.write(posKey, value_to_tt(bestValue, ss->ply), pvHit,