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 Marco Costalba
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/>.
34 extern SearchStack EmptySearchStack;
37 //// Local definitions
54 Ambiguity move_ambiguity(const Position& pos, Move m);
55 const std::string time_string(int milliseconds);
56 const std::string score_string(Value v);
64 /// move_to_san() takes a position and a move as input, where it is assumed
65 /// that the move is a legal move from the position. The return value is
66 /// a string containing the move in short algebraic notation.
68 const std::string move_to_san(const Position& pos, Move m) {
71 assert(move_is_ok(m));
77 else if (m == MOVE_NULL)
79 else if (move_is_long_castle(m))
81 else if (move_is_short_castle(m))
85 Piece pc = pos.piece_on(move_from(m));
86 if (type_of_piece(pc) != PAWN)
88 san += piece_type_to_char(type_of_piece(pc), true);
89 Square from = move_from(m);
90 switch (move_ambiguity(pos, m)) {
94 san += file_to_char(square_file(from));
97 san += rank_to_char(square_rank(from));
100 san += square_to_string(from);
106 if (pos.move_is_capture(m))
108 if (type_of_piece(pc) == PAWN)
109 san += file_to_char(square_file(move_from(m)));
112 san += square_to_string(move_to(m));
113 if (move_promotion(m))
116 san += piece_type_to_char(move_promotion(m), true);
119 // Is the move check? We don't use pos.move_is_check(m) here, because
120 // Position::move_is_check doesn't detect all checks (not castling moves,
121 // promotions and en passant captures).
126 san += p.is_mate()? "#" : "+";
132 /// move_from_san() takes a position and a string as input, and tries to
133 /// interpret the string as a move in short algebraic notation. On success,
134 /// the move is returned. On failure (i.e. if the string is unparsable, or
135 /// if the move is illegal or ambiguous), MOVE_NONE is returned.
137 Move move_from_san(const Position& pos, const std::string& movestr) {
141 MovePicker mp = MovePicker(pos, false, MOVE_NONE, EmptySearchStack, OnePly);
144 if (movestr == "O-O-O")
147 while ((m = mp.get_next_move()) != MOVE_NONE)
148 if (move_is_long_castle(m) && pos.pl_move_is_legal(m))
153 else if (movestr == "O-O")
156 while ((m = mp.get_next_move()) != MOVE_NONE)
157 if (move_is_short_castle(m) && pos.pl_move_is_legal(m))
164 const char *cstr = movestr.c_str();
170 // Initialize str[] by making a copy of movestr with the characters
171 // 'x', '=', '+' and '#' removed.
173 for(i=0, c=cstr; i<10 && *c!='\0' && *c!='\n' && *c!=' '; i++, c++)
174 if(!strchr("x=+#", *c)) {
175 *cc = strchr("nrq", *c)? toupper(*c) : *c;
180 size_t left = 0, right = strlen(str) - 1;
181 PieceType pt = NO_PIECE_TYPE, promotion;
183 File fromFile = FILE_NONE;
184 Rank fromRank = RANK_NONE;
187 if(strchr("BNRQ", str[right])) {
188 promotion = piece_type_from_char(str[right]);
192 promotion = NO_PIECE_TYPE;
194 // Find the moving piece:
196 if(strchr("BNRQK", str[left])) {
197 pt = piece_type_from_char(str[left]);
204 // Find the to square:
206 if(str[right] < '1' || str[right] > '8' ||
207 str[right-1] < 'a' || str[right-1] > 'h')
209 to = make_square(file_from_char(str[right-1]), rank_from_char(str[right]));
215 // Find the file and/or rank of the from square:
217 if(strchr("abcdefgh", str[left])) {
218 fromFile = file_from_char(str[left]);
221 if(strchr("12345678", str[left]))
222 fromRank = rank_from_char(str[left]);
225 // Look for a matching move:
226 Move m, move = MOVE_NONE;
229 while((m = mp.get_next_move()) != MOVE_NONE) {
231 if(pos.type_of_piece_on(move_from(m)) != pt)
233 else if(move_to(m) != to)
235 else if(move_promotion(m) != promotion)
237 else if(fromFile != FILE_NONE && fromFile != square_file(move_from(m)))
239 else if(fromRank != RANK_NONE && fromRank != square_rank(move_from(m)))
254 /// line_to_san() takes a position and a line (an array of moves representing
255 /// a sequence of legal moves from the position) as input, and returns a
256 /// string containing the line in short algebraic notation. If the boolean
257 /// parameter 'breakLines' is true, line breaks are inserted, with a line
258 /// length of 80 characters. After a line break, 'startColumn' spaces are
259 /// inserted at the beginning of the new line.
261 const std::string line_to_san(const Position& pos, Move line[], int startColumn, bool breakLines) {
267 size_t maxLength = 80 - startColumn;
270 for (int i = 0; line[i] != MOVE_NONE; i++)
272 moveStr = move_to_san(p, line[i]);
273 length += moveStr.length() + 1;
274 if (breakLines && length > maxLength)
276 s << '\n' << std::setw(startColumn) << ' ';
277 length = moveStr.length() + 1;
281 if (line[i] == MOVE_NULL)
284 p.do_move(line[i], u);
290 /// pretty_pv() creates a human-readable string from a position and a PV.
291 /// It is used to write search information to the log file (which is created
292 /// when the UCI parameter "Use Search Log" is "true").
294 const std::string pretty_pv(const Position& pos, int time, int depth,
295 uint64_t nodes, Value score, Move pv[]) {
299 s << std::setw(2) << depth << " ";
302 s << std::setw(8) << score_string(score);
305 s << std::setw(8) << time_string(time) << " ";
308 if (nodes < 1000000ULL)
309 s << std::setw(8) << nodes << " ";
310 else if (nodes < 1000000000ULL)
311 s << std::setw(7) << nodes/1000ULL << 'k' << " ";
313 s << std::setw(7) << nodes/1000000ULL << 'M' << " ";
316 s << line_to_san(pos, pv, 30, true);
324 Ambiguity move_ambiguity(const Position& pos, Move m) {
326 Square from = move_from(m);
327 Square to = move_to(m);
328 Piece pc = pos.piece_on(from);
330 // King moves are never ambiguous, because there is never two kings of
332 if (type_of_piece(pc) == KING)
333 return AMBIGUITY_NONE;
335 MovePicker mp = MovePicker(pos, false, MOVE_NONE, EmptySearchStack, OnePly);
336 Move mv, moveList[8];
339 while ((mv = mp.get_next_move()) != MOVE_NONE)
340 if (move_to(mv) == to && pos.piece_on(move_from(mv)) == pc && pos.pl_move_is_legal(mv))
344 return AMBIGUITY_NONE;
347 for (int i = 0; i < n; i++)
349 if (square_file(move_from(moveList[i])) == square_file(from))
352 if (square_rank(move_from(moveList[i])) == square_rank(from))
356 return AMBIGUITY_FILE;
359 return AMBIGUITY_RANK;
361 return AMBIGUITY_BOTH;
365 const std::string time_string(int milliseconds) {
368 s << std::setfill('0');
370 int hours = milliseconds / (1000*60*60);
371 int minutes = (milliseconds - hours*1000*60*60) / (1000*60);
372 int seconds = (milliseconds - hours*1000*60*60 - minutes*1000*60) / 1000;
377 s << std::setw(2) << minutes << ':' << std::setw(2) << seconds;
382 const std::string score_string(Value v) {
386 if (v >= VALUE_MATE - 200)
387 s << "#" << (VALUE_MATE - v + 1) / 2;
388 else if(v <= -VALUE_MATE + 200)
389 s << "-#" << (VALUE_MATE + v) / 2;
392 float floatScore = float(v) / float(PawnValueMidgame);
396 s << std::setprecision(2) << std::fixed << floatScore;