Handle placement corner cases more naturally
authorFabian Fichter <ianfab@users.noreply.github.com>
Sat, 2 Oct 2021 21:46:35 +0000 (23:46 +0200)
committerFabian Fichter <ianfab@users.noreply.github.com>
Sat, 2 Oct 2021 21:46:35 +0000 (23:46 +0200)
No functional change for legal positions,
just generalizes more naturally to artifical positions.

src/position.cpp

index f2d6e6f..acce8a4 100644 (file)
@@ -958,13 +958,13 @@ bool Position::legal(Move m) const {
       {
           Bitboard remaining = drop_region(us, BISHOP) & ~pieces() & ~square_bb(to);
           // Are enough squares available to drop bishops on opposite colors?
-          if (  (!( DarkSquares & pieces(us, BISHOP)) && ( DarkSquares & remaining))
-              + (!(~DarkSquares & pieces(us, BISHOP)) && (~DarkSquares & remaining)) < count_in_hand(us, BISHOP))
+          if (   popcount( DarkSquares & (pieces(us, BISHOP) | remaining)) < count_with_hand(us, BISHOP) / 2
+              || popcount(~DarkSquares & (pieces(us, BISHOP) | remaining)) < count_with_hand(us, BISHOP) / 2)
               return false;
       }
       else
           // Drop resulting in same-colored bishops
-          if ((DarkSquares & to ? DarkSquares : ~DarkSquares) & pieces(us, BISHOP))
+          if (popcount((DarkSquares & to ? DarkSquares : ~DarkSquares) & pieces(us, BISHOP)) + 1 > (count_with_hand(us, BISHOP) + 1) / 2)
               return false;
   }
 
@@ -1086,15 +1086,15 @@ bool Position::legal(Move m) const {
   if (var->makpongRule && checkers() && type_of(moved_piece(m)) == KING && (checkers() ^ to))
       return false;
 
-  // Return early when without king
-  if (!count<KING>(us))
-      return true;
-
   // If the moving piece is a king, check whether the destination square is
   // attacked by the opponent.
   if (type_of(moved_piece(m)) == KING)
       return !attackers_to(to, occupied, ~us);
 
+  // Return early when without king
+  if (!count<KING>(us))
+      return true;
+
   Bitboard janggiCannons = pieces(JANGGI_CANNON);
   if (type_of(moved_piece(m)) == JANGGI_CANNON)
       janggiCannons = (type_of(m) == DROP ? janggiCannons : janggiCannons ^ from) | to;