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-2010 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/>.
37 //// Local definitions
43 AMBIGUITY_NONE, AMBIGUITY_FILE, AMBIGUITY_RANK, AMBIGUITY_BOTH
46 Ambiguity move_ambiguity(const Position& pos, Move m);
47 const string time_string(int milliseconds);
48 const string score_string(Value v);
56 /// move_to_san() takes a position and a move as input, where it is assumed
57 /// that the move is a legal move from the position. The return value is
58 /// a string containing the move in short algebraic notation.
60 const string move_to_san(Position& pos, Move m) {
63 assert(move_is_ok(m));
66 Square from = move_from(m);
67 Square to = move_to(m);
68 PieceType pt = type_of_piece(pos.piece_on(from));
76 if (move_is_long_castle(m))
78 else if (move_is_short_castle(m))
84 san += piece_type_to_char(pt);
86 switch (move_ambiguity(pos, m)) {
90 san += file_to_char(square_file(from));
93 san += rank_to_char(square_rank(from));
96 san += square_to_string(from);
103 if (pos.move_is_capture(m))
106 san += file_to_char(square_file(from));
110 san += square_to_string(to);
112 if (move_is_promotion(m))
115 san += piece_type_to_char(move_promotion_piece(m));
119 // The move gives check ? We don't use pos.move_is_check() here
120 // because we need to test for mate after the move is done.
124 san += pos.is_mate() ? "#" : "+";
131 /// move_from_san() takes a position and a string as input, and tries to
132 /// interpret the string as a move in short algebraic notation. On success,
133 /// the move is returned. On failure (i.e. if the string is unparsable, or
134 /// if the move is illegal or ambiguous), MOVE_NONE is returned.
136 Move move_from_san(const Position& pos, const string& movestr) {
140 enum { START, TO_FILE, TO_RANK, PROMOTION_OR_CHECK, PROMOTION, CHECK, END };
141 static const string pieceLetters = "KQRBN";
143 MoveStack mlist[MOVES_MAX], *last;
144 PieceType pt = PIECE_TYPE_NONE, promotion = PIECE_TYPE_NONE;
145 File fromFile = FILE_NONE, toFile = FILE_NONE;
146 Rank fromRank = RANK_NONE, toRank = RANK_NONE;
147 Move move = MOVE_NONE;
149 int matches, state = START;
151 // Generate all legal moves for the given position
152 last = generate<MV_LEGAL>(pos, mlist);
155 if (movestr == "O-O-O" || movestr == "O-O-O+")
157 for (MoveStack* cur = mlist; cur != last; cur++)
158 if (move_is_long_castle(cur->move))
163 else if (movestr == "O-O" || movestr == "O-O+")
165 for (MoveStack* cur = mlist; cur != last; cur++)
166 if (move_is_short_castle(cur->move))
172 // Normal moves. We use a simple FSM to parse the san string
173 for (size_t i = 0; i < movestr.length(); i++)
175 char type, c = movestr[i];
177 if (pieceLetters.find(c) != string::npos)
179 else if (c >= 'a' && c <= 'h')
181 else if (c >= '1' && c <= '8')
190 pt = piece_type_from_char(c);
193 else if (state == PROMOTION)
195 promotion = piece_type_from_char(c);
196 state = (i < movestr.length() - 1 ? CHECK : END);
205 fromFile = toFile = file_from_char(c);
208 else if (state == TO_FILE)
210 toFile = file_from_char(c);
213 else if (state == TO_RANK && toFile != FILE_NONE)
215 // Previous file was for disambiguation
217 toFile = file_from_char(c);
223 if (state == TO_RANK)
225 toRank = rank_from_char(c);
226 state = (i < movestr.length() - 1) ? PROMOTION_OR_CHECK : END;
228 else if (state == TO_FILE && fromRank == RANK_NONE)
230 // It's a disambiguation rank instead of a file
231 fromRank = rank_from_char(c);
238 if (state == TO_RANK)
240 // Previous file was for disambiguation, or it's a pawn capture
244 else if (state != TO_FILE)
248 if (state == PROMOTION_OR_CHECK)
255 if (state == PROMOTION_OR_CHECK || state == CHECK)
269 // Look for an unambiguous matching move
270 to = make_square(toFile, toRank);
273 for (MoveStack* cur = mlist; cur != last; cur++)
275 from = move_from(cur->move);
277 if ( pos.type_of_piece_on(from) == pt
278 && move_to(cur->move) == to
279 && move_promotion_piece(cur->move) == promotion
280 && (fromFile == FILE_NONE || fromFile == square_file(from))
281 && (fromRank == RANK_NONE || fromRank == square_rank(from)))
287 return matches == 1 ? move : MOVE_NONE;
291 /// line_to_san() takes a position and a line (an array of moves representing
292 /// a sequence of legal moves from the position) as input, and returns a
293 /// string containing the line in short algebraic notation. If the boolean
294 /// parameter 'breakLines' is true, line breaks are inserted, with a line
295 /// length of 80 characters. After a line break, 'startColumn' spaces are
296 /// inserted at the beginning of the new line.
298 const string line_to_san(const Position& pos, Move line[], int startColumn, bool breakLines) {
304 size_t maxLength = 80 - startColumn;
305 Position p(pos, pos.thread());
307 for (Move* m = line; *m != MOVE_NONE; m++)
309 moveStr = move_to_san(p, *m);
310 length += moveStr.length() + 1;
311 if (breakLines && length > maxLength)
313 s << "\n" << std::setw(startColumn) << " ";
314 length = moveStr.length() + 1;
327 /// pretty_pv() creates a human-readable string from a position and a PV.
328 /// It is used to write search information to the log file (which is created
329 /// when the UCI parameter "Use Search Log" is "true").
331 const string pretty_pv(const Position& pos, int time, int depth,
332 Value score, ValueType type, Move pv[]) {
334 const int64_t K = 1000;
335 const int64_t M = 1000000;
340 s << std::setw(2) << depth << " ";
343 s << (type == VALUE_TYPE_LOWER ? ">" : type == VALUE_TYPE_UPPER ? "<" : " ")
344 << std::setw(7) << score_string(score);
347 s << std::setw(8) << time_string(time) << " ";
350 if (pos.nodes_searched() < M)
351 s << std::setw(8) << pos.nodes_searched() / 1 << " ";
352 else if (pos.nodes_searched() < K * M)
353 s << std::setw(7) << pos.nodes_searched() / K << "K ";
355 s << std::setw(7) << pos.nodes_searched() / M << "M ";
358 s << line_to_san(pos, pv, 30, true);
366 Ambiguity move_ambiguity(const Position& pos, Move m) {
368 MoveStack mlist[MOVES_MAX], *last;
370 Square from = move_from(m);
371 Square to = move_to(m);
372 Piece pc = pos.piece_on(from);
373 int matches = 0, f = 0, r = 0;
375 // If there is only one piece 'pc' then move cannot be ambiguous
376 if (pos.piece_count(pos.side_to_move(), type_of_piece(pc)) == 1)
377 return AMBIGUITY_NONE;
379 // Collect all legal moves of piece 'pc' with destination 'to'
380 last = generate<MV_LEGAL>(pos, mlist);
381 for (MoveStack* cur = mlist; cur != last; cur++)
382 if (move_to(cur->move) == to && pos.piece_on(move_from(cur->move)) == pc)
383 candidates[matches++] = cur->move;
386 return AMBIGUITY_NONE;
388 for (int i = 0; i < matches; i++)
390 if (square_file(move_from(candidates[i])) == square_file(from))
393 if (square_rank(move_from(candidates[i])) == square_rank(from))
397 return f == 1 ? AMBIGUITY_FILE : r == 1 ? AMBIGUITY_RANK : AMBIGUITY_BOTH;
401 const string time_string(int millisecs) {
403 const int MSecMinute = 1000 * 60;
404 const int MSecHour = 1000 * 60 * 60;
407 s << std::setfill('0');
409 int hours = millisecs / MSecHour;
410 int minutes = (millisecs - hours * MSecHour) / MSecMinute;
411 int seconds = (millisecs - hours * MSecHour - minutes * MSecMinute) / 1000;
416 s << std::setw(2) << minutes << ':' << std::setw(2) << seconds;
421 const string score_string(Value v) {
425 if (v >= VALUE_MATE - 200)
426 s << "#" << (VALUE_MATE - v + 1) / 2;
427 else if (v <= -VALUE_MATE + 200)
428 s << "-#" << (VALUE_MATE + v) / 2;
431 float floatScore = float(v) / float(PawnValueMidgame);
435 s << std::setprecision(2) << std::fixed << floatScore;