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
);
138 else if (token
== "isready")
139 std::cout
<< "readyok" << std::endl
;
140 else if (token
== "position")
142 else if (token
== "setoption")
144 else if (token
== "go")
147 // The remaining commands are for debugging purposes only.
148 // Perhaps they should be removed later in order to reduce the
149 // size of the program binary.
150 else if (token
== "d")
151 RootPosition
.print();
152 else if (token
== "flip")
154 Position
p(RootPosition
);
155 RootPosition
.flipped_copy(p
);
157 else if (token
== "eval")
160 std::cout
<< "Incremental mg: " << RootPosition
.mg_value()
162 std::cout
<< "Incremental eg: " << RootPosition
.eg_value()
164 std::cout
<< "Full eval: "
165 << evaluate(RootPosition
, ei
, 0)
168 else if (token
== "key")
170 std::cout
<< "key: " << RootPosition
.get_key()
171 << " material key: " << RootPosition
.get_material_key()
172 << " pawn key: " << RootPosition
.get_pawn_key()
177 std::cout
<< "Unknown command: " << command
<< std::endl
;
181 std::cout
<< token
<< std::endl
;
187 // set_position() is called when Glaurung receives the "position" UCI
188 // command. The input parameter is a UCIInputParser. It is assumed
189 // that this parser has consumed the first token of the UCI command
190 // ("position"), and is ready to read the second token ("startpos"
191 // or "fen", if the input is well-formed).
193 void set_position(UCIInputParser
&uip
) {
197 uip
>> token
; // operator >> skips any whitespace
199 if (token
== "startpos")
200 RootPosition
.from_fen(StartPosition
);
201 else if (token
== "fen")
204 while (token
!= "moves" && !uip
.eof())
210 RootPosition
.from_fen(fen
);
215 if (token
!= "moves")
217 if (token
== "moves")
224 move
= move_from_string(RootPosition
, token
);
225 RootPosition
.do_move(move
, u
);
226 if (RootPosition
.rule_50_counter() == 0)
227 RootPosition
.reset_game_ply();
234 // set_option() is called when Glaurung receives the "setoption" UCI
235 // command. The input parameter is a UCIInputParser. It is assumed
236 // that this parser has consumed the first token of the UCI command
237 // ("setoption"), and is ready to read the second token ("name", if
238 // the input is well-formed).
240 void set_option(UCIInputParser
&uip
) {
242 std::string token
, name
;
249 while (!uip
.eof() && token
!= "value")
251 name
+= (" " + token
);
254 if (token
== "value")
256 std::getline(uip
, token
); // reads until end of line
257 set_option_value(name
, token
);
264 // go() is called when Glaurung receives the "go" UCI command. The
265 // input parameter is a UCIInputParser. It is assumed that this
266 // parser has consumed the first token of the UCI command ("go"),
267 // and is ready to read the second token. The function sets the
268 // thinking time and other parameters from the input string, and
269 // calls think() (defined in search.cpp) with the appropriate
272 void go(UCIInputParser
&uip
) {
276 int time
[2] = {0, 0}, inc
[2] = {0, 0};
277 int movesToGo
= 0, depth
= 0, nodes
= 0, moveTime
= 0;
278 bool infinite
= false, ponder
= false;
279 Move searchMoves
[500];
281 searchMoves
[0] = MOVE_NONE
;
287 if (token
== "infinite")
289 else if (token
== "ponder")
291 else if (token
== "wtime")
293 else if (token
== "btime")
295 else if (token
== "winc")
297 else if (token
== "binc")
299 else if (token
== "movestogo")
301 else if (token
== "depth")
303 else if (token
== "nodes")
305 else if (token
== "movetime")
307 else if (token
== "searchmoves")
313 searchMoves
[numOfMoves
++] = move_from_string(RootPosition
, token
);
315 searchMoves
[numOfMoves
] = MOVE_NONE
;
320 infinite
= true; // HACK
322 think(RootPosition
, infinite
, ponder
, time
[RootPosition
.side_to_move()],
323 inc
[RootPosition
.side_to_move()], movesToGo
, depth
, nodes
, moveTime
,