]> git.sesse.net Git - stockfish/blob - src/uci.cpp
Retire push_button() and button_was_pressed()
[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         set_option_value("New Game", "true");
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     {
142         Position p(pos, pos.thread());
143         pos.flipped_copy(p);
144     }
145     else if (token == "eval")
146     {
147         Value evalMargin;
148         cout << "Incremental mg: "   << mg_value(pos.value())
149              << "\nIncremental eg: " << eg_value(pos.value())
150              << "\nFull eval: "      << evaluate(pos, evalMargin) << endl;
151     }
152     else if (token == "key")
153         cout << "key: " << hex << pos.get_key()
154              << "\nmaterial key: " << pos.get_material_key()
155              << "\npawn key: " << pos.get_pawn_key() << endl;
156     else if (token == "perft")
157         perft(pos, uip);
158     else
159         cout << "Unknown command: " << command << endl;
160
161     return true;
162   }
163
164
165   // set_position() is called when Stockfish receives the "position" UCI
166   // command. The input parameter is a UCIInputParser. It is assumed
167   // that this parser has consumed the first token of the UCI command
168   // ("position"), and is ready to read the second token ("startpos"
169   // or "fen", if the input is well-formed).
170
171   void set_position(Position& pos, UCIInputParser& uip) {
172
173     string token;
174
175     if (!(uip >> token)) // operator>>() skips any whitespace
176         return;
177
178     if (token == "startpos")
179         pos.from_fen(StartPositionFEN);
180     else if (token == "fen")
181     {
182         string fen;
183         while (uip >> token && token != "moves")
184         {
185             fen += token;
186             fen += ' ';
187         }
188         pos.from_fen(fen);
189     }
190
191     if (uip.good())
192     {
193         if (token != "moves")
194           uip >> token;
195
196         if (token == "moves")
197         {
198             Move move;
199             StateInfo st;
200             while (uip >> token)
201             {
202                 move = move_from_string(pos, token);
203                 pos.do_move(move, st);
204                 if (pos.rule_50_counter() == 0)
205                     pos.reset_game_ply();
206
207                 pos.inc_startpos_ply_counter(); //FIXME: make from_fen to support this and rule50
208             }
209             // Our StateInfo st is about going out of scope so copy
210             // its content inside pos before it disappears.
211             pos.detach();
212         }
213     }
214   }
215
216
217   // set_option() is called when Stockfish receives the "setoption" UCI
218   // command. The input parameter is a UCIInputParser. It is assumed
219   // that this parser has consumed the first token of the UCI command
220   // ("setoption"), and is ready to read the second token ("name", if
221   // the input is well-formed).
222
223   void set_option(UCIInputParser& uip) {
224
225     string token, name, value;
226
227     if (!(uip >> token)) // operator>>() skips any whitespace
228         return;
229
230     if (token != "name" || !(uip >> name))
231         return;
232
233     while (uip >> token && token != "value")
234         name += (" " + token);
235
236     if (token != "value" || !(uip >> value))
237     {
238         set_option_value(name, "true");
239         return;
240     }
241
242     while (uip >> token)
243         value += (" " + token);
244
245     set_option_value(name, value);
246   }
247
248
249   // go() is called when Stockfish receives the "go" UCI command. The
250   // input parameter is a UCIInputParser. It is assumed that this
251   // parser has consumed the first token of the UCI command ("go"),
252   // and is ready to read the second token. The function sets the
253   // thinking time and other parameters from the input string, and
254   // calls think() (defined in search.cpp) with the appropriate
255   // parameters. Returns false if a quit command is received while
256   // thinking, returns true otherwise.
257
258   bool go(Position& pos, UCIInputParser& uip) {
259
260     string token;
261
262     int time[2] = {0, 0}, inc[2] = {0, 0};
263     int movesToGo = 0, depth = 0, nodes = 0, moveTime = 0;
264     bool infinite = false, ponder = false;
265     Move searchMoves[MOVES_MAX];
266
267     searchMoves[0] = MOVE_NONE;
268
269     while (uip >> token)
270     {
271         if (token == "infinite")
272             infinite = true;
273         else if (token == "ponder")
274             ponder = true;
275         else if (token == "wtime")
276             uip >> time[0];
277         else if (token == "btime")
278             uip >> time[1];
279         else if (token == "winc")
280             uip >> inc[0];
281         else if (token == "binc")
282             uip >> inc[1];
283         else if (token == "movestogo")
284             uip >> movesToGo;
285         else if (token == "depth")
286             uip >> depth;
287         else if (token == "nodes")
288             uip >> nodes;
289         else if (token == "movetime")
290             uip >> moveTime;
291         else if (token == "searchmoves")
292         {
293             int numOfMoves = 0;
294             while (uip >> token)
295                 searchMoves[numOfMoves++] = move_from_string(pos, token);
296
297             searchMoves[numOfMoves] = MOVE_NONE;
298         }
299     }
300
301     assert(pos.is_ok());
302
303     return think(pos, infinite, ponder, time, inc, movesToGo,
304                  depth, nodes, moveTime, searchMoves);
305   }
306
307   void perft(Position& pos, UCIInputParser& uip) {
308
309     string token;
310     int depth, tm, n;
311
312     if (!(uip >> depth))
313         return;
314
315     tm = get_system_time();
316
317     n = perft(pos, depth * ONE_PLY);
318
319     tm = get_system_time() - tm;
320     std::cout << "\nNodes " << n
321               << "\nTime (ms) " << tm
322               << "\nNodes/second " << int(n / (tm / 1000.0)) << std::endl;
323   }
324 }