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