Fix a bug in Position::is_ok()
authorMarco Costalba <mcostalba@gmail.com>
Sun, 26 Jun 2011 10:42:58 +0000 (11:42 +0100)
committerMarco Costalba <mcostalba@gmail.com>
Sun, 26 Jun 2011 10:44:12 +0000 (11:44 +0100)
If we cannot castle castleRightsMask[] could be not valid,
for instance when king initial file is FILE_A as queen rook.

In this case castleRightsMask[] at initialQRFile is different
from the expected (ALL_CASTLES ^ WHITE_OOO).

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
src/position.cpp

index bb9ec00c9f66f84caf03cc89e88420028d14ad88..42fdd62da8bfc3c121c61916164449682a2ed630 100644 (file)
@@ -2039,14 +2039,19 @@ bool Position::is_ok(int* failedStep) const {
           if (can_castle_queenside(c) && piece_on(initial_qr_square(c)) != make_piece(c, ROOK))
               return false;
       }
-      if (castleRightsMask[initial_kr_square(WHITE)] != (ALL_CASTLES ^ WHITE_OO))
-          return false;
-      if (castleRightsMask[initial_qr_square(WHITE)] != (ALL_CASTLES ^ WHITE_OOO))
-          return false;
-      if (castleRightsMask[initial_kr_square(BLACK)] != (ALL_CASTLES ^ BLACK_OO))
-          return false;
-      if (castleRightsMask[initial_qr_square(BLACK)] != (ALL_CASTLES ^ BLACK_OOO))
-          return false;
+      // If we cannot castle castleRightsMask[] could be not valid, for instance when
+      // king initial file is FILE_A as queen rook.
+      if (can_castle(WHITE) || can_castle(BLACK))
+      {
+          if (castleRightsMask[initial_kr_square(WHITE)] != (ALL_CASTLES ^ WHITE_OO))
+              return false;
+          if (castleRightsMask[initial_qr_square(WHITE)] != (ALL_CASTLES ^ WHITE_OOO))
+              return false;
+          if (castleRightsMask[initial_kr_square(BLACK)] != (ALL_CASTLES ^ BLACK_OO))
+              return false;
+          if (castleRightsMask[initial_qr_square(BLACK)] != (ALL_CASTLES ^ BLACK_OOO))
+              return false;
+      }
   }
 
   if (failedStep) *failedStep = 0;