X-Git-Url: https://git.sesse.net/?p=stockfish;a=blobdiff_plain;f=src%2Fposition.cpp;h=6a393ebb9b0730d5058a21c81ee622c71ec54225;hp=1b82b6c01f48013a85f02abc50bea48ce4ae97fc;hb=fe99de20ff8f0594a17e2cedf680d25e86dd5a13;hpb=ec83e8a72c702713d550fb2a228f56a6c03704c4 diff --git a/src/position.cpp b/src/position.cpp index 1b82b6c0..6a393ebb 100644 --- a/src/position.cpp +++ b/src/position.cpp @@ -1079,25 +1079,30 @@ bool Position::see_ge(Move m, Value v) const { /// Position::is_draw() tests whether the position is drawn by 50-move rule /// or by repetition. It does not detect stalemates. -bool Position::is_draw() const { +bool Position::is_draw(int ply) const { if (st->rule50 > 99 && (!checkers() || MoveList(*this).size())) return true; - int e = std::min(st->rule50, st->pliesFromNull); + int end = std::min(st->rule50, st->pliesFromNull); - if (e < 4) + if (end < 4) return false; StateInfo* stp = st->previous->previous; + int cnt = 0; - do { + for (int i = 4; i <= end; i += 2) + { stp = stp->previous->previous; - if (stp->key == st->key) - return true; // Draw at first repetition - - } while ((e -= 2) >= 4); + // At root position ply is 1, so return a draw score if a position + // repeats once earlier but after or at the root, or repeats twice + // strictly before the root. + if ( stp->key == st->key + && ++cnt + (ply - i > 0) == 2) + return true; + } return false; }