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
47 // is actually a string stream built on a given input string.
49 typedef std::istringstream UCIInputParser;
51 // The root position. This is set up when the user (or in practice, the GUI)
52 // sends the "position" UCI command. The root position is sent to the think()
53 // function when the program receives the "go" command.
54 Position RootPosition;
58 void handle_command(const std::string &command);
59 void set_option(UCIInputParser &uip);
60 void set_position(UCIInputParser &uip);
61 void go(UCIInputParser &uip);
69 /// uci_main_loop() is the only global function in this file. It is
70 /// called immediately after the program has finished initializing.
71 /// The program remains in this loop until it receives the "quit" UCI
74 void uci_main_loop() {
76 RootPosition.from_fen(StartPosition);
89 // get_command() waits for a command from the user, and passes
90 // this command to handle_command. get_command also intercepts
91 // EOF from stdin, by translating EOF to the "quit" command. This
92 // ensures that Glaurung exits gracefully if the GUI dies
99 if (!std::getline(std::cin, command))
102 handle_command(command);
106 // handle_command() takes a text string as input, uses a
107 // UCIInputParser object to parse this text string as a UCI command,
108 // and calls the appropriate functions. In addition to the UCI
109 // commands, the function also supports a few debug commands.
111 void handle_command(const std::string &command) {
113 UCIInputParser uip(command);
116 uip >> token; // operator >> skips any whitespace
125 else if (token == "uci")
127 std::cout << "id name " << engine_name() << std::endl;
128 std::cout << "id author Tord Romstad" << std::endl;
130 std::cout << "uciok" << std::endl;
132 else if (token == "ucinewgame")
135 Position::init_piece_square_tables();
136 RootPosition.from_fen(StartPosition);
139 else if (token == "isready")
140 std::cout << "readyok" << std::endl;
141 else if (token == "position")
143 else if (token == "setoption")
145 else if (token == "go")
148 // The remaining commands are for debugging purposes only.
149 // Perhaps they should be removed later in order to reduce the
150 // size of the program binary.
151 else if (token == "d")
152 RootPosition.print();
153 else if (token == "flip")
155 Position p(RootPosition);
156 RootPosition.flipped_copy(p);
158 else if (token == "eval")
161 std::cout << "Incremental mg: " << RootPosition.mg_value()
163 std::cout << "Incremental eg: " << RootPosition.eg_value()
165 std::cout << "Full eval: "
166 << evaluate(RootPosition, ei, 0)
169 else if (token == "key")
171 std::cout << "key: " << RootPosition.get_key()
172 << " material key: " << RootPosition.get_material_key()
173 << " pawn key: " << RootPosition.get_pawn_key()
178 std::cout << "Unknown command: " << command << std::endl;
182 std::cout << token << std::endl;
188 // set_position() is called when Glaurung receives the "position" UCI
189 // command. The input parameter is a UCIInputParser. It is assumed
190 // that this parser has consumed the first token of the UCI command
191 // ("position"), and is ready to read the second token ("startpos"
192 // or "fen", if the input is well-formed).
194 void set_position(UCIInputParser &uip) {
198 uip >> token; // operator >> skips any whitespace
200 if (token == "startpos")
201 RootPosition.from_fen(StartPosition);
202 else if (token == "fen")
205 while (token != "moves" && !uip.eof())
211 RootPosition.from_fen(fen);
216 if (token != "moves")
218 if (token == "moves")
225 move = move_from_string(RootPosition, token);
226 RootPosition.do_move(move, u);
227 if (RootPosition.rule_50_counter() == 0)
228 RootPosition.reset_game_ply();
235 // set_option() is called when Glaurung receives the "setoption" UCI
236 // command. The input parameter is a UCIInputParser. It is assumed
237 // that this parser has consumed the first token of the UCI command
238 // ("setoption"), and is ready to read the second token ("name", if
239 // the input is well-formed).
241 void set_option(UCIInputParser &uip) {
243 std::string token, name;
250 while (!uip.eof() && token != "value")
252 name += (" " + token);
255 if (token == "value")
257 std::getline(uip, token); // reads until end of line
258 set_option_value(name, token);
265 // go() is called when Glaurung receives the "go" UCI command. The
266 // input parameter is a UCIInputParser. It is assumed that this
267 // parser has consumed the first token of the UCI command ("go"),
268 // and is ready to read the second token. The function sets the
269 // thinking time and other parameters from the input string, and
270 // calls think() (defined in search.cpp) with the appropriate
273 void go(UCIInputParser &uip) {
277 int time[2] = {0, 0}, inc[2] = {0, 0};
278 int movesToGo = 0, depth = 0, nodes = 0, moveTime = 0;
279 bool infinite = false, ponder = false;
280 Move searchMoves[500];
282 searchMoves[0] = MOVE_NONE;
288 if (token == "infinite")
290 else if (token == "ponder")
292 else if (token == "wtime")
294 else if (token == "btime")
296 else if (token == "winc")
298 else if (token == "binc")
300 else if (token == "movestogo")
302 else if (token == "depth")
304 else if (token == "nodes")
306 else if (token == "movetime")
308 else if (token == "searchmoves")
314 searchMoves[numOfMoves++] = move_from_string(RootPosition, token);
316 searchMoves[numOfMoves] = MOVE_NONE;
321 infinite = true; // HACK
323 think(RootPosition, infinite, ponder, RootPosition.side_to_move(), time,
324 inc, movesToGo, depth, nodes, moveTime, searchMoves);