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-2014 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 static const char* PieceToChar[COLOR_NB] = { " PNBRQK", " pnbrqk" };
32 /// score_to_uci() converts a value to a string suitable for use with the UCI
33 /// protocol specifications:
35 /// cp <x> The score from the engine's point of view in centipawns.
36 /// mate <y> Mate in y moves, not plies. If the engine is getting mated
37 /// use negative values for y.
39 string score_to_uci(Value v, Value alpha, Value beta) {
43 if (abs(v) < VALUE_MATE_IN_MAX_PLY)
44 ss << "cp " << v * 100 / PawnValueEg;
46 ss << "mate " << (v > 0 ? VALUE_MATE - v + 1 : -VALUE_MATE - v) / 2;
48 ss << (v >= beta ? " lowerbound" : v <= alpha ? " upperbound" : "");
54 /// move_to_uci() converts a move to a string in coordinate notation
55 /// (g1f3, a7a8q, etc.). The only special case is castling moves, where we print
56 /// in the e1g1 notation in normal chess mode, and in e1h1 notation in chess960
57 /// mode. Internally castling moves are always encoded as "king captures rook".
59 const string move_to_uci(Move m, bool chess960) {
61 Square from = from_sq(m);
70 if (type_of(m) == CASTLING && !chess960)
71 to = make_square(to > from ? FILE_G : FILE_C, rank_of(from));
73 string move = to_string(from) + to_string(to);
75 if (type_of(m) == PROMOTION)
76 move += PieceToChar[BLACK][promotion_type(m)]; // Lower case
82 /// move_from_uci() takes a position and a string representing a move in
83 /// simple coordinate notation and returns an equivalent legal Move if any.
85 Move move_from_uci(const Position& pos, string& str) {
87 if (str.length() == 5) // Junior could send promotion piece in uppercase
88 str[4] = char(tolower(str[4]));
90 for (MoveList<LEGAL> it(pos); *it; ++it)
91 if (str == move_to_uci(*it, pos.is_chess960()))