]> git.sesse.net Git - stockfish/blob - src/uci.cpp
Fix reading a book under-promotion move
[stockfish] / src / uci.cpp
1 /*
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
5
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.
10
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.
15
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/>.
18 */
19
20
21 ////
22 //// Includes
23 ////
24
25 #include <cassert>
26 #include <cctype>
27 #include <iostream>
28 #include <sstream>
29 #include <string>
30
31 #include "evaluate.h"
32 #include "misc.h"
33 #include "move.h"
34 #include "movegen.h"
35 #include "position.h"
36 #include "san.h"
37 #include "search.h"
38 #include "ucioption.h"
39
40 using namespace std;
41
42
43 namespace {
44
45   // FEN string for the initial position
46   const string StartPositionFEN = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
47
48   // UCIParser is a class for parsing UCI input. The class
49   // is actually a string stream built on a given input string.
50   typedef istringstream UCIParser;
51
52   // Local functions
53   void set_option(UCIParser& up);
54   void set_position(Position& pos, UCIParser& up);
55   bool go(Position& pos, UCIParser& up);
56   void perft(Position& pos, UCIParser& up);
57   Move parse_uci_move(const Position& pos, const std::string &str);
58 }
59
60
61 /// execute_uci_command() takes a string as input, uses a UCIParser
62 /// object to parse this text string as a UCI command, and calls
63 /// the appropriate functions. In addition to the UCI commands,
64 /// the function also supports a few debug commands.
65
66 bool execute_uci_command(const string& cmd) {
67
68   static Position pos(StartPositionFEN, false, 0); // The root position
69   UCIParser up(cmd);
70   string token;
71
72   if (!(up >> token)) // operator>>() skips any whitespace
73       return true;
74
75   if (token == "quit")
76       return false;
77
78   if (token == "go")
79       return go(pos, up);
80
81   if (token == "uci")
82   {
83       cout << "id name " << engine_name()
84            << "\nid author Tord Romstad, Marco Costalba, Joona Kiiski\n";
85       print_uci_options();
86       cout << "uciok" << endl;
87   }
88   else if (token == "ucinewgame")
89       pos.from_fen(StartPositionFEN, false);
90
91   else if (token == "isready")
92       cout << "readyok" << endl;
93
94   else if (token == "position")
95       set_position(pos, up);
96
97   else if (token == "setoption")
98       set_option(up);
99
100   // The remaining commands are for debugging purposes only
101   else if (token == "d")
102       pos.print();
103
104   else if (token == "flip")
105   {
106       Position p(pos, pos.thread());
107       pos.flipped_copy(p);
108   }
109   else if (token == "eval")
110   {
111       Value evalMargin;
112       cout << "Incremental mg: "   << mg_value(pos.value())
113            << "\nIncremental eg: " << eg_value(pos.value())
114            << "\nFull eval: "      << evaluate(pos, evalMargin) << endl;
115   }
116   else if (token == "key")
117       cout << "key: " << hex << pos.get_key()
118            << "\nmaterial key: " << pos.get_material_key()
119            << "\npawn key: " << pos.get_pawn_key() << endl;
120
121   else if (token == "perft")
122       perft(pos, up);
123
124   else
125       cout << "Unknown command: " << cmd << endl;
126
127   return true;
128 }
129
130
131 ////
132 //// Local functions
133 ////
134
135 namespace {
136
137   // parse_uci_move() takes a position and a string as input, and attempts to
138   // convert the string to a move, using simple coordinate notation (g1f3,
139   // a7a8q, etc.). In order to correctly parse en passant captures and castling
140   // moves, we need the position. This function is not robust, and expects that
141   // the input move is legal and correctly formatted.
142
143   Move parse_uci_move(const Position& pos, const std::string& str) {
144
145     Square from, to;
146     Piece piece;
147     Color us = pos.side_to_move();
148
149     if (str.length() < 4)
150         return MOVE_NONE;
151
152     // Read the from and to squares
153     from = make_square(file_from_char(str[0]), rank_from_char(str[1]));
154     to   = make_square(file_from_char(str[2]), rank_from_char(str[3]));
155
156     // Find the moving piece
157     piece = pos.piece_on(from);
158
159     // If the string has more than 4 characters, try to interpret the 5th
160     // character as a promotion.
161     if (str.length() > 4 && piece == piece_of_color_and_type(us, PAWN))
162     {
163         switch (tolower(str[4])) {
164         case 'n':
165             return make_promotion_move(from, to, KNIGHT);
166         case 'b':
167             return make_promotion_move(from, to, BISHOP);
168         case 'r':
169             return make_promotion_move(from, to, ROOK);
170         case 'q':
171             return make_promotion_move(from, to, QUEEN);
172         }
173     }
174
175     // En passant move? We assume that a pawn move is an en passant move
176     // if the destination square is epSquare.
177     if (to == pos.ep_square() && piece == piece_of_color_and_type(us, PAWN))
178         return make_ep_move(from, to);
179
180     // Is this a castling move? A king move is assumed to be a castling move
181     // if the destination square is occupied by a friendly rook, or if the
182     // distance between the source and destination squares is more than 1.
183     if (piece == piece_of_color_and_type(us, KING))
184     {
185         if (pos.piece_on(to) == piece_of_color_and_type(us, ROOK))
186             return make_castle_move(from, to);
187
188         if (square_distance(from, to) > 1)
189         {
190             // This is a castling move, but we have to translate it to the
191             // internal "king captures rook" representation.
192             SquareDelta delta = (to > from ? DELTA_E : DELTA_W);
193             Square s = from;
194
195             do s += delta;
196             while (   pos.piece_on(s) != piece_of_color_and_type(us, ROOK)
197                    && relative_rank(us, s) == RANK_1);
198
199             return relative_rank(us, s) == RANK_1 ? make_castle_move(from, s) : MOVE_NONE;
200         }
201     }
202
203     return make_move(from, to);
204   }
205
206   // set_position() is called when Stockfish receives the "position" UCI
207   // command. The input parameter is a UCIParser. It is assumed
208   // that this parser has consumed the first token of the UCI command
209   // ("position"), and is ready to read the second token ("startpos"
210   // or "fen", if the input is well-formed).
211
212   void set_position(Position& pos, UCIParser& up) {
213
214     string token;
215
216     if (!(up >> token) || (token != "startpos" && token != "fen"))
217         return;
218
219     if (token == "startpos")
220     {
221         pos.from_fen(StartPositionFEN, false);
222         if (!(up >> token))
223             return;
224     }
225     else // fen
226     {
227         string fen;
228         while (up >> token && token != "moves")
229         {
230             fen += token;
231             fen += ' ';
232         }
233         pos.from_fen(fen, Options["UCI_Chess960"].value<bool>());
234     }
235
236     if (token != "moves")
237         return;
238
239     // Parse optional move list
240     Move move;
241     StateInfo st;
242     while (up >> token)
243     {
244         move = parse_uci_move(pos, token);
245         pos.do_setup_move(move, st);
246     }
247     // Our StateInfo st is about going out of scope so copy
248     // its content inside pos before it disappears.
249     pos.detach();
250   }
251
252
253   // set_option() is called when Stockfish receives the "setoption" UCI
254   // command. The input parameter is a UCIParser. It is assumed
255   // that this parser has consumed the first token of the UCI command
256   // ("setoption"), and is ready to read the second token ("name", if
257   // the input is well-formed).
258
259   void set_option(UCIParser& up) {
260
261     string token, name, value;
262
263     if (!(up >> token) || token != "name") // operator>>() skips any whitespace
264         return;
265
266     if (!(up >> name))
267         return;
268
269     // Handle names with included spaces
270     while (up >> token && token != "value")
271         name += (" " + token);
272
273     if (Options.find(name) == Options.end())
274     {
275         cout << "No such option: " << name << endl;
276         return;
277     }
278
279     // Is a button ?
280     if (token != "value")
281     {
282         Options[name].set_value("true");
283         return;
284     }
285
286     if (!(up >> value))
287         return;
288
289     // Handle values with included spaces
290     while (up >> token)
291         value += (" " + token);
292
293     Options[name].set_value(value);
294   }
295
296
297   // go() is called when Stockfish receives the "go" UCI command. The
298   // input parameter is a UCIParser. It is assumed that this
299   // parser has consumed the first token of the UCI command ("go"),
300   // and is ready to read the second token. The function sets the
301   // thinking time and other parameters from the input string, and
302   // calls think() (defined in search.cpp) with the appropriate
303   // parameters. Returns false if a quit command is received while
304   // thinking, returns true otherwise.
305
306   bool go(Position& pos, UCIParser& up) {
307
308     string token;
309
310     int time[2] = {0, 0}, inc[2] = {0, 0};
311     int movesToGo = 0, depth = 0, nodes = 0, moveTime = 0;
312     bool infinite = false, ponder = false;
313     Move searchMoves[MOVES_MAX];
314
315     searchMoves[0] = MOVE_NONE;
316
317     while (up >> token)
318     {
319         if (token == "infinite")
320             infinite = true;
321         else if (token == "ponder")
322             ponder = true;
323         else if (token == "wtime")
324             up >> time[0];
325         else if (token == "btime")
326             up >> time[1];
327         else if (token == "winc")
328             up >> inc[0];
329         else if (token == "binc")
330             up >> inc[1];
331         else if (token == "movestogo")
332             up >> movesToGo;
333         else if (token == "depth")
334             up >> depth;
335         else if (token == "nodes")
336             up >> nodes;
337         else if (token == "movetime")
338             up >> moveTime;
339         else if (token == "searchmoves")
340         {
341             int numOfMoves = 0;
342             while (up >> token)
343                 searchMoves[numOfMoves++] = parse_uci_move(pos, token);
344
345             searchMoves[numOfMoves] = MOVE_NONE;
346         }
347     }
348
349     assert(pos.is_ok());
350
351     return think(pos, infinite, ponder, time, inc, movesToGo,
352                  depth, nodes, moveTime, searchMoves);
353   }
354
355   void perft(Position& pos, UCIParser& up) {
356
357     int depth, tm;
358     int64_t n;
359
360     if (!(up >> depth))
361         return;
362
363     tm = get_system_time();
364
365     n = perft(pos, depth * ONE_PLY);
366
367     tm = get_system_time() - tm;
368     std::cout << "\nNodes " << n
369               << "\nTime (ms) " << tm
370               << "\nNodes/second " << int(n / (tm / 1000.0)) << std::endl;
371   }
372 }