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