2 Glaurung, a UCI chess playing engine.
3 Copyright (C) 2004-2008 Tord Romstad
5 Glaurung is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 Glaurung is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
35 //// Local definitions
52 Ambiguity move_ambiguity(Position &pos, Move m);
53 const std::string time_string(int milliseconds);
54 const std::string score_string(Value v);
62 /// move_to_san() takes a position and a move as input, where it is assumed
63 /// that the move is a legal move from the position. The return value is
64 /// a string containing the move in short algebraic notation.
66 const std::string move_to_san(Position &pos, Move m) {
70 assert(move_is_ok(m));
76 else if(m == MOVE_NULL) {
80 else if(move_is_long_castle(m))
82 else if(move_is_short_castle(m))
90 pc = pos.piece_on(move_from(m));
94 if(type_of_piece(pc) == PAWN) {
95 if(pos.move_is_capture(m))
96 str += file_to_char(square_file(move_from(m)));
99 str += piece_type_to_char(type_of_piece(pc), true);
101 Ambiguity amb = move_ambiguity(pos, m);
108 str += file_to_char(square_file(from));
112 str += rank_to_char(square_rank(from));
116 str += square_to_string(from);
124 if(pos.move_is_capture(m))
127 str += square_to_string(move_to(m));
129 if(move_promotion(m)) {
131 str += piece_type_to_char(move_promotion(m), true);
135 // Is the move check? We don't use pos.move_is_check(m) here, because
136 // Position::move_is_check doesn't detect all checks (not castling moves,
137 // promotions and en passant captures).
141 str += pos.is_mate()? "#" : "+";
148 /// move_from_san() takes a position and a string as input, and tries to
149 /// interpret the string as a move in short algebraic notation. On success,
150 /// the move is returned. On failure (i.e. if the string is unparsable, or
151 /// if the move is illegal or ambiguous), MOVE_NONE is returned.
153 Move move_from_san(Position &pos, const std::string &movestr) {
156 MovePicker mp = MovePicker(pos, false, MOVE_NONE, MOVE_NONE, MOVE_NONE,
160 if(movestr == "O-O-O") {
162 while((m = mp.get_next_move()) != MOVE_NONE)
163 if(move_is_long_castle(m) && pos.move_is_legal(m))
167 else if(movestr == "O-O") {
169 while((m = mp.get_next_move()) != MOVE_NONE)
170 if(move_is_short_castle(m) && pos.move_is_legal(m))
176 const char *cstr = movestr.c_str();
182 // Initialize str[] by making a copy of movestr with the characters
183 // 'x', '=', '+' and '#' removed.
185 for(i=0, c=cstr; i<10 && *c!='\0' && *c!='\n' && *c!=' '; i++, c++)
186 if(!strchr("x=+#", *c)) {
187 *cc = strchr("nrq", *c)? toupper(*c) : *c;
192 size_t left = 0, right = strlen(str) - 1;
193 PieceType pt = NO_PIECE_TYPE, promotion;
195 File fromFile = FILE_NONE;
196 Rank fromRank = RANK_NONE;
199 if(strchr("BNRQ", str[right])) {
200 promotion = piece_type_from_char(str[right]);
204 promotion = NO_PIECE_TYPE;
206 // Find the moving piece:
208 if(strchr("BNRQK", str[left])) {
209 pt = piece_type_from_char(str[left]);
216 // Find the to square:
218 if(str[right] < '1' || str[right] > '8' ||
219 str[right-1] < 'a' || str[right-1] > 'h')
221 to = make_square(file_from_char(str[right-1]), rank_from_char(str[right]));
227 // Find the file and/or rank of the from square:
229 if(strchr("abcdefgh", str[left])) {
230 fromFile = file_from_char(str[left]);
233 if(strchr("12345678", str[left]))
234 fromRank = rank_from_char(str[left]);
237 // Look for a matching move:
238 Move m, move = MOVE_NONE;
241 while((m = mp.get_next_move()) != MOVE_NONE) {
243 if(pos.type_of_piece_on(move_from(m)) != pt)
245 else if(move_to(m) != to)
247 else if(move_promotion(m) != promotion)
249 else if(fromFile != FILE_NONE && fromFile != square_file(move_from(m)))
251 else if(fromRank != RANK_NONE && fromRank != square_rank(move_from(m)))
266 /// line_to_san() takes a position and a line (an array of moves representing
267 /// a sequence of legal moves from the position) as input, and returns a
268 /// string containing the line in short algebraic notation. If the boolean
269 /// parameter 'breakLines' is true, line breaks are inserted, with a line
270 /// length of 80 characters. After a line break, 'startColumn' spaces are
271 /// inserted at the beginning of the new line.
273 const std::string line_to_san(const Position &pos, Move line[], int startColumn,
275 Position p = Position(pos);
279 size_t length, maxLength;
282 maxLength = 80 - startColumn;
284 for(int i = 0; line[i] != MOVE_NONE; i++) {
285 moveStr = move_to_san(p, line[i]);
286 length += moveStr.length() + 1;
287 if(breakLines && length > maxLength) {
289 for(int j = 0; j < startColumn; j++)
291 length = moveStr.length() + 1;
295 if(line[i] == MOVE_NULL)
298 p.do_move(line[i], u);
305 /// pretty_pv() creates a human-readable string from a position and a PV.
306 /// It is used to write search information to the log file (which is created
307 /// when the UCI parameter "Use Search Log" is "true").
309 const std::string pretty_pv(const Position &pos, int time, int depth,
310 uint64_t nodes, Value score, Move pv[]) {
314 s << std::setw(2) << std::setfill(' ') << depth << " ";
317 s << std::setw(8) << score_string(score);
320 s << std::setw(8) << std::setfill(' ') << time_string(time) << " ";
323 if(nodes < 1000000ULL)
324 s << std::setw(8) << std::setfill(' ') << nodes << " ";
325 else if(nodes < 1000000000ULL)
326 s << std::setw(7) << std::setfill(' ') << nodes/1000ULL << 'k' << " ";
328 s << std::setw(7) << std::setfill(' ') << nodes/1000000ULL << 'M' << " ";
331 s << line_to_san(pos, pv, 30, true);
339 Ambiguity move_ambiguity(Position &pos, Move m) {
345 pc = pos.piece_on(from);
347 // King moves are never ambiguous, because there is never two kings of
349 if(type_of_piece(pc) == KING)
350 return AMBIGUITY_NONE;
352 MovePicker mp = MovePicker(pos, false, MOVE_NONE, MOVE_NONE, MOVE_NONE,
354 Move mv, moveList[8];
358 while((mv = mp.get_next_move()) != MOVE_NONE)
359 if(move_to(mv) == to && pos.piece_on(move_from(mv)) == pc
360 && pos.move_is_legal(mv))
363 return AMBIGUITY_NONE;
366 for(i = 0; i < n; i++)
367 if(square_file(move_from(moveList[i])) == square_file(from))
370 return AMBIGUITY_FILE;
373 for(i = 0; i < n; i++)
374 if(square_rank(move_from(moveList[i])) == square_rank(from))
377 return AMBIGUITY_RANK;
379 return AMBIGUITY_BOTH;
383 const std::string time_string(int milliseconds) {
386 int hours = milliseconds / (1000 * 60 * 60);
387 int minutes = (milliseconds - hours*1000*60*60) / (60*1000);
388 int seconds = (milliseconds - hours*1000*60*60 - minutes*60*1000) / 1000;
392 s << std::setw(2) << std::setfill('0') << minutes << ':';
393 s << std::setw(2) << std::setfill('0') << seconds;
399 const std::string score_string(Value v) {
402 if(abs(v) >= VALUE_MATE - 200) {
404 s << "-#" << (VALUE_MATE + v) / 2;
406 s << "#" << (VALUE_MATE - v + 1) / 2;
409 float floatScore = float(v) / float(PawnValueMidgame);
412 s << std::setprecision(2) << std::fixed << floatScore;