// Update king attacks used for fast check detection
set_check_info(st);
+ // Calculate the repetition info. It is the ply distance from the previous
+ // occurrence of the same position, negative in the 3-fold case, or zero
+ // if the position was not repeated.
+ st->repetition = 0;
+ int end = std::min(st->rule50, st->pliesFromNull);
+ if (end >= 4)
+ {
+ StateInfo* stp = st->previous->previous;
+ for (int i=4; i <= end; i += 2)
+ {
+ stp = stp->previous->previous;
+ if (stp->key == st->key)
+ {
+ st->repetition = stp->repetition ? -i : i;
+ break;
+ }
+ }
+ }
+
assert(pos_is_ok());
}
set_check_info(st);
+ st->repetition = 0;
+
assert(pos_is_ok());
}
if (st->rule50 > 99 && (!checkers() || MoveList<LEGAL>(*this).size()))
return true;
- int end = std::min(st->rule50, st->pliesFromNull);
-
- if (end < 4)
- return false;
-
- StateInfo* stp = st->previous->previous;
- 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 a draw score if a position repeats once earlier but strictly
+ // after the root, or repeats twice before or at the root.
+ if (st->repetition && st->repetition < ply)
+ return true;
return false;
}
bool Position::has_repeated() const {
StateInfo* stc = st;
- while (true)
+ int end = std::min(st->rule50, st->pliesFromNull);
+ while (end-- >= 4)
{
- int i = 4, end = std::min(stc->rule50, stc->pliesFromNull);
-
- if (end < i)
- return false;
-
- StateInfo* stp = stc->previous->previous;
-
- do {
- stp = stp->previous->previous;
-
- if (stp->key == stc->key)
- return true;
-
- i += 2;
- } while (i <= end);
+ if (stc->repetition)
+ return true;
stc = stc->previous;
}
+ return false;
}
continue;
// For repetitions before or at the root, require one more
- StateInfo* next_stp = stp;
- for (int k = i + 2; k <= end; k += 2)
- {
- next_stp = next_stp->previous->previous;
- if (next_stp->key == stp->key)
- return true;
- }
+ if (stp->repetition)
+ return true;
}
}
}