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
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.
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.
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/>.
39 #include "ucioption.h"
43 //// Local definitions:
48 // UCIInputParser is a class for parsing UCI input. The class
49 // is actually a string stream built on a given input string.
51 typedef std::istringstream UCIInputParser;
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;
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);
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.
78 void uci_main_loop() {
80 RootPosition.from_fen(StartPosition);
84 // Wait for a command from stdin
85 if (!std::getline(std::cin, command))
88 } while (handle_command(command));
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.
103 bool handle_command(const std::string& command) {
105 UCIInputParser uip(command);
108 uip >> token; // operator >> skips any whitespace
118 std::cout << "id name " << engine_name() << std::endl
119 << "id author Tord Romstad, Marco Costalba"
122 std::cout << "uciok" << std::endl;
124 else if (token == "ucinewgame")
127 Position::init_piece_square_tables();
128 RootPosition.from_fen(StartPosition);
130 else if (token == "isready")
131 std::cout << "readyok" << std::endl;
132 else if (token == "position")
134 else if (token == "setoption")
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")
144 Position p(RootPosition);
145 RootPosition.flipped_copy(p);
147 else if (token == "eval")
150 std::cout << "Incremental mg: " << RootPosition.mg_value()
152 std::cout << "Incremental eg: " << RootPosition.eg_value()
154 std::cout << "Full eval: "
155 << evaluate(RootPosition, ei, 0)
158 else if (token == "key")
160 std::cout << "key: " << std::hex << RootPosition.get_key()
161 << " material key: " << RootPosition.get_material_key()
162 << " pawn key: " << RootPosition.get_pawn_key()
167 std::cout << "Unknown command: " << command << std::endl;
171 std::cout << token << std::endl;
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).
184 void set_position(UCIInputParser& uip) {
188 uip >> token; // operator >> skips any whitespace
190 if (token == "startpos")
191 RootPosition.from_fen(StartPosition);
192 else if (token == "fen")
195 while (token != "moves" && !uip.eof())
201 RootPosition.from_fen(fen);
206 if (token != "moves")
208 if (token == "moves")
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();
220 // Our StateInfo st is about going out of scope,
221 // so save its content before they disappear.
222 RootPosition.setStartState(st);
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).
234 void set_option(UCIInputParser& uip) {
236 std::string token, name;
245 if (token == "value")
248 name += (" " + token);
250 if (token == "value")
252 std::getline(uip, token); // reads until end of line
253 set_option_value(name, token);
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.
269 bool go(UCIInputParser& uip) {
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];
278 searchMoves[0] = MOVE_NONE;
284 if (token == "infinite")
286 else if (token == "ponder")
288 else if (token == "wtime")
290 else if (token == "btime")
292 else if (token == "winc")
294 else if (token == "binc")
296 else if (token == "movestogo")
298 else if (token == "depth")
300 else if (token == "nodes")
302 else if (token == "movetime")
304 else if (token == "searchmoves")
310 searchMoves[numOfMoves++] = move_from_string(RootPosition, token);
312 searchMoves[numOfMoves] = MOVE_NONE;
317 infinite = true; // HACK
319 assert(RootPosition.is_ok());
321 return think(RootPosition, infinite, ponder, RootPosition.side_to_move(), time,
322 inc, movesToGo, depth, nodes, moveTime, searchMoves);