- san += piece_type_to_char(pt);
-
- switch (move_ambiguity(pos, m)) {
- case AMBIGUITY_NONE:
- break;
- case AMBIGUITY_FILE:
- san += file_to_char(square_file(from));
- break;
- case AMBIGUITY_RANK:
- san += rank_to_char(square_rank(from));
- break;
- case AMBIGUITY_BOTH:
- san += square_to_string(from);
- break;
- default:
- assert(false);
- }
+ san = piece_type_to_char(pt);
+
+ // Collect all legal moves of piece type 'pt' with destination 'to'
+ MoveStack* last = generate<MV_LEGAL>(pos, mlist);
+ int f = 0, r = 0;
+
+ for (MoveStack* cur = mlist; cur != last; cur++)
+ if ( move_to(cur->move) == to
+ && pos.type_of_piece_on(move_from(cur->move)) == pt)
+ {
+ if (square_file(move_from(cur->move)) == square_file(from))
+ f++;
+
+ if (square_rank(move_from(cur->move)) == square_rank(from))
+ r++;
+ }
+
+ assert(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);