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