+ int cnt = 0;
+
+ for (int i = 4; i <= end; i += 2)
+ {
+ stp = stp->previous->previous;
+
+ // Return a draw score if a position repeats once earlier but strictly
+ // after the root, or repeats twice before or at the root.
+ if ( stp->key == st->key
+ && ++cnt + (ply > i) == 2)
+ return true;
+ }
+
+ return false;
+}
+
+
+// Position::has_repeated() tests whether there has been at least one repetition
+// of positions since the last capture or pawn move.
+
+bool Position::has_repeated() const {
+
+ StateInfo* stc = st;
+ while (true)
+ {
+ int i = 4, end = std::min(stc->rule50, stc->pliesFromNull);
+
+ if (end < i)
+ return false;
+
+ StateInfo* stp = stc->previous->previous;