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"
44 //// Local definitions:
49 // UCIInputParser is a class for parsing UCI input. The class
50 // is actually a string stream built on a given input string.
52 typedef istringstream UCIInputParser;
54 // The root position. This is set up when the user (or in practice, the GUI)
55 // sends the "position" UCI command. The root position is sent to the think()
56 // function when the program receives the "go" command.
57 Position RootPosition;
60 bool handle_command(const string& command);
61 void set_option(UCIInputParser& uip);
62 void set_position(UCIInputParser& uip);
63 bool go(UCIInputParser& uip);
71 /// uci_main_loop() is the only global function in this file. It is
72 /// called immediately after the program has finished initializing.
73 /// The program remains in this loop until it receives the "quit" UCI
74 /// command. It waits for a command from the user, and passes this
75 /// command to handle_command and also intercepts EOF from stdin,
76 /// by translating EOF to the "quit" command. This ensures that Stockfish
77 /// exits gracefully if the GUI dies unexpectedly.
79 void uci_main_loop() {
81 RootPosition.from_fen(StartPosition);
85 // Wait for a command from stdin
86 if (!getline(cin, command))
89 } while (handle_command(command));
99 // handle_command() takes a text string as input, uses a
100 // UCIInputParser object to parse this text string as a UCI command,
101 // and calls the appropriate functions. In addition to the UCI
102 // commands, the function also supports a few debug commands.
104 bool handle_command(const string& command) {
106 UCIInputParser uip(command);
109 uip >> token; // operator>>() skips any whitespace
119 cout << "id name " << engine_name()
120 << "\nid author Tord Romstad, Marco Costalba, Joona Kiiski\n";
122 cout << "uciok" << endl;
124 else if (token == "ucinewgame")
126 push_button("Clear Hash");
127 Position::init_piece_square_tables();
128 RootPosition.from_fen(StartPosition);
130 else if (token == "isready")
131 cout << "readyok" << 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 cout << "Incremental mg: " << RootPosition.mg_value()
151 << "\nIncremental eg: " << RootPosition.eg_value()
152 << "\nFull eval: " << evaluate(RootPosition, ei, 0) << endl;
154 else if (token == "key")
155 cout << "key: " << hex << RootPosition.get_key()
156 << "\nmaterial key: " << RootPosition.get_material_key()
157 << "\npawn key: " << RootPosition.get_pawn_key() << endl;
160 cout << "Unknown command: " << command << endl;
164 cout << token << endl;
171 // set_position() is called when Stockfish receives the "position" UCI
172 // command. The input parameter is a UCIInputParser. It is assumed
173 // that this parser has consumed the first token of the UCI command
174 // ("position"), and is ready to read the second token ("startpos"
175 // or "fen", if the input is well-formed).
177 void set_position(UCIInputParser& uip) {
181 uip >> token; // operator>>() skips any whitespace
183 if (token == "startpos")
184 RootPosition.from_fen(StartPosition);
185 else if (token == "fen")
188 while (token != "moves" && !uip.eof())
194 RootPosition.from_fen(fen);
199 if (token != "moves")
201 if (token == "moves")
208 move = move_from_string(RootPosition, token);
209 RootPosition.do_move(move, st);
210 if (RootPosition.rule_50_counter() == 0)
211 RootPosition.reset_game_ply();
213 // Our StateInfo st is about going out of scope so copy
214 // its content inside RootPosition before they disappear.
215 RootPosition.saveState();
221 // set_option() is called when Stockfish receives the "setoption" UCI
222 // command. The input parameter is a UCIInputParser. It is assumed
223 // that this parser has consumed the first token of the UCI command
224 // ("setoption"), and is ready to read the second token ("name", if
225 // the input is well-formed).
227 void set_option(UCIInputParser& uip) {
238 if (token == "value")
241 name += (" " + token);
243 if (token == "value")
245 // Reads until end of line and left trim white space
247 token.erase(0, token.find_first_not_of(" \n\r\t"));
249 set_option_value(name, token);
256 // go() is called when Stockfish receives the "go" UCI command. The
257 // input parameter is a UCIInputParser. It is assumed that this
258 // parser has consumed the first token of the UCI command ("go"),
259 // and is ready to read the second token. The function sets the
260 // thinking time and other parameters from the input string, and
261 // calls think() (defined in search.cpp) with the appropriate
262 // parameters. Returns false if a quit command is received while
263 // thinking, returns true otherwise.
265 bool go(UCIInputParser& uip) {
269 int time[2] = {0, 0}, inc[2] = {0, 0};
270 int movesToGo = 0, depth = 0, nodes = 0, moveTime = 0;
271 bool infinite = false, ponder = false;
272 Move searchMoves[500];
274 searchMoves[0] = MOVE_NONE;
280 if (token == "infinite")
282 else if (token == "ponder")
284 else if (token == "wtime")
286 else if (token == "btime")
288 else if (token == "winc")
290 else if (token == "binc")
292 else if (token == "movestogo")
294 else if (token == "depth")
296 else if (token == "nodes")
298 else if (token == "movetime")
300 else if (token == "searchmoves")
306 searchMoves[numOfMoves++] = move_from_string(RootPosition, token);
308 searchMoves[numOfMoves] = MOVE_NONE;
313 infinite = true; // HACK
315 assert(RootPosition.is_ok());
317 return think(RootPosition, infinite, ponder, RootPosition.side_to_move(),
318 time, inc, movesToGo, depth, nodes, moveTime, searchMoves);