2 Glaurung, a UCI chess playing engine.
3 Copyright (C) 2004-2008 Tord Romstad
5 Glaurung is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 Glaurung is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
37 #include "ucioption.h"
41 //// Local definitions:
46 // UCIInputParser is a class for parsing UCI input. The class is
47 // very simple, and basically just consist of a string stream
48 // built on a given input string. There are methods for checking
49 // if we are at the end of the line, for getting the next token
50 // (defined as any whitespace-delimited sequence of characters),
51 // and for getting the rest of the line as a single string.
53 class UCIInputParser {
56 UCIInputParser(const std::string &line);
57 std::string get_next_token();
58 std::string get_rest_of_line();
59 bool at_end_of_line();
62 std::istringstream inputLineStream;
67 // The root position. This is set up when the user (or in practice, the GUI)
68 // sends the "position" UCI command. The root position is sent to the think()
69 // function when the program receives the "go" command.
70 Position RootPosition;
73 void wait_for_command();
74 void handle_command(const std::string &command);
75 void set_option(UCIInputParser &uip);
76 void set_position(UCIInputParser &uip);
77 void go(UCIInputParser &uip);
85 /// uci_main_loop() is the only global function in this file. It is
86 /// called immediately after the program has finished initializing.
87 /// The program remains in this loop until it receives the "quit" UCI
90 void uci_main_loop() {
91 RootPosition.from_fen(StartPosition);
92 while(1) wait_for_command();
103 /// Implementation of the UCIInputParser class.
106 // Constructor for the UCIInputParser class. The constructor takes a
107 // text string containing a single UCI command as input.
109 UCIInputParser::UCIInputParser(const std::string &line) : inputLineStream(line) { }
112 // UCIInputParser::get_next_token() gets the next token in an UCI
113 // command. A 'token' in an UCI command is simply any
114 // whitespace-delimited sequence of characters.
116 std::string UCIInputParser::get_next_token() {
120 inputLineStream >> token; // operator >> skips any whitespace
126 // UCIInputParser::get_rest_of_line() returns the rest of the input
127 // line (from the current location) as a single string.
129 std::string UCIInputParser::get_rest_of_line() {
133 std::getline(inputLineStream, tail);
139 // UCIInputParser::at_end_of_line() tests whether we have reached the
140 // end of the input string, i.e. if any more input remains to be
143 bool UCIInputParser::at_end_of_line() {
145 return inputLineStream.eof();
154 // wait_for_command() waits for a command from the user, and passes
155 // this command to handle_command. wait_for_command also intercepts
156 // EOF from stdin, by translating EOF to the "quit" command. This
157 // ensures that Glaurung exits gracefully if the GUI dies
160 void wait_for_command() {
164 if (!std::getline(std::cin, command))
167 handle_command(command);
171 // handle_command() takes a text string as input, uses a
172 // UCIInputParser object to parse this text string as a UCI command,
173 // and calls the appropriate functions. In addition to the UCI
174 // commands, the function also supports a few debug commands.
176 void handle_command(const std::string &command) {
178 UCIInputParser uip(command);
179 std::string token = uip.get_next_token();
181 if(token == "quit") {
187 else if(token == "uci") {
188 std::cout << "id name " << engine_name() << std::endl;
189 std::cout << "id author Tord Romstad" << std::endl;
191 std::cout << "uciok" << std::endl;
193 else if(token == "ucinewgame") {
195 Position::init_piece_square_tables();
196 RootPosition.from_fen(StartPosition);
198 else if(token == "isready")
199 std::cout << "readyok" << std::endl;
200 else if(token == "position")
202 else if(token == "setoption")
204 else if(token == "go")
207 // The remaining commands are for debugging purposes only.
208 // Perhaps they should be removed later in order to reduce the
209 // size of the program binary.
210 else if(token == "d")
211 RootPosition.print();
212 else if(token == "flip") {
213 Position p(RootPosition);
214 RootPosition.flipped_copy(p);
216 else if(token == "eval") {
218 std::cout << "Incremental mg: " << RootPosition.mg_value()
220 std::cout << "Incremental eg: " << RootPosition.eg_value()
222 std::cout << "Full eval: "
223 << evaluate(RootPosition, ei, 0)
226 else if(token == "key") {
227 std::cout << "key: " << RootPosition.get_key()
228 << " material key: " << RootPosition.get_material_key()
229 << " pawn key: " << RootPosition.get_pawn_key()
233 std::cout << "Unknown command: " << command << std::endl;
234 while(!uip.at_end_of_line()) {
235 std::cout << uip.get_next_token() << std::endl;
241 // set_position() is called when Glaurung receives the "position" UCI
242 // command. The input parameter is a UCIInputParser. It is assumed
243 // that this parser has consumed the first token of the UCI command
244 // ("position"), and is ready to read the second token ("startpos"
245 // or "fen", if the input is well-formed).
247 void set_position(UCIInputParser &uip) {
251 token = uip.get_next_token();
252 if(token == "startpos")
253 RootPosition.from_fen(StartPosition);
254 else if(token == "fen") {
256 while(token != "moves" && !uip.at_end_of_line()) {
257 token = uip.get_next_token();
261 RootPosition.from_fen(fen);
264 if(!uip.at_end_of_line()) {
266 token = uip.get_next_token();
267 if(token == "moves") {
270 while(!uip.at_end_of_line()) {
271 token = uip.get_next_token();
272 move = move_from_string(RootPosition, token);
273 RootPosition.do_move(move, u);
274 if(RootPosition.rule_50_counter() == 0)
275 RootPosition.reset_game_ply();
282 // set_option() is called when Glaurung receives the "setoption" UCI
283 // command. The input parameter is a UCIInputParser. It is assumed
284 // that this parser has consumed the first token of the UCI command
285 // ("setoption"), and is ready to read the second token ("name", if
286 // the input is well-formed).
288 void set_option(UCIInputParser &uip) {
292 if(!uip.at_end_of_line()) {
293 token = uip.get_next_token();
294 if(token == "name" && !uip.at_end_of_line()) {
295 std::string name = uip.get_next_token();
296 std::string nextToken;
297 while(!uip.at_end_of_line()
298 && (nextToken = uip.get_next_token()) != "value")
299 name += (" " + nextToken);
300 if(nextToken == "value")
301 set_option_value(name, uip.get_rest_of_line());
309 // go() is called when Glaurung receives the "go" UCI command. The
310 // input parameter is a UCIInputParser. It is assumed that this
311 // parser has consumed the first token of the UCI command ("go"),
312 // and is ready to read the second token. The function sets the
313 // thinking time and other parameters from the input string, and
314 // calls think() (defined in search.cpp) with the appropriate
317 void go(UCIInputParser &uip) {
321 int time[2] = {0, 0}, inc[2] = {0, 0}, movesToGo = 0, depth = 0, nodes = 0;
323 bool infinite = false, ponder = false;
324 Move searchMoves[500];
326 searchMoves[0] = MOVE_NONE;
328 while(!uip.at_end_of_line()) {
329 token = uip.get_next_token();
331 if(token == "infinite")
333 else if(token == "ponder")
335 else if(token == "wtime") {
336 if(!uip.at_end_of_line())
337 time[0] = atoi(uip.get_next_token().c_str());
339 else if(token == "btime") {
340 if(!uip.at_end_of_line())
341 time[1] = atoi(uip.get_next_token().c_str());
343 else if(token == "winc") {
344 if(!uip.at_end_of_line())
345 inc[0] = atoi(uip.get_next_token().c_str());
347 else if(token == "binc") {
348 if(!uip.at_end_of_line())
349 inc[1] = atoi(uip.get_next_token().c_str());
351 else if(token == "movestogo") {
352 if(!uip.at_end_of_line())
353 movesToGo = atoi(uip.get_next_token().c_str());
355 else if(token == "depth") {
356 if(!uip.at_end_of_line())
357 depth = atoi(uip.get_next_token().c_str());
359 else if(token == "nodes") {
360 if(!uip.at_end_of_line())
361 nodes = atoi(uip.get_next_token().c_str());
363 else if(token == "movetime") {
364 if(!uip.at_end_of_line())
365 moveTime = atoi(uip.get_next_token().c_str());
367 else if(token == "searchmoves" && !uip.at_end_of_line()) {
369 while(!uip.at_end_of_line()) {
370 token = uip.get_next_token();
371 searchMoves[numOfMoves++] = move_from_string(RootPosition, token);
373 searchMoves[numOfMoves] = MOVE_NONE;
378 infinite = true; // HACK
380 think(RootPosition, infinite, ponder, time[RootPosition.side_to_move()],
381 inc[RootPosition.side_to_move()], movesToGo, depth, nodes, moveTime,