Fix detection of optional game end
authorFabian Fichter <ianfab@users.noreply.github.com>
Mon, 4 Mar 2019 19:54:56 +0000 (20:54 +0100)
committerFabian Fichter <ianfab@users.noreply.github.com>
Mon, 4 Mar 2019 19:58:40 +0000 (20:58 +0100)
No functional change for variants without counting rules.

src/position.cpp

index 45373a9..7c6c007 100644 (file)
@@ -1570,27 +1570,27 @@ bool Position::is_optional_game_end(Value& result, int ply) const {
   {
       int end = captures_to_hand() ? st->pliesFromNull : std::min(st->rule50, st->pliesFromNull);
 
-      if (end < 4)
-          return false;
-
-      StateInfo* stp = st->previous->previous;
-      int cnt = 0;
-      bool perpetual = true;
-
-      for (int i = 4; i <= end; i += 2)
+      if (end >= 4)
       {
-          stp = stp->previous->previous;
-          perpetual &= bool(stp->checkersBB);
+          StateInfo* stp = st->previous->previous;
+          int cnt = 0;
+          bool perpetual = 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 (   stp->key == st->key
-              && ++cnt + 1 == (ply > i ? 2 : n_fold_rule()))
+          for (int i = 4; i <= end; i += 2)
           {
-              result = convert_mate_value(  var->perpetualCheckIllegal && perpetual ? VALUE_MATE
-                                          : var->nFoldValueAbsolute && sideToMove == BLACK ? -var->nFoldValue
-                                          : var->nFoldValue, ply);
-              return true;
+              stp = stp->previous->previous;
+              perpetual &= bool(stp->checkersBB);
+
+              // 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 + 1 == (ply > i ? 2 : n_fold_rule()))
+              {
+                  result = convert_mate_value(  var->perpetualCheckIllegal && perpetual ? VALUE_MATE
+                                              : var->nFoldValueAbsolute && sideToMove == BLACK ? -var->nFoldValue
+                                              : var->nFoldValue, ply);
+                  return true;
+              }
           }
       }
   }