]> git.sesse.net Git - stockfish/blob - src/uci.cpp
22141cb37a7687826daa69afe5344441867bbb73
[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 <iostream>
27 #include <sstream>
28 #include <string>
29
30 #include "book.h"
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 "uci.h"
39 #include "ucioption.h"
40
41 using namespace std;
42
43 ////
44 //// Local definitions:
45 ////
46
47 namespace {
48
49   // FEN string for the initial position
50   const string StartPositionFEN = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
51
52   // UCIInputParser is a class for parsing UCI input. The class
53   // is actually a string stream built on a given input string.
54   typedef istringstream UCIInputParser;
55
56   // Local functions
57   bool handle_command(Position& pos, const string& command);
58   void set_option(UCIInputParser& uip);
59   void set_position(Position& pos, UCIInputParser& uip);
60   bool go(Position& pos, UCIInputParser& uip);
61   void perft(Position& pos, UCIInputParser& uip);
62 }
63
64
65 ////
66 //// Functions
67 ////
68
69 /// uci_main_loop() is the only global function in this file. It is
70 /// called immediately after the program has finished initializing.
71 /// The program remains in this loop until it receives the "quit" UCI
72 /// command. It waits for a command from the user, and passes this
73 /// command to handle_command and also intercepts EOF from stdin,
74 /// by translating EOF to the "quit" command. This ensures that Stockfish
75 /// exits gracefully if the GUI dies unexpectedly.
76
77 void uci_main_loop() {
78
79   Position pos(StartPositionFEN, 0); // The root position
80   string command;
81
82   do {
83       // Wait for a command from stdin
84       if (!getline(cin, command))
85           command = "quit";
86
87   } while (handle_command(pos, command));
88 }
89
90
91 ////
92 //// Local functions
93 ////
94
95 namespace {
96
97   // handle_command() takes a text string as input, uses a
98   // UCIInputParser object to parse this text string as a UCI command,
99   // and calls the appropriate functions. In addition to the UCI
100   // commands, the function also supports a few debug commands.
101
102   bool handle_command(Position& pos, const string& command) {
103
104     UCIInputParser uip(command);
105     string token;
106
107     if (!(uip >> token)) // operator>>() skips any whitespace
108         return true;
109
110     if (token == "quit")
111         return false;
112
113     if (token == "go")
114         return go(pos, uip);
115
116     if (token == "uci")
117     {
118         cout << "id name " << engine_name()
119              << "\nid author Tord Romstad, Marco Costalba, Joona Kiiski\n";
120         print_uci_options();
121         cout << "uciok" << endl;
122     }
123     else if (token == "ucinewgame")
124     {
125         push_button("New Game");
126         pos.from_fen(StartPositionFEN);
127     }
128     else if (token == "isready")
129         cout << "readyok" << endl;
130     else if (token == "position")
131         set_position(pos, uip);
132     else if (token == "setoption")
133         set_option(uip);
134
135     // The remaining commands are for debugging purposes only.
136     // Perhaps they should be removed later in order to reduce the
137     // size of the program binary.
138     else if (token == "d")
139         pos.print();
140     else if (token == "flip")
141         pos.flipped_copy(Position(pos, pos.thread()));
142     else if (token == "eval")
143     {
144         Value evalMargin;
145         cout << "Incremental mg: "   << mg_value(pos.value())
146              << "\nIncremental eg: " << eg_value(pos.value())
147              << "\nFull eval: "      << evaluate(pos, evalMargin) << endl;
148     }
149     else if (token == "key")
150         cout << "key: " << hex << pos.get_key()
151              << "\nmaterial key: " << pos.get_material_key()
152              << "\npawn key: " << pos.get_pawn_key() << endl;
153     else if (token == "perft")
154         perft(pos, uip);
155     else
156         cout << "Unknown command: " << command << endl;
157
158     return true;
159   }
160
161
162   // set_position() is called when Stockfish receives the "position" UCI
163   // command. The input parameter is a UCIInputParser. It is assumed
164   // that this parser has consumed the first token of the UCI command
165   // ("position"), and is ready to read the second token ("startpos"
166   // or "fen", if the input is well-formed).
167
168   void set_position(Position& pos, UCIInputParser& uip) {
169
170     string token;
171
172     if (!(uip >> token)) // operator>>() skips any whitespace
173         return;
174
175     if (token == "startpos")
176         pos.from_fen(StartPositionFEN);
177     else if (token == "fen")
178     {
179         string fen;
180         while (uip >> token && token != "moves")
181         {
182             fen += token;
183             fen += ' ';
184         }
185         pos.from_fen(fen);
186     }
187
188     if (uip.good())
189     {
190         if (token != "moves")
191           uip >> token;
192
193         if (token == "moves")
194         {
195             Move move;
196             StateInfo st;
197             while (uip >> token)
198             {
199                 move = move_from_string(pos, token);
200                 pos.do_move(move, st);
201                 if (pos.rule_50_counter() == 0)
202                     pos.reset_game_ply();
203
204                 pos.inc_startpos_ply_counter(); //FIXME: make from_fen to support this and rule50
205             }
206             // Our StateInfo st is about going out of scope so copy
207             // its content inside pos before it disappears.
208             pos.detach();
209         }
210     }
211   }
212
213
214   // set_option() is called when Stockfish receives the "setoption" UCI
215   // command. The input parameter is a UCIInputParser. It is assumed
216   // that this parser has consumed the first token of the UCI command
217   // ("setoption"), and is ready to read the second token ("name", if
218   // the input is well-formed).
219
220   void set_option(UCIInputParser& uip) {
221
222     string token, name, value;
223
224     if (!(uip >> token)) // operator>>() skips any whitespace
225         return;
226
227     if (token == "name" && uip >> name)
228     {
229         while (uip >> token && token != "value")
230             name += (" " + token);
231
232         if (token == "value" && uip >> value)
233         {
234             while (uip >> token)
235                 value += (" " + token);
236
237             set_option_value(name, value);
238         } else
239             push_button(name);
240     }
241   }
242
243
244   // go() is called when Stockfish receives the "go" UCI command. The
245   // input parameter is a UCIInputParser. It is assumed that this
246   // parser has consumed the first token of the UCI command ("go"),
247   // and is ready to read the second token. The function sets the
248   // thinking time and other parameters from the input string, and
249   // calls think() (defined in search.cpp) with the appropriate
250   // parameters. Returns false if a quit command is received while
251   // thinking, returns true otherwise.
252
253   bool go(Position& pos, UCIInputParser& uip) {
254
255     string token;
256
257     int time[2] = {0, 0}, inc[2] = {0, 0};
258     int movesToGo = 0, depth = 0, nodes = 0, moveTime = 0;
259     bool infinite = false, ponder = false;
260     Move searchMoves[500];
261
262     searchMoves[0] = MOVE_NONE;
263
264     while (uip >> token)
265     {
266         if (token == "infinite")
267             infinite = true;
268         else if (token == "ponder")
269             ponder = true;
270         else if (token == "wtime")
271             uip >> time[0];
272         else if (token == "btime")
273             uip >> time[1];
274         else if (token == "winc")
275             uip >> inc[0];
276         else if (token == "binc")
277             uip >> inc[1];
278         else if (token == "movestogo")
279             uip >> movesToGo;
280         else if (token == "depth")
281             uip >> depth;
282         else if (token == "nodes")
283             uip >> nodes;
284         else if (token == "movetime")
285             uip >> moveTime;
286         else if (token == "searchmoves")
287         {
288             int numOfMoves = 0;
289             while (uip >> token)
290                 searchMoves[numOfMoves++] = move_from_string(pos, token);
291
292             searchMoves[numOfMoves] = MOVE_NONE;
293         }
294     }
295
296     assert(pos.is_ok());
297
298     return think(pos, infinite, ponder, time, inc, movesToGo,
299                  depth, nodes, moveTime, searchMoves);
300   }
301
302   void perft(Position& pos, UCIInputParser& uip) {
303
304     string token;
305     int depth, tm, n;
306
307     if (!(uip >> depth))
308         return;
309
310     tm = get_system_time();
311
312     n = perft(pos, depth * ONE_PLY);
313
314     tm = get_system_time() - tm;
315     std::cout << "\nNodes " << n
316               << "\nTime (ms) " << tm
317               << "\nNodes/second " << (int)(n/(tm/1000.0)) << std::endl;
318   }
319 }