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_uci() converts a move to a string in coordinate notation
57 /// (g1f3, a7a8q, etc.). The only special case is castling moves, where we
58 /// print in the e1g1 notation in normal chess mode, and in e1h1 notation in
61 const std::string move_to_uci(Move m, bool chess960) {
63 std::string promotion;
64 Square from = move_from(m);
65 Square to = move_to(m);
73 if (move_is_short_castle(m) && !chess960)
74 return from == SQ_E1 ? "e1g1" : "e8g8";
76 if (move_is_long_castle(m) && !chess960)
77 return from == SQ_E1 ? "e1c1" : "e8c8";
79 if (move_is_promotion(m))
80 promotion = char(tolower(piece_type_to_char(move_promotion_piece(m))));
82 return square_to_string(from) + square_to_string(to) + promotion;
86 /// move_from_uci() takes a position and a string representing a move in
87 /// simple coordinate notation and returns an equivalent Move.
89 Move move_from_uci(const Position& pos, const std::string& str) {
91 MoveStack mlist[MOVES_MAX];
92 MoveStack* last = generate<MV_LEGAL>(pos, mlist);
94 for (MoveStack* cur = mlist; cur != last; cur++)
95 if (str == move_to_uci(cur->move, pos.is_chess960()))
102 /// move_to_san() takes a position and a move as input, where it is assumed
103 /// that the move is a legal move from the position. The return value is
104 /// a string containing the move in short algebraic notation.
106 const string move_to_san(Position& pos, Move m) {
109 assert(move_is_ok(m));
112 Square from = move_from(m);
113 Square to = move_to(m);
114 PieceType pt = type_of_piece(pos.piece_on(from));
122 if (move_is_long_castle(m))
124 else if (move_is_short_castle(m))
130 san += piece_type_to_char(pt);
132 switch (move_ambiguity(pos, m)) {
136 san += file_to_char(square_file(from));
139 san += rank_to_char(square_rank(from));
142 san += square_to_string(from);
149 if (pos.move_is_capture(m))
152 san += file_to_char(square_file(from));
156 san += square_to_string(to);
158 if (move_is_promotion(m))
161 san += piece_type_to_char(move_promotion_piece(m));
165 // The move gives check ? We don't use pos.move_is_check() here
166 // because we need to test for mate after the move is done.
170 san += pos.is_mate() ? "#" : "+";
177 /// move_from_san() takes a position and a string as input, and tries to
178 /// interpret the string as a move in short algebraic notation. On success,
179 /// the move is returned. On failure (i.e. if the string is unparsable, or
180 /// if the move is illegal or ambiguous), MOVE_NONE is returned.
182 Move move_from_san(const Position& pos, const string& movestr) {
186 enum { START, TO_FILE, TO_RANK, PROMOTION_OR_CHECK, PROMOTION, CHECK, END };
187 static const string pieceLetters = "KQRBN";
189 MoveStack mlist[MOVES_MAX], *last;
190 PieceType pt = PIECE_TYPE_NONE, promotion = PIECE_TYPE_NONE;
191 File fromFile = FILE_NONE, toFile = FILE_NONE;
192 Rank fromRank = RANK_NONE, toRank = RANK_NONE;
193 Move move = MOVE_NONE;
195 int matches, state = START;
197 // Generate all legal moves for the given position
198 last = generate<MV_LEGAL>(pos, mlist);
201 if (movestr == "O-O-O" || movestr == "O-O-O+")
203 for (MoveStack* cur = mlist; cur != last; cur++)
204 if (move_is_long_castle(cur->move))
209 else if (movestr == "O-O" || movestr == "O-O+")
211 for (MoveStack* cur = mlist; cur != last; cur++)
212 if (move_is_short_castle(cur->move))
218 // Normal moves. We use a simple FSM to parse the san string
219 for (size_t i = 0; i < movestr.length(); i++)
221 char type, c = movestr[i];
223 if (pieceLetters.find(c) != string::npos)
225 else if (c >= 'a' && c <= 'h')
227 else if (c >= '1' && c <= '8')
236 pt = piece_type_from_char(c);
239 else if (state == PROMOTION)
241 promotion = piece_type_from_char(c);
242 state = (i < movestr.length() - 1 ? CHECK : END);
251 fromFile = toFile = file_from_char(c);
254 else if (state == TO_FILE)
256 toFile = file_from_char(c);
259 else if (state == TO_RANK && toFile != FILE_NONE)
261 // Previous file was for disambiguation
263 toFile = file_from_char(c);
269 if (state == TO_RANK)
271 toRank = rank_from_char(c);
272 state = (i < movestr.length() - 1) ? PROMOTION_OR_CHECK : END;
274 else if (state == TO_FILE && fromRank == RANK_NONE)
276 // It's a disambiguation rank instead of a file
277 fromRank = rank_from_char(c);
284 if (state == TO_RANK)
286 // Previous file was for disambiguation, or it's a pawn capture
290 else if (state != TO_FILE)
294 if (state == PROMOTION_OR_CHECK)
301 if (state == PROMOTION_OR_CHECK || state == CHECK)
315 // Look for an unambiguous matching move
316 to = make_square(toFile, toRank);
319 for (MoveStack* cur = mlist; cur != last; cur++)
321 from = move_from(cur->move);
323 if ( pos.type_of_piece_on(from) == pt
324 && move_to(cur->move) == to
325 && move_promotion_piece(cur->move) == promotion
326 && (fromFile == FILE_NONE || fromFile == square_file(from))
327 && (fromRank == RANK_NONE || fromRank == square_rank(from)))
333 return matches == 1 ? move : MOVE_NONE;
337 /// line_to_san() takes a position and a line (an array of moves representing
338 /// a sequence of legal moves from the position) as input, and returns a
339 /// string containing the line in short algebraic notation. If the boolean
340 /// parameter 'breakLines' is true, line breaks are inserted, with a line
341 /// length of 80 characters. After a line break, 'startColumn' spaces are
342 /// inserted at the beginning of the new line.
344 const string line_to_san(const Position& pos, Move line[], int startColumn, bool breakLines) {
350 size_t maxLength = 80 - startColumn;
351 Position p(pos, pos.thread());
353 for (Move* m = line; *m != MOVE_NONE; m++)
355 moveStr = move_to_san(p, *m);
356 length += moveStr.length() + 1;
357 if (breakLines && length > maxLength)
359 s << "\n" << std::setw(startColumn) << " ";
360 length = moveStr.length() + 1;
373 /// pretty_pv() creates a human-readable string from a position and a PV.
374 /// It is used to write search information to the log file (which is created
375 /// when the UCI parameter "Use Search Log" is "true").
377 const string pretty_pv(const Position& pos, int time, int depth,
378 Value score, ValueType type, Move pv[]) {
380 const int64_t K = 1000;
381 const int64_t M = 1000000;
386 s << std::setw(2) << depth << " ";
389 s << (type == VALUE_TYPE_LOWER ? ">" : type == VALUE_TYPE_UPPER ? "<" : " ")
390 << std::setw(7) << score_string(score);
393 s << std::setw(8) << time_string(time) << " ";
396 if (pos.nodes_searched() < M)
397 s << std::setw(8) << pos.nodes_searched() / 1 << " ";
398 else if (pos.nodes_searched() < K * M)
399 s << std::setw(7) << pos.nodes_searched() / K << "K ";
401 s << std::setw(7) << pos.nodes_searched() / M << "M ";
404 s << line_to_san(pos, pv, 30, true);
412 Ambiguity move_ambiguity(const Position& pos, Move m) {
414 MoveStack mlist[MOVES_MAX], *last;
416 Square from = move_from(m);
417 Square to = move_to(m);
418 Piece pc = pos.piece_on(from);
419 int matches = 0, f = 0, r = 0;
421 // If there is only one piece 'pc' then move cannot be ambiguous
422 if (pos.piece_count(pos.side_to_move(), type_of_piece(pc)) == 1)
423 return AMBIGUITY_NONE;
425 // Collect all legal moves of piece 'pc' with destination 'to'
426 last = generate<MV_LEGAL>(pos, mlist);
427 for (MoveStack* cur = mlist; cur != last; cur++)
428 if (move_to(cur->move) == to && pos.piece_on(move_from(cur->move)) == pc)
429 candidates[matches++] = cur->move;
432 return AMBIGUITY_NONE;
434 for (int i = 0; i < matches; i++)
436 if (square_file(move_from(candidates[i])) == square_file(from))
439 if (square_rank(move_from(candidates[i])) == square_rank(from))
443 return f == 1 ? AMBIGUITY_FILE : r == 1 ? AMBIGUITY_RANK : AMBIGUITY_BOTH;
447 const string time_string(int millisecs) {
449 const int MSecMinute = 1000 * 60;
450 const int MSecHour = 1000 * 60 * 60;
453 s << std::setfill('0');
455 int hours = millisecs / MSecHour;
456 int minutes = (millisecs - hours * MSecHour) / MSecMinute;
457 int seconds = (millisecs - hours * MSecHour - minutes * MSecMinute) / 1000;
462 s << std::setw(2) << minutes << ':' << std::setw(2) << seconds;
467 const string score_string(Value v) {
471 if (v >= VALUE_MATE - 200)
472 s << "#" << (VALUE_MATE - v + 1) / 2;
473 else if (v <= -VALUE_MATE + 200)
474 s << "-#" << (VALUE_MATE + v) / 2;
477 float floatScore = float(v) / float(PawnValueMidgame);
481 s << std::setprecision(2) << std::fixed << floatScore;