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