Simplify move_to_san()
authorMarco Costalba <mcostalba@gmail.com>
Sat, 9 Feb 2013 05:40:46 +0000 (06:40 +0100)
committerMarco Costalba <mcostalba@gmail.com>
Sat, 9 Feb 2013 06:43:53 +0000 (07:43 +0100)
Nicely simplify disambiguation code.

No functional change.

src/notation.cpp

index bb4a713cc2e563752bf17cefd57720b121de6430..b8e89b67b2bb3b0e89af2d5b9701d5a150e56f53 100644 (file)
@@ -110,8 +110,7 @@ const string move_to_san(Position& pos, Move m) {
 
   assert(MoveList<LEGAL>(pos).contains(m));
 
-  Bitboard attackers;
-  bool ambiguousMove, ambiguousFile, ambiguousRank;
+  Bitboard others, b;
   string san;
   Color us = pos.side_to_move();
   Square from = from_sq(m);
@@ -127,31 +126,23 @@ const string move_to_san(Position& pos, Move m) {
       {
           san = PieceToChar[WHITE][pt]; // Upper case
 
-          // Disambiguation if we have more then one piece with destination 'to'
-          // note that for pawns is not needed because starting file is explicit.
-          ambiguousMove = ambiguousFile = ambiguousRank = false;
+          // Disambiguation if we have more then one piece of type 'pt' that can
+          // reach 'to' with a legal move.
+          others = b = (pos.attacks_from(pc, to) & pos.pieces(us, pt)) ^ from;
 
-          attackers = (pos.attacks_from(pc, to) & pos.pieces(us, pt)) ^ from;
-
-          while (attackers)
+          while (b)
           {
-              Square sq = pop_lsb(&attackers);
-
-              // If the move is illegal, the piece is not included in the sub-set
-              if (!pos.pl_move_is_legal(make_move(sq, to), pos.pinned_pieces()))
-                  continue;
-
-              ambiguousFile |= file_of(sq) == file_of(from);
-              ambiguousRank |= rank_of(sq) == rank_of(from);
-              ambiguousMove = true;
+              Move move = make_move(pop_lsb(&b), to);
+              if (!pos.pl_move_is_legal(move, pos.pinned_pieces()))
+                  others ^= from_sq(move);
           }
 
-          if (ambiguousMove)
+          if (others)
           {
-              if (!ambiguousFile)
+              if (!(others & file_bb(from)))
                   san += file_to_char(file_of(from));
 
-              else if (!ambiguousRank)
+              else if (!(others & rank_bb(from)))
                   san += rank_to_char(rank_of(from));
 
               else