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 Square from = move_from(m);
86 Square to = move_to(m);
87 Piece pc = pos.piece_on(move_from(m));
89 if (type_of_piece(pc) == PAWN)
91 if (pos.move_is_capture(m))
92 san += file_to_char(square_file(move_from(m)));
96 san += piece_type_to_char(type_of_piece(pc), true);
98 switch (move_ambiguity(pos, m)) {
104 san += file_to_char(square_file(from));
108 san += rank_to_char(square_rank(from));
112 san += square_to_string(from);
120 if (pos.move_is_capture(m))
123 san += square_to_string(move_to(m));
125 if (move_promotion(m))
128 san += piece_type_to_char(move_promotion(m), true);
132 // Is the move check? We don't use pos.move_is_check(m) here, because
133 // Position::move_is_check doesn't detect all checks (not castling moves,
134 // promotions and en passant captures).
139 san += p.is_mate()? "#" : "+";
145 /// move_from_san() takes a position and a string as input, and tries to
146 /// interpret the string as a move in short algebraic notation. On success,
147 /// the move is returned. On failure (i.e. if the string is unparsable, or
148 /// if the move is illegal or ambiguous), MOVE_NONE is returned.
150 Move move_from_san(const Position& pos, const std::string& movestr) {
154 MovePicker mp = MovePicker(pos, false, MOVE_NONE, EmptySearchStack, OnePly);
157 if (movestr == "O-O-O")
160 while ((m = mp.get_next_move()) != MOVE_NONE)
161 if (move_is_long_castle(m) && pos.pl_move_is_legal(m))
166 else if (movestr == "O-O")
169 while ((m = mp.get_next_move()) != MOVE_NONE)
170 if(move_is_short_castle(m) && pos.pl_move_is_legal(m))
177 const char *cstr = movestr.c_str();
183 // Initialize str[] by making a copy of movestr with the characters
184 // 'x', '=', '+' and '#' removed.
186 for(i=0, c=cstr; i<10 && *c!='\0' && *c!='\n' && *c!=' '; i++, c++)
187 if(!strchr("x=+#", *c)) {
188 *cc = strchr("nrq", *c)? toupper(*c) : *c;
193 size_t left = 0, right = strlen(str) - 1;
194 PieceType pt = NO_PIECE_TYPE, promotion;
196 File fromFile = FILE_NONE;
197 Rank fromRank = RANK_NONE;
200 if(strchr("BNRQ", str[right])) {
201 promotion = piece_type_from_char(str[right]);
205 promotion = NO_PIECE_TYPE;
207 // Find the moving piece:
209 if(strchr("BNRQK", str[left])) {
210 pt = piece_type_from_char(str[left]);
217 // Find the to square:
219 if(str[right] < '1' || str[right] > '8' ||
220 str[right-1] < 'a' || str[right-1] > 'h')
222 to = make_square(file_from_char(str[right-1]), rank_from_char(str[right]));
228 // Find the file and/or rank of the from square:
230 if(strchr("abcdefgh", str[left])) {
231 fromFile = file_from_char(str[left]);
234 if(strchr("12345678", str[left]))
235 fromRank = rank_from_char(str[left]);
238 // Look for a matching move:
239 Move m, move = MOVE_NONE;
242 while((m = mp.get_next_move()) != MOVE_NONE) {
244 if(pos.type_of_piece_on(move_from(m)) != pt)
246 else if(move_to(m) != to)
248 else if(move_promotion(m) != promotion)
250 else if(fromFile != FILE_NONE && fromFile != square_file(move_from(m)))
252 else if(fromRank != RANK_NONE && fromRank != square_rank(move_from(m)))
267 /// line_to_san() takes a position and a line (an array of moves representing
268 /// a sequence of legal moves from the position) as input, and returns a
269 /// string containing the line in short algebraic notation. If the boolean
270 /// parameter 'breakLines' is true, line breaks are inserted, with a line
271 /// length of 80 characters. After a line break, 'startColumn' spaces are
272 /// inserted at the beginning of the new line.
274 const std::string line_to_san(const Position& pos, Move line[], int startColumn, bool breakLines) {
276 Position p = Position(pos);
280 size_t length, maxLength;
283 maxLength = 80 - startColumn;
285 for(int i = 0; line[i] != MOVE_NONE; i++) {
286 moveStr = move_to_san(p, line[i]);
287 length += moveStr.length() + 1;
288 if(breakLines && length > maxLength) {
290 for(int j = 0; j < startColumn; j++)
292 length = moveStr.length() + 1;
296 if(line[i] == MOVE_NULL)
299 p.do_move(line[i], u);
306 /// pretty_pv() creates a human-readable string from a position and a PV.
307 /// It is used to write search information to the log file (which is created
308 /// when the UCI parameter "Use Search Log" is "true").
310 const std::string pretty_pv(const Position& pos, int time, int depth,
311 uint64_t nodes, Value score, Move pv[]) {
315 s << std::setw(2) << std::setfill(' ') << depth << " ";
318 s << std::setw(8) << score_string(score);
321 s << std::setw(8) << std::setfill(' ') << time_string(time) << " ";
324 if(nodes < 1000000ULL)
325 s << std::setw(8) << std::setfill(' ') << nodes << " ";
326 else if(nodes < 1000000000ULL)
327 s << std::setw(7) << std::setfill(' ') << nodes/1000ULL << 'k' << " ";
329 s << std::setw(7) << std::setfill(' ') << nodes/1000000ULL << 'M' << " ";
332 s << line_to_san(pos, pv, 30, true);
340 Ambiguity move_ambiguity(const Position& pos, Move m) {
342 Square from = move_from(m);
343 Square to = move_to(m);
344 Piece pc = pos.piece_on(from);
346 // King moves are never ambiguous, because there is never two kings of
348 if (type_of_piece(pc) == KING)
349 return AMBIGUITY_NONE;
351 MovePicker mp = MovePicker(pos, false, MOVE_NONE, EmptySearchStack, OnePly);
352 Move mv, moveList[8];
355 while ((mv = mp.get_next_move()) != MOVE_NONE)
356 if (move_to(mv) == to && pos.piece_on(move_from(mv)) == pc && pos.pl_move_is_legal(mv))
360 return AMBIGUITY_NONE;
363 for (int i = 0; i < n; i++)
365 if (square_file(move_from(moveList[i])) == square_file(from))
368 if (square_rank(move_from(moveList[i])) == square_rank(from))
372 return AMBIGUITY_FILE;
375 return AMBIGUITY_RANK;
377 return AMBIGUITY_BOTH;
381 const std::string time_string(int milliseconds) {
384 int hours = milliseconds / (1000 * 60 * 60);
385 int minutes = (milliseconds - hours*1000*60*60) / (60*1000);
386 int seconds = (milliseconds - hours*1000*60*60 - minutes*60*1000) / 1000;
390 s << std::setw(2) << std::setfill('0') << minutes << ':';
391 s << std::setw(2) << std::setfill('0') << seconds;
397 const std::string score_string(Value v) {
400 if(abs(v) >= VALUE_MATE - 200) {
402 s << "-#" << (VALUE_MATE + v) / 2;
404 s << "#" << (VALUE_MATE - v + 1) / 2;
407 float floatScore = float(v) / float(PawnValueMidgame);
410 s << std::setprecision(2) << std::fixed << floatScore;