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 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/>.
38 #include "ucioption.h"
42 //// Local definitions:
47 // UCIInputParser is a class for parsing UCI input. The class
48 // is actually a string stream built on a given input string.
50 typedef std::istringstream UCIInputParser;
52 // The root position. This is set up when the user (or in practice, the GUI)
53 // sends the "position" UCI command. The root position is sent to the think()
54 // function when the program receives the "go" command.
55 Position RootPosition;
59 void handle_command(const std::string &command);
60 void set_option(UCIInputParser &uip);
61 void set_position(UCIInputParser &uip);
62 void 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
75 void uci_main_loop() {
77 RootPosition.from_fen(StartPosition);
90 // get_command() waits for a command from the user, and passes
91 // this command to handle_command. get_command also intercepts
92 // EOF from stdin, by translating EOF to the "quit" command. This
93 // ensures that Stockfish exits gracefully if the GUI dies
100 if (!std::getline(std::cin, command))
103 handle_command(command);
107 // handle_command() takes a text string as input, uses a
108 // UCIInputParser object to parse this text string as a UCI command,
109 // and calls the appropriate functions. In addition to the UCI
110 // commands, the function also supports a few debug commands.
112 void handle_command(const std::string &command) {
114 UCIInputParser uip(command);
117 uip >> token; // operator >> skips any whitespace
126 else if (token == "uci")
128 std::cout << "id name " << engine_name() << std::endl
129 << "id author Tord Romstad, Marco Costalba"
132 std::cout << "uciok" << std::endl;
134 else if (token == "ucinewgame")
137 Position::init_piece_square_tables();
138 RootPosition.from_fen(StartPosition);
140 else if (token == "isready")
141 std::cout << "readyok" << std::endl;
142 else if (token == "position")
144 else if (token == "setoption")
146 else if (token == "go")
149 // The remaining commands are for debugging purposes only.
150 // Perhaps they should be removed later in order to reduce the
151 // size of the program binary.
152 else if (token == "d")
153 RootPosition.print();
154 else if (token == "flip")
156 Position p(RootPosition);
157 RootPosition.flipped_copy(p);
159 else if (token == "eval")
162 std::cout << "Incremental mg: " << RootPosition.mg_value()
164 std::cout << "Incremental eg: " << RootPosition.eg_value()
166 std::cout << "Full eval: "
167 << evaluate(RootPosition, ei, 0)
170 else if (token == "key")
172 std::cout << "key: " << RootPosition.get_key()
173 << " material key: " << RootPosition.get_material_key()
174 << " pawn key: " << RootPosition.get_pawn_key()
179 std::cout << "Unknown command: " << command << std::endl;
183 std::cout << token << std::endl;
189 // set_position() is called when Stockfish receives the "position" UCI
190 // command. The input parameter is a UCIInputParser. It is assumed
191 // that this parser has consumed the first token of the UCI command
192 // ("position"), and is ready to read the second token ("startpos"
193 // or "fen", if the input is well-formed).
195 void set_position(UCIInputParser &uip) {
199 uip >> token; // operator >> skips any whitespace
201 if (token == "startpos")
202 RootPosition.from_fen(StartPosition);
203 else if (token == "fen")
206 while (token != "moves" && !uip.eof())
212 RootPosition.from_fen(fen);
217 if (token != "moves")
219 if (token == "moves")
226 move = move_from_string(RootPosition, token);
227 RootPosition.do_move(move, st);
228 if (RootPosition.rule_50_counter() == 0)
229 RootPosition.reset_game_ply();
236 // set_option() is called when Stockfish receives the "setoption" UCI
237 // command. The input parameter is a UCIInputParser. It is assumed
238 // that this parser has consumed the first token of the UCI command
239 // ("setoption"), and is ready to read the second token ("name", if
240 // the input is well-formed).
242 void set_option(UCIInputParser &uip) {
244 std::string token, name;
251 while (!uip.eof() && token != "value")
253 name += (" " + token);
256 if (token == "value")
258 std::getline(uip, token); // reads until end of line
259 set_option_value(name, token);
266 // go() is called when Stockfish receives the "go" UCI command. The
267 // input parameter is a UCIInputParser. It is assumed that this
268 // parser has consumed the first token of the UCI command ("go"),
269 // and is ready to read the second token. The function sets the
270 // thinking time and other parameters from the input string, and
271 // calls think() (defined in search.cpp) with the appropriate
274 void go(UCIInputParser &uip) {
278 int time[2] = {0, 0}, inc[2] = {0, 0};
279 int movesToGo = 0, depth = 0, nodes = 0, moveTime = 0;
280 bool infinite = false, ponder = false;
281 Move searchMoves[500];
283 searchMoves[0] = MOVE_NONE;
289 if (token == "infinite")
291 else if (token == "ponder")
293 else if (token == "wtime")
295 else if (token == "btime")
297 else if (token == "winc")
299 else if (token == "binc")
301 else if (token == "movestogo")
303 else if (token == "depth")
305 else if (token == "nodes")
307 else if (token == "movetime")
309 else if (token == "searchmoves")
315 searchMoves[numOfMoves++] = move_from_string(RootPosition, token);
317 searchMoves[numOfMoves] = MOVE_NONE;
322 infinite = true; // HACK
324 think(RootPosition, infinite, ponder, RootPosition.side_to_move(), time,
325 inc, movesToGo, depth, nodes, moveTime, searchMoves);