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/>.
31 static const char* PieceToChar[COLOR_NB] = { " PNBRQK", " pnbrqk" };
34 /// score_to_uci() converts a value to a string suitable for use with the UCI
35 /// protocol specifications:
37 /// cp <x> The score from the engine's point of view in centipawns.
38 /// mate <y> Mate in y moves, not plies. If the engine is getting mated
39 /// use negative values for y.
41 string score_to_uci(Value v, Value alpha, Value beta) {
45 if (abs(v) < VALUE_MATE_IN_MAX_PLY)
46 s << "cp " << v * 100 / int(PawnValueMg);
48 s << "mate " << (v > 0 ? VALUE_MATE - v + 1 : -VALUE_MATE - v) / 2;
50 s << (v >= beta ? " lowerbound" : v <= alpha ? " upperbound" : "");
56 /// move_to_uci() converts a move to a string in coordinate notation
57 /// (g1f3, a7a8q, etc.). The only special case is castling moves, where we print
58 /// in the e1g1 notation in normal chess mode, and in e1h1 notation in chess960
59 /// mode. Internally castling moves are always encoded as "king captures rook".
61 const string move_to_uci(Move m, bool chess960) {
63 Square from = from_sq(m);
72 if (type_of(m) == CASTLING && !chess960)
73 to = make_square(to > from ? FILE_G : FILE_C, rank_of(from));
75 string move = to_string(from) + to_string(to);
77 if (type_of(m) == PROMOTION)
78 move += PieceToChar[BLACK][promotion_type(m)]; // Lower case
84 /// move_from_uci() takes a position and a string representing a move in
85 /// simple coordinate notation and returns an equivalent legal Move if any.
87 Move move_from_uci(const Position& pos, string& str) {
89 if (str.length() == 5) // Junior could send promotion piece in uppercase
90 str[4] = char(tolower(str[4]));
92 for (MoveList<LEGAL> it(pos); *it; ++it)
93 if (str == move_to_uci(*it, pos.is_chess960()))
100 /// move_to_san() takes a position and a legal Move as input and returns its
101 /// short algebraic notation representation.
103 const string move_to_san(Position& pos, Move m) {
111 assert(MoveList<LEGAL>(pos).contains(m));
115 Color us = pos.side_to_move();
116 Square from = from_sq(m);
117 Square to = to_sq(m);
118 Piece pc = pos.piece_on(from);
119 PieceType pt = type_of(pc);
121 if (type_of(m) == CASTLING)
122 san = to > from ? "O-O" : "O-O-O";
127 san = PieceToChar[WHITE][pt]; // Upper case
129 // A disambiguation occurs if we have more then one piece of type 'pt'
130 // that can reach 'to' with a legal move.
131 others = b = (pos.attacks_from(pc, to) & pos.pieces(us, pt)) ^ from;
135 Move move = make_move(pop_lsb(&b), to);
136 if (!pos.legal(move, pos.pinned_pieces(pos.side_to_move())))
137 others ^= from_sq(move);
142 if (!(others & file_bb(from)))
143 san += to_char(file_of(from));
145 else if (!(others & rank_bb(from)))
146 san += to_char(rank_of(from));
149 san += to_string(from);
152 else if (pos.capture(m))
153 san = to_char(file_of(from));
158 san += to_string(to);
160 if (type_of(m) == PROMOTION)
161 san += string("=") + PieceToChar[WHITE][promotion_type(m)];
164 if (pos.gives_check(m, CheckInfo(pos)))
168 san += MoveList<LEGAL>(pos).size() ? "+" : "#";
176 /// pretty_pv() formats human-readable search information, typically to be
177 /// appended to the search log file. It uses the two helpers below to pretty
178 /// format the time and score respectively.
180 static string time_to_string(int64_t msecs) {
182 const int MSecMinute = 1000 * 60;
183 const int MSecHour = 1000 * 60 * 60;
185 int64_t hours = msecs / MSecHour;
186 int64_t minutes = (msecs % MSecHour) / MSecMinute;
187 int64_t seconds = ((msecs % MSecHour) % MSecMinute) / 1000;
194 s << setfill('0') << setw(2) << minutes << ':' << setw(2) << seconds;
199 static string score_to_string(Value v) {
203 if (v >= VALUE_MATE_IN_MAX_PLY)
204 s << "#" << (VALUE_MATE - v + 1) / 2;
206 else if (v <= VALUE_MATED_IN_MAX_PLY)
207 s << "-#" << (VALUE_MATE + v) / 2;
210 s << setprecision(2) << fixed << showpos << double(v) / PawnValueMg;
215 string pretty_pv(Position& pos, int depth, Value value, uint64_t msecs, Move pv[]) {
217 const uint64_t K = 1000;
218 const uint64_t M = 1000000;
220 std::stack<StateInfo> st;
226 s << setw(2) << depth
227 << setw(8) << score_to_string(value)
228 << setw(8) << time_to_string(msecs);
230 if (pos.nodes_searched() < M)
231 s << setw(8) << pos.nodes_searched() / 1 << " ";
233 else if (pos.nodes_searched() < K * M)
234 s << setw(7) << pos.nodes_searched() / K << "K ";
237 s << setw(7) << pos.nodes_searched() / M << "M ";
239 padding = string(s.str().length(), ' ');
240 length = padding.length();
242 while (*m != MOVE_NONE)
244 san = move_to_san(pos, *m);
246 if (length + san.length() > 80)
249 length = padding.length();
253 length += san.length() + 1;
255 st.push(StateInfo());
256 pos.do_move(*m++, st.top());