]> git.sesse.net Git - stockfish/blob - src/uci.cpp
ddaad41725aae83c8161549199649990ca719c68
[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 "notation.h"
26 #include "position.h"
27 #include "search.h"
28 #include "thread.h"
29 #include "tt.h"
30 #include "ucioption.h"
31
32 using namespace std;
33
34 extern void benchmark(const Position& pos, istream& is);
35
36 namespace {
37
38   // FEN string of the initial position, normal chess
39   const char* StartFEN = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
40
41   // Keep track of position keys along the setup moves (from start position to the
42   // position just before to start searching). Needed by repetition draw detection.
43   Search::StateStackPtr SetupStates;
44
45   void set_option(istringstream& up);
46   void set_position(Position& pos, istringstream& up);
47   void go(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(const string& args) {
57
58   Position pos(StartFEN, false, Threads.main_thread()); // The root position
59   string token, cmd = args;
60
61   do {
62       if (args.empty() && !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       {
71           Search::Signals.stop = true;
72           Threads.main_thread()->wake_up(); // Could be sleeping
73       }
74       else if (token == "ponderhit")
75       {
76           // GUI sends "ponderhit" if we were told to ponder on the same move the
77           // opponent has played. In case Signals.stopOnPonderhit is set we are
78           // waiting for "ponderhit" to stop the search (for instance because we
79           // already ran out of time), otherwise we should continue searching but
80           // switching from pondering to normal search.
81           if (Search::Signals.stopOnPonderhit)
82           {
83               Search::Signals.stop = true;
84               Threads.main_thread()->wake_up(); // Could be sleeping
85           }
86           else
87               Search::Limits.ponder = false;
88       }
89       else if (token == "perft" && (is >> token)) // Read perft depth
90       {
91           stringstream ss;
92
93           ss << Options["Hash"]    << " "
94              << Options["Threads"] << " " << token << " current perft";
95
96           benchmark(pos, ss);
97       }
98       else if (token == "key") sync_cout <<   "position key: " << hex << pos.key()
99                                          << "\nmaterial key: " << pos.material_key()
100                                          << "\npawn key:     " << pos.pawn_key()
101                                          << sync_endl;
102
103       else if (token == "uci") sync_cout << "id name " << engine_info(true)
104                                          << "\n"       << Options
105                                          << "\nuciok"  << sync_endl;
106
107       else if (token == "ucinewgame") TT.clear();
108       else if (token == "go")         go(pos, is);
109       else if (token == "position")   set_position(pos, is);
110       else if (token == "setoption")  set_option(is);
111       else if (token == "flip")       pos.flip();
112       else if (token == "bench")      benchmark(pos, is);
113       else if (token == "d")          sync_cout << pos.pretty() << sync_endl;
114       else if (token == "isready")    sync_cout << "readyok" << sync_endl;
115       else if (token == "eval")       sync_cout << Eval::trace(pos) << sync_endl;
116       else
117           sync_cout << "Unknown command: " << cmd << sync_endl;
118
119   } while (token != "quit" && args.empty()); // Args have one-shot behaviour
120
121   Threads.wait_for_search_finished(); // Cannot quit while search is running
122 }
123
124
125 namespace {
126
127   // set_position() is called when engine receives the "position" UCI command.
128   // The function sets up the position described in the given fen string ("fen")
129   // or the starting position ("startpos") and then makes the moves given in the
130   // following move list ("moves").
131
132   void set_position(Position& pos, istringstream& is) {
133
134     Move m;
135     string token, fen;
136
137     is >> token;
138
139     if (token == "startpos")
140     {
141         fen = StartFEN;
142         is >> token; // Consume "moves" token if any
143     }
144     else if (token == "fen")
145         while (is >> token && token != "moves")
146             fen += token + " ";
147     else
148         return;
149
150     pos.set(fen, Options["UCI_Chess960"], Threads.main_thread());
151     SetupStates = Search::StateStackPtr(new std::stack<StateInfo>());
152
153     // Parse move list (if any)
154     while (is >> token && (m = move_from_uci(pos, token)) != MOVE_NONE)
155     {
156         SetupStates->push(StateInfo());
157         pos.do_move(m, SetupStates->top());
158     }
159   }
160
161
162   // set_option() is called when engine receives the "setoption" UCI command. The
163   // function updates the UCI option ("name") to the given value ("value").
164
165   void set_option(istringstream& is) {
166
167     string token, name, value;
168
169     is >> token; // Consume "name" token
170
171     // Read option name (can contain spaces)
172     while (is >> token && token != "value")
173         name += string(" ", !name.empty()) + token;
174
175     // Read option value (can contain spaces)
176     while (is >> token)
177         value += string(" ", !value.empty()) + token;
178
179     if (Options.count(name))
180         Options[name] = value;
181     else
182         sync_cout << "No such option: " << name << sync_endl;
183   }
184
185
186   // go() is called when engine receives the "go" UCI command. The function sets
187   // the thinking time and other parameters from the input string, and starts
188   // the search.
189
190   void go(Position& pos, istringstream& is) {
191
192     Search::LimitsType limits;
193     vector<Move> searchMoves;
194     string token;
195
196     while (is >> token)
197     {
198         if (token == "searchmoves")
199             while (is >> token)
200                 searchMoves.push_back(move_from_uci(pos, token));
201
202         else if (token == "wtime")     is >> limits.time[WHITE];
203         else if (token == "btime")     is >> limits.time[BLACK];
204         else if (token == "winc")      is >> limits.inc[WHITE];
205         else if (token == "binc")      is >> limits.inc[BLACK];
206         else if (token == "movestogo") is >> limits.movestogo;
207         else if (token == "depth")     is >> limits.depth;
208         else if (token == "nodes")     is >> limits.nodes;
209         else if (token == "movetime")  is >> limits.movetime;
210         else if (token == "mate")      is >> limits.mate;
211         else if (token == "infinite")  limits.infinite = true;
212         else if (token == "ponder")    limits.ponder = true;
213     }
214
215     Threads.start_searching(pos, limits, searchMoves, SetupStates);
216   }
217 }