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));
78 pt = type_of_piece(pos.piece_on(move_from(m)));
84 else if (m == MOVE_NULL)
86 else if (move_is_long_castle(m) || (int(to - from) == -2 && pt == KING))
88 else if (move_is_short_castle(m) || (int(to - from) == 2 && pt == KING))
94 san += piece_type_to_char(pt, true);
95 switch (move_ambiguity(pos, m)) {
99 san += file_to_char(square_file(from));
102 san += rank_to_char(square_rank(from));
105 san += square_to_string(from);
111 if (pos.move_is_capture(m))
114 san += file_to_char(square_file(move_from(m)));
117 san += square_to_string(move_to(m));
118 if (move_promotion(m))
121 san += piece_type_to_char(move_promotion(m), true);
124 // Is the move check? We don't use pos.move_is_check(m) here, because
125 // Position::move_is_check doesn't detect all checks (not castling moves,
126 // promotions and en passant captures).
131 san += p.is_mate()? "#" : "+";
137 /// move_from_san() takes a position and a string as input, and tries to
138 /// interpret the string as a move in short algebraic notation. On success,
139 /// the move is returned. On failure (i.e. if the string is unparsable, or
140 /// if the move is illegal or ambiguous), MOVE_NONE is returned.
142 Move move_from_san(const Position& pos, const std::string& movestr) {
146 MovePicker mp = MovePicker(pos, false, MOVE_NONE, EmptySearchStack, OnePly);
149 if (movestr == "O-O-O" || movestr == "O-O-O+")
152 while ((m = mp.get_next_move()) != MOVE_NONE)
153 if (move_is_long_castle(m) && pos.pl_move_is_legal(m))
158 else if (movestr == "O-O" || movestr == "O-O+")
161 while ((m = mp.get_next_move()) != MOVE_NONE)
162 if (move_is_short_castle(m) && pos.pl_move_is_legal(m))
168 // Normal moves. We use a simple FSM to parse the san string.
169 enum { START, TO_FILE, TO_RANK, PROMOTION_OR_CHECK, PROMOTION, CHECK, END };
170 static const std::string pieceLetters = "KQRBN";
171 PieceType pt = NO_PIECE_TYPE, promotion = NO_PIECE_TYPE;
172 File fromFile = FILE_NONE, toFile = FILE_NONE;
173 Rank fromRank = RANK_NONE, toRank = RANK_NONE;
177 for (size_t i = 0; i < movestr.length(); i++)
179 char type, c = movestr[i];
180 if (pieceLetters.find(c) != std::string::npos)
182 else if (c >= 'a' && c <= 'h')
184 else if (c >= '1' && c <= '8')
193 pt = piece_type_from_char(c);
196 else if (state == PROMOTION)
198 promotion = piece_type_from_char(c);
199 state = (i < movestr.length() - 1) ? CHECK : END;
208 fromFile = toFile = file_from_char(c);
211 else if (state == TO_FILE)
213 toFile = file_from_char(c);
216 else if (state == TO_RANK && toFile != FILE_NONE)
218 // Previous file was for disambiguation
220 toFile = file_from_char(c);
226 if (state == TO_RANK)
228 toRank = rank_from_char(c);
229 state = (i < movestr.length() - 1) ? PROMOTION_OR_CHECK : END;
231 else if (state == TO_FILE && fromRank == RANK_NONE)
233 // It's a disambiguation rank instead of a file
234 fromRank = rank_from_char(c);
240 if (state == TO_RANK)
242 // Previous file was for disambiguation, or it's a pawn capture
246 else if (state != TO_FILE)
250 if (state == PROMOTION_OR_CHECK)
256 if (state == PROMOTION_OR_CHECK || state == CHECK)
270 // Look for a matching move
271 Move m, move = MOVE_NONE;
272 to = make_square(toFile, toRank);
275 while ((m = mp.get_next_move()) != MOVE_NONE)
276 if ( pos.type_of_piece_on(move_from(m)) == pt
278 && move_promotion(m) == promotion
279 && (fromFile == FILE_NONE || fromFile == square_file(move_from(m)))
280 && (fromRank == RANK_NONE || fromRank == square_rank(move_from(m))))
285 return (matches == 1 ? move : MOVE_NONE);
289 /// line_to_san() takes a position and a line (an array of moves representing
290 /// a sequence of legal moves from the position) as input, and returns a
291 /// string containing the line in short algebraic notation. If the boolean
292 /// parameter 'breakLines' is true, line breaks are inserted, with a line
293 /// length of 80 characters. After a line break, 'startColumn' spaces are
294 /// inserted at the beginning of the new line.
296 const std::string line_to_san(const Position& pos, Move line[], int startColumn, bool breakLines) {
302 size_t maxLength = 80 - startColumn;
305 for (int i = 0; line[i] != MOVE_NONE; i++)
307 moveStr = move_to_san(p, line[i]);
308 length += moveStr.length() + 1;
309 if (breakLines && length > maxLength)
311 s << '\n' << std::setw(startColumn) << ' ';
312 length = moveStr.length() + 1;
316 if (line[i] == MOVE_NULL)
319 p.do_move(line[i], st);
325 /// pretty_pv() creates a human-readable string from a position and a PV.
326 /// It is used to write search information to the log file (which is created
327 /// when the UCI parameter "Use Search Log" is "true").
329 const std::string pretty_pv(const Position& pos, int time, int depth,
330 uint64_t nodes, Value score, Move pv[]) {
334 s << std::setw(2) << depth << " ";
337 s << std::setw(8) << score_string(score);
340 s << std::setw(8) << time_string(time) << " ";
343 if (nodes < 1000000ULL)
344 s << std::setw(8) << nodes << " ";
345 else if (nodes < 1000000000ULL)
346 s << std::setw(7) << nodes/1000ULL << 'k' << " ";
348 s << std::setw(7) << nodes/1000000ULL << 'M' << " ";
351 s << line_to_san(pos, pv, 30, true);
359 Ambiguity move_ambiguity(const Position& pos, Move m) {
361 Square from = move_from(m);
362 Square to = move_to(m);
363 Piece pc = pos.piece_on(from);
365 // King moves are never ambiguous, because there is never two kings of
367 if (type_of_piece(pc) == KING)
368 return AMBIGUITY_NONE;
370 MovePicker mp = MovePicker(pos, false, MOVE_NONE, EmptySearchStack, OnePly);
371 Move mv, moveList[8];
374 while ((mv = mp.get_next_move()) != MOVE_NONE)
375 if (move_to(mv) == to && pos.piece_on(move_from(mv)) == pc && pos.pl_move_is_legal(mv))
379 return AMBIGUITY_NONE;
382 for (int i = 0; i < n; i++)
384 if (square_file(move_from(moveList[i])) == square_file(from))
387 if (square_rank(move_from(moveList[i])) == square_rank(from))
391 return AMBIGUITY_FILE;
394 return AMBIGUITY_RANK;
396 return AMBIGUITY_BOTH;
400 const std::string time_string(int milliseconds) {
403 s << std::setfill('0');
405 int hours = milliseconds / (1000*60*60);
406 int minutes = (milliseconds - hours*1000*60*60) / (1000*60);
407 int seconds = (milliseconds - hours*1000*60*60 - minutes*1000*60) / 1000;
412 s << std::setw(2) << minutes << ':' << std::setw(2) << seconds;
417 const std::string score_string(Value v) {
421 if (v >= VALUE_MATE - 200)
422 s << "#" << (VALUE_MATE - v + 1) / 2;
423 else if(v <= -VALUE_MATE + 200)
424 s << "-#" << (VALUE_MATE + v) / 2;
427 float floatScore = float(v) / float(PawnValueMidgame);
431 s << std::setprecision(2) << std::fixed << floatScore;