]> git.sesse.net Git - stockfish/blob - src/uci.cpp
Use printf() instead of std::cout()
[stockfish] / src / uci.cpp
1 /*
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-2010 Marco Costalba, Joona Kiiski, Tord Romstad
5
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.
10
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.
15
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/>.
18 */
19
20 #include <iostream>
21 #include <sstream>
22 #include <string>
23 #include <vector>
24
25 #include "evaluate.h"
26 #include "misc.h"
27 #include "position.h"
28 #include "search.h"
29 #include "thread.h"
30 #include "ucioption.h"
31
32 using namespace std;
33
34 namespace {
35
36   // FEN string of the initial position, normal chess
37   const char* StartFEN = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
38
39   // Keep track of position keys along the setup moves (from start position to the
40   // position just before to start searching). This is needed by draw detection
41   // where, due to 50 moves rule, we need to check at most 100 plies back.
42   StateInfo StateRingBuf[102], *SetupState = StateRingBuf;
43
44   void set_option(istringstream& up);
45   void set_position(Position& pos, istringstream& up);
46   void go(Position& pos, istringstream& up);
47   void perft(Position& pos, istringstream& up);
48 }
49
50
51 /// Wait for a command from the user, parse this text string as an UCI command,
52 /// and call the appropriate functions. Also intercepts EOF from stdin to ensure
53 /// that we exit gracefully if the GUI dies unexpectedly. In addition to the UCI
54 /// commands, the function also supports a few debug commands.
55
56 void uci_loop() {
57
58   Position pos(StartFEN, false, 0); // The root position
59   string cmd, token;
60
61   while (token != "quit")
62   {
63       if (!getline(cin, cmd)) // Block here waiting for input
64           cmd = "quit";
65
66       istringstream is(cmd);
67
68       is >> skipws >> token;
69
70       if (token == "quit" || token == "stop")
71       {
72           Search::Signals.stop = true;
73           Threads[0].wake_up(); // In case is waiting for stop or ponderhit
74       }
75
76       else if (token == "ponderhit")
77       {
78           // The opponent has played the expected move. GUI sends "ponderhit" if
79           // we were told to ponder on the same move the opponent has played. We
80           // should continue searching but switching from pondering to normal search.
81           Search::Limits.ponder = false;
82
83           if (Search::Signals.stopOnPonderhit)
84               Search::Signals.stop = true;
85
86           Threads[0].wake_up(); // In case is waiting for stop or ponderhit
87       }
88
89       else if (token == "go")
90           go(pos, is);
91
92       else if (token == "ucinewgame")
93           pos.from_fen(StartFEN, false);
94
95       else if (token == "isready")
96           cout << "readyok" << endl;
97
98       else if (token == "position")
99           set_position(pos, is);
100
101       else if (token == "setoption")
102           set_option(is);
103
104       else if (token == "perft")
105           perft(pos, is);
106
107       else if (token == "d")
108           pos.print();
109
110       else if (token == "flip")
111           pos.flip_me();
112
113       else if (token == "eval")
114       {
115           read_evaluation_uci_options(pos.side_to_move());
116           cout << trace_evaluate(pos) << endl;
117       }
118
119       else if (token == "key")
120           cout << "key: " << hex     << pos.get_key()
121                << "\nmaterial key: " << pos.get_material_key()
122                << "\npawn key: "     << pos.get_pawn_key() << endl;
123
124       else if (token == "uci")
125           cout << "id name "     << engine_name()
126                << "\nid author " << engine_authors()
127                << "\n"           << Options.print_all()
128                << "\nuciok"      << endl;
129       else
130           cout << "Unknown command: " << cmd << endl;
131   }
132 }
133
134
135 namespace {
136
137   // set_position() is called when engine receives the "position" UCI
138   // command. The function sets up the position described in the given
139   // fen string ("fen") or the starting position ("startpos") and then
140   // makes the moves given in the following move list ("moves").
141
142   void set_position(Position& pos, istringstream& is) {
143
144     Move m;
145     string token, fen;
146
147     is >> token;
148
149     if (token == "startpos")
150     {
151         fen = StartFEN;
152         is >> token; // Consume "moves" token if any
153     }
154     else if (token == "fen")
155         while (is >> token && token != "moves")
156             fen += token + " ";
157     else
158         return;
159
160     pos.from_fen(fen, Options["UCI_Chess960"].value<bool>());
161
162     // Parse move list (if any)
163     while (is >> token && (m = move_from_uci(pos, token)) != MOVE_NONE)
164     {
165         pos.do_move(m, *SetupState);
166
167         // Increment pointer to StateRingBuf circular buffer
168         if (++SetupState - StateRingBuf >= 102)
169             SetupState = StateRingBuf;
170     }
171   }
172
173
174   // set_option() is called when engine receives the "setoption" UCI command. The
175   // function updates the UCI option ("name") to the given value ("value").
176
177   void set_option(istringstream& is) {
178
179     string token, name, value;
180
181     is >> token; // Consume "name" token
182
183     // Read option name (can contain spaces)
184     while (is >> token && token != "value")
185         name += string(" ", !name.empty()) + token;
186
187     // Read option value (can contain spaces)
188     while (is >> token)
189         value += string(" ", !value.empty()) + token;
190
191     if (Options.find(name) != Options.end())
192         Options[name].set_value(value.empty() ? "true" : value); // UCI buttons don't have "value"
193     else
194         cout << "No such option: " << name << endl;
195   }
196
197
198   // go() is called when engine receives the "go" UCI command. The function sets
199   // the thinking time and other parameters from the input string, and then starts
200   // the main searching thread.
201
202   void go(Position& pos, istringstream& is) {
203
204     string token;
205     Search::LimitsType limits;
206     std::vector<Move> searchMoves;
207     int time[] = { 0, 0 }, inc[] = { 0, 0 };
208
209     while (is >> token)
210     {
211         if (token == "infinite")
212             limits.infinite = true;
213         else if (token == "ponder")
214             limits.ponder = true;
215         else if (token == "wtime")
216             is >> time[WHITE];
217         else if (token == "btime")
218             is >> time[BLACK];
219         else if (token == "winc")
220             is >> inc[WHITE];
221         else if (token == "binc")
222             is >> inc[BLACK];
223         else if (token == "movestogo")
224             is >> limits.movesToGo;
225         else if (token == "depth")
226             is >> limits.maxDepth;
227         else if (token == "nodes")
228             is >> limits.maxNodes;
229         else if (token == "movetime")
230             is >> limits.maxTime;
231         else if (token == "searchmoves")
232             while (is >> token)
233                 searchMoves.push_back(move_from_uci(pos, token));
234     }
235
236     limits.time = time[pos.side_to_move()];
237     limits.increment = inc[pos.side_to_move()];
238
239     Threads.start_thinking(pos, limits, searchMoves, true);
240   }
241
242
243   // perft() is called when engine receives the "perft" command. The function
244   // calls perft() with the required search depth then prints counted leaf nodes
245   // and elapsed time.
246
247   void perft(Position& pos, istringstream& is) {
248
249     int depth, time;
250
251     if (!(is >> depth))
252         return;
253
254     time = get_system_time();
255
256     int64_t n = Search::perft(pos, depth * ONE_PLY);
257
258     time = get_system_time() - time;
259
260     std::cout << "\nNodes " << n
261               << "\nTime (ms) " << time
262               << "\nNodes/second " << int(n / (time / 1000.0)) << std::endl;
263   }
264 }