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