]> git.sesse.net Git - stockfish/blob - src/uci.cpp
Fix MinGW warnings
[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         push_button("New Game");
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     {
232         while (uip >> token && token != "value")
233             name += (" " + token);
234
235         if (token == "value" && uip >> value)
236         {
237             while (uip >> token)
238                 value += (" " + token);
239
240             set_option_value(name, value);
241         } else
242             push_button(name);
243     }
244   }
245
246
247   // go() is called when Stockfish receives the "go" UCI command. The
248   // input parameter is a UCIInputParser. It is assumed that this
249   // parser has consumed the first token of the UCI command ("go"),
250   // and is ready to read the second token. The function sets the
251   // thinking time and other parameters from the input string, and
252   // calls think() (defined in search.cpp) with the appropriate
253   // parameters. Returns false if a quit command is received while
254   // thinking, returns true otherwise.
255
256   bool go(Position& pos, UCIInputParser& uip) {
257
258     string token;
259
260     int time[2] = {0, 0}, inc[2] = {0, 0};
261     int movesToGo = 0, depth = 0, nodes = 0, moveTime = 0;
262     bool infinite = false, ponder = false;
263     Move searchMoves[500];
264
265     searchMoves[0] = MOVE_NONE;
266
267     while (uip >> token)
268     {
269         if (token == "infinite")
270             infinite = true;
271         else if (token == "ponder")
272             ponder = true;
273         else if (token == "wtime")
274             uip >> time[0];
275         else if (token == "btime")
276             uip >> time[1];
277         else if (token == "winc")
278             uip >> inc[0];
279         else if (token == "binc")
280             uip >> inc[1];
281         else if (token == "movestogo")
282             uip >> movesToGo;
283         else if (token == "depth")
284             uip >> depth;
285         else if (token == "nodes")
286             uip >> nodes;
287         else if (token == "movetime")
288             uip >> moveTime;
289         else if (token == "searchmoves")
290         {
291             int numOfMoves = 0;
292             while (uip >> token)
293                 searchMoves[numOfMoves++] = move_from_string(pos, token);
294
295             searchMoves[numOfMoves] = MOVE_NONE;
296         }
297     }
298
299     assert(pos.is_ok());
300
301     return think(pos, infinite, ponder, time, inc, movesToGo,
302                  depth, nodes, moveTime, searchMoves);
303   }
304
305   void perft(Position& pos, UCIInputParser& uip) {
306
307     string token;
308     int depth, tm, n;
309
310     if (!(uip >> depth))
311         return;
312
313     tm = get_system_time();
314
315     n = perft(pos, depth * ONE_PLY);
316
317     tm = get_system_time() - tm;
318     std::cout << "\nNodes " << n
319               << "\nTime (ms) " << tm
320               << "\nNodes/second " << (int)(n/(tm/1000.0)) << std::endl;
321   }
322 }