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