]> git.sesse.net Git - stockfish/blobdiff - src/move.cpp
Fix SAN disambiguation bug
[stockfish] / src / move.cpp
index f915feecc1c5412974f25cf37de4d86f80d8918a..7504615d6d670c5ee0e7279ca90c57aa06bd6b27 100644 (file)
@@ -66,11 +66,12 @@ const string move_to_uci(Move m, bool chess960) {
 
 
 /// move_from_uci() takes a position and a string representing a move in
-/// simple coordinate notation and returns an equivalent Move.
+/// simple coordinate notation and returns an equivalent Move if any.
+/// Moves are guaranteed to be legal.
 
 Move move_from_uci(const Position& pos, const string& str) {
 
-  MoveStack mlist[MOVES_MAX];
+  MoveStack mlist[MAX_MOVES];
   MoveStack* last = generate<MV_LEGAL>(pos, mlist);
 
   for (MoveStack* cur = mlist; cur != last; cur++)
@@ -90,7 +91,7 @@ const string move_to_san(Position& pos, Move m) {
   assert(pos.is_ok());
   assert(move_is_ok(m));
 
-  MoveStack mlist[MOVES_MAX];
+  MoveStack mlist[MAX_MOVES];
   Square from = move_from(m);
   Square to = move_to(m);
   PieceType pt = pos.type_of_piece_on(from);
@@ -114,12 +115,14 @@ const string move_to_san(Position& pos, Move m) {
 
           // Collect all legal moves of piece type 'pt' with destination 'to'
           MoveStack* last = generate<MV_LEGAL>(pos, mlist);
-          int f = 0, r = 0;
+          int f = 0, r = 0, cnt = 0;
 
           for (MoveStack* cur = mlist; cur != last; cur++)
               if (   move_to(cur->move) == to
                   && pos.type_of_piece_on(move_from(cur->move)) == pt)
               {
+                  cnt++;
+
                   if (square_file(move_from(cur->move)) == square_file(from))
                       f++;
 
@@ -127,15 +130,18 @@ const string move_to_san(Position& pos, Move m) {
                       r++;
               }
 
-          assert(f > 0 && r > 0);
+          assert(cnt > 0 && f > 0 && r > 0);
 
           // Disambiguation if we have more then one piece with destination 'to'
-          if (f == 1 && r > 1)
-              san += file_to_char(square_file(from));
-          else if (f > 1 && r == 1)
-              san += rank_to_char(square_rank(from));
-          else if (f > 1 && r > 1)
-              san += square_to_string(from);
+          if (cnt > 1)
+          {
+              if (f == 1)
+                  san += file_to_char(square_file(from));
+              else if (r == 1)
+                  san += rank_to_char(square_rank(from));
+              else
+                  san += square_to_string(from);
+          }
       }
 
       if (pos.move_is_capture(m))
@@ -155,11 +161,11 @@ const string move_to_san(Position& pos, Move m) {
       }
   }
 
-  // The move gives check? We don't use pos.move_is_check() here
+  // The move gives check? We don't use pos.move_gives_check() here
   // because we need to test for a mate after the move is done.
   StateInfo st;
   pos.do_move(m, st);
-  if (pos.is_check())
+  if (pos.in_check())
       san += pos.is_mate() ? "#" : "+";
   pos.undo_move(m);