2 Stockfish, a UCI chess playing engine derived from Glaurung 2.1
3 Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
4 Copyright (C) 2008-2012 Marco Costalba, Joona Kiiski, Tord Romstad
6 Stockfish is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 Stockfish is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
29 /// move_to_uci() converts a move to a string in coordinate notation
30 /// (g1f3, a7a8q, etc.). The only special case is castling moves, where we
31 /// print in the e1g1 notation in normal chess mode, and in e1h1 notation in
32 /// Chess960 mode. Instead internally Move is coded as "king captures rook".
34 const string move_to_uci(Move m, bool chess960) {
36 Square from = from_sq(m);
46 if (is_castle(m) && !chess960)
47 to = from + (file_of(to) == FILE_H ? Square(2) : -Square(2));
50 promotion = char(tolower(piece_type_to_char(promotion_type(m))));
52 return square_to_string(from) + square_to_string(to) + promotion;
56 /// move_from_uci() takes a position and a string representing a move in
57 /// simple coordinate notation and returns an equivalent Move if any.
58 /// Moves are guaranteed to be legal.
60 Move move_from_uci(const Position& pos, string str) {
62 // Some GUIs, like Junior, could send promotion in uppercase
63 std::transform(str.begin(), str.end(), str.begin(), tolower);
65 for (MoveList<MV_LEGAL> ml(pos); !ml.end(); ++ml)
66 if (str == move_to_uci(ml.move(), pos.is_chess960()))
73 /// move_to_san() takes a position and a move as input, where it is assumed
74 /// that the move is a legal move for the position. The return value is
75 /// a string containing the move in short algebraic notation.
77 const string move_to_san(Position& pos, Move m) {
88 bool ambiguousMove, ambiguousFile, ambiguousRank;
89 Square sq, from = from_sq(m);
91 PieceType pt = type_of(pos.piece_moved(m));
95 san = (to_sq(m) < from_sq(m) ? "O-O-O" : "O-O");
100 san = piece_type_to_char(pt);
102 // Disambiguation if we have more then one piece with destination 'to'
103 // note that for pawns is not needed because starting file is explicit.
104 attackers = pos.attackers_to(to) & pos.pieces(pos.side_to_move(), pt);
106 ambiguousMove = ambiguousFile = ambiguousRank = false;
110 sq = pop_1st_bit(&attackers);
112 // Pinned pieces are not included in the possible sub-set
113 if (!pos.pl_move_is_legal(make_move(sq, to), pos.pinned_pieces()))
116 if (file_of(sq) == file_of(from))
117 ambiguousFile = true;
119 if (rank_of(sq) == rank_of(from))
120 ambiguousRank = true;
122 ambiguousMove = true;
128 san += file_to_char(file_of(from));
129 else if (!ambiguousRank)
130 san += rank_to_char(rank_of(from));
132 san += square_to_string(from);
136 if (pos.is_capture(m))
139 san += file_to_char(file_of(from));
144 san += square_to_string(to);
149 san += piece_type_to_char(promotion_type(m));
153 if (pos.move_gives_check(m, CheckInfo(pos)))
157 san += MoveList<MV_LEGAL>(pos).size() ? "+" : "#";