]> git.sesse.net Git - stockfish/blob - src/uci.cpp
Allow SearchStack to link an EvalInfo object
[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     if (!(uip >> token)) // operator>>() skips any whitespace
111         return true;
112
113     if (token == "quit")
114         return false;
115
116     if (token == "go")
117         return go(uip);
118
119     if (token == "uci")
120     {
121         cout << "id name " << engine_name()
122              << "\nid author Tord Romstad, Marco Costalba, Joona Kiiski\n";
123         print_uci_options();
124         cout << "uciok" << endl;
125     }
126     else if (token == "ucinewgame")
127     {
128         push_button("New Game");
129         Position::init_piece_square_tables();
130         RootPosition.from_fen(StartPosition);
131     }
132     else if (token == "isready")
133         cout << "readyok" << endl;
134     else if (token == "position")
135         set_position(uip);
136     else if (token == "setoption")
137         set_option(uip);
138
139     // The remaining commands are for debugging purposes only.
140     // Perhaps they should be removed later in order to reduce the
141     // size of the program binary.
142     else if (token == "d")
143         RootPosition.print();
144     else if (token == "flip")
145     {
146         Position p(RootPosition);
147         RootPosition.flipped_copy(p);
148     }
149     else if (token == "eval")
150     {
151         EvalInfo ei;
152         cout << "Incremental mg: " << mg_value(RootPosition.value())
153              << "\nIncremental eg: " << eg_value(RootPosition.value())
154              << "\nFull eval: " << evaluate(RootPosition, ei, 0) << endl;
155     }
156     else if (token == "key")
157         cout << "key: " << hex << RootPosition.get_key()
158              << "\nmaterial key: " << RootPosition.get_material_key()
159              << "\npawn key: " << RootPosition.get_pawn_key() << endl;
160     else if (token == "perft")
161         perft(uip);
162     else
163         cout << "Unknown command: " << command << endl;
164
165     return true;
166   }
167
168
169   // set_position() is called when Stockfish receives the "position" UCI
170   // command. The input parameter is a UCIInputParser. It is assumed
171   // that this parser has consumed the first token of the UCI command
172   // ("position"), and is ready to read the second token ("startpos"
173   // or "fen", if the input is well-formed).
174
175   void set_position(UCIInputParser& uip) {
176
177     string token;
178
179     if (!(uip >> token)) // operator>>() skips any whitespace
180         return;
181
182     if (token == "startpos")
183         RootPosition.from_fen(StartPosition);
184     else if (token == "fen")
185     {
186         string fen;
187         while (uip >> token && token != "moves")
188         {
189             fen += token;
190             fen += ' ';
191         }
192         RootPosition.from_fen(fen);
193     }
194
195     if (uip.good())
196     {
197         if (token != "moves")
198           uip >> token;
199
200         if (token == "moves")
201         {
202             Move move;
203             StateInfo st;
204             while (uip >> token)
205             {
206                 move = move_from_string(RootPosition, token);
207                 RootPosition.do_move(move, st);
208                 if (RootPosition.rule_50_counter() == 0)
209                     RootPosition.reset_game_ply();
210             }
211             // Our StateInfo st is about going out of scope so copy
212             // its content inside RootPosition before they disappear.
213             RootPosition.saveState();
214         }
215     }
216   }
217
218
219   // set_option() is called when Stockfish receives the "setoption" UCI
220   // command. The input parameter is a UCIInputParser. It is assumed
221   // that this parser has consumed the first token of the UCI command
222   // ("setoption"), and is ready to read the second token ("name", if
223   // the input is well-formed).
224
225   void set_option(UCIInputParser& uip) {
226
227     string token, name;
228
229     if (!(uip >> token)) // operator>>() skips any whitespace
230         return;
231
232     if (token == "name" && uip >> name)
233     {
234         while (uip >> token && token != "value")
235             name += (" " + token);
236
237         if (token == "value")
238         {
239             // Reads until end of line and left trim white space
240             getline(uip, token);
241             token.erase(0, token.find_first_not_of(" \n\r\t"));
242
243             set_option_value(name, token);
244         } else
245             push_button(name);
246     }
247   }
248
249
250   // go() is called when Stockfish receives the "go" UCI command. The
251   // input parameter is a UCIInputParser. It is assumed that this
252   // parser has consumed the first token of the UCI command ("go"),
253   // and is ready to read the second token. The function sets the
254   // thinking time and other parameters from the input string, and
255   // calls think() (defined in search.cpp) with the appropriate
256   // parameters. Returns false if a quit command is received while
257   // thinking, returns true otherwise.
258
259   bool go(UCIInputParser& uip) {
260
261     string token;
262
263     int time[2] = {0, 0}, inc[2] = {0, 0};
264     int movesToGo = 0, depth = 0, nodes = 0, moveTime = 0;
265     bool infinite = false, ponder = false;
266     Move searchMoves[500];
267
268     searchMoves[0] = MOVE_NONE;
269
270     while (uip >> token)
271     {
272         if (token == "infinite")
273             infinite = true;
274         else if (token == "ponder")
275             ponder = true;
276         else if (token == "wtime")
277             uip >> time[0];
278         else if (token == "btime")
279             uip >> time[1];
280         else if (token == "winc")
281             uip >> inc[0];
282         else if (token == "binc")
283             uip >> inc[1];
284         else if (token == "movestogo")
285             uip >> movesToGo;
286         else if (token == "depth")
287             uip >> depth;
288         else if (token == "nodes")
289             uip >> nodes;
290         else if (token == "movetime")
291             uip >> moveTime;
292         else if (token == "searchmoves")
293         {
294             int numOfMoves = 0;
295             while (!uip.eof())
296             {
297                 uip >> token;
298                 searchMoves[numOfMoves++] = move_from_string(RootPosition, token);
299             }
300             searchMoves[numOfMoves] = MOVE_NONE;
301         }
302     }
303
304     if (moveTime)
305         infinite = true;  // HACK
306
307     assert(RootPosition.is_ok());
308
309     return think(RootPosition, infinite, ponder, RootPosition.side_to_move(),
310                  time, inc, movesToGo, depth, nodes, moveTime, searchMoves);
311   }
312
313   void perft(UCIInputParser& uip) {
314
315     string token;
316     int depth, tm, n;
317     Position pos = RootPosition;
318
319     if (!(uip >> depth))
320         return;
321
322     tm = get_system_time();
323
324     n = perft(pos, depth * OnePly);
325
326     tm = get_system_time() - tm;
327     std::cout << "\nNodes " << n
328               << "\nTime (ms) " << tm
329               << "\nNodes/second " << (int)(n/(tm/1000.0)) << std::endl;
330   }
331 }