]> git.sesse.net Git - stockfish/blob - src/uci.cpp
Don't push on the stack 200KB in one go
[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 cmd, token;
60
61   while (token != "quit")
62   {
63       if (!args.empty())
64           cmd = args;
65
66       else if (!getline(cin, cmd)) // Block here waiting for input
67           cmd = "quit";
68
69       istringstream is(cmd);
70
71       is >> skipws >> token;
72
73       if (token == "quit" || token == "stop")
74       {
75           Search::Signals.stop = true;
76           Threads.wait_for_search_finished(); // Cannot quit while threads are running
77       }
78
79       else if (token == "ponderhit")
80       {
81           // The opponent has played the expected move. GUI sends "ponderhit" if
82           // we were told to ponder on the same move the opponent has played. We
83           // should continue searching but switching from pondering to normal search.
84           Search::Limits.ponder = false;
85
86           if (Search::Signals.stopOnPonderhit)
87           {
88               Search::Signals.stop = true;
89               Threads.main_thread()->wake_up(); // Could be sleeping
90           }
91       }
92
93       else if (token == "go")
94           go(pos, is);
95
96       else if (token == "ucinewgame")
97           TT.clear();
98
99       else if (token == "isready")
100           sync_cout << "readyok" << sync_endl;
101
102       else if (token == "position")
103           set_position(pos, is);
104
105       else if (token == "setoption")
106           set_option(is);
107
108       else if (token == "d")
109           pos.print();
110
111       else if (token == "flip")
112           pos.flip();
113
114       else if (token == "eval")
115           sync_cout << Eval::trace(pos) << sync_endl;
116
117       else if (token == "bench")
118           benchmark(pos, is);
119
120       else if (token == "key")
121           sync_cout << "key: " << hex     << pos.key()
122                     << "\nmaterial key: " << pos.material_key()
123                     << "\npawn key: "     << pos.pawn_key() << sync_endl;
124
125       else if (token == "uci")
126           sync_cout << "id name " << engine_info(true)
127                     << "\n"       << Options
128                     << "\nuciok"  << sync_endl;
129
130       else if (token == "perft" && (is >> token)) // Read depth
131       {
132           stringstream ss;
133
134           ss << Options["Hash"]    << " "
135              << Options["Threads"] << " " << token << " current perft";
136
137           benchmark(pos, ss);
138       }
139
140       else
141           sync_cout << "Unknown command: " << cmd << sync_endl;
142
143       if (!args.empty()) // Command line arguments have one-shot behaviour
144       {
145           Threads.wait_for_search_finished();
146           break;
147       }
148   }
149 }
150
151
152 namespace {
153
154   // set_position() is called when engine receives the "position" UCI command.
155   // The function sets up the position described in the given fen string ("fen")
156   // or the starting position ("startpos") and then makes the moves given in the
157   // following move list ("moves").
158
159   void set_position(Position& pos, istringstream& is) {
160
161     Move m;
162     string token, fen;
163
164     is >> token;
165
166     if (token == "startpos")
167     {
168         fen = StartFEN;
169         is >> token; // Consume "moves" token if any
170     }
171     else if (token == "fen")
172         while (is >> token && token != "moves")
173             fen += token + " ";
174     else
175         return;
176
177     pos.from_fen(fen, Options["UCI_Chess960"], Threads.main_thread());
178     SetupStates = Search::StateStackPtr(new std::stack<StateInfo>());
179
180     // Parse move list (if any)
181     while (is >> token && (m = move_from_uci(pos, token)) != MOVE_NONE)
182     {
183         SetupStates->push(StateInfo());
184         pos.do_move(m, SetupStates->top());
185     }
186   }
187
188
189   // set_option() is called when engine receives the "setoption" UCI command. The
190   // function updates the UCI option ("name") to the given value ("value").
191
192   void set_option(istringstream& is) {
193
194     string token, name, value;
195
196     is >> token; // Consume "name" token
197
198     // Read option name (can contain spaces)
199     while (is >> token && token != "value")
200         name += string(" ", !name.empty()) + token;
201
202     // Read option value (can contain spaces)
203     while (is >> token)
204         value += string(" ", !value.empty()) + token;
205
206     if (Options.count(name))
207         Options[name] = value;
208     else
209         sync_cout << "No such option: " << name << sync_endl;
210   }
211
212
213   // go() is called when engine receives the "go" UCI command. The function sets
214   // the thinking time and other parameters from the input string, and then starts
215   // the search.
216
217   void go(Position& pos, istringstream& is) {
218
219     Search::LimitsType limits;
220     vector<Move> searchMoves;
221     string token;
222
223     while (is >> token)
224     {
225         if (token == "wtime")
226             is >> limits.time[WHITE];
227         else if (token == "btime")
228             is >> limits.time[BLACK];
229         else if (token == "winc")
230             is >> limits.inc[WHITE];
231         else if (token == "binc")
232             is >> limits.inc[BLACK];
233         else if (token == "movestogo")
234             is >> limits.movestogo;
235         else if (token == "depth")
236             is >> limits.depth;
237         else if (token == "nodes")
238             is >> limits.nodes;
239         else if (token == "movetime")
240             is >> limits.movetime;
241         else if (token == "infinite")
242             limits.infinite = true;
243         else if (token == "ponder")
244             limits.ponder = true;
245         else if (token == "searchmoves")
246             while (is >> token)
247                 searchMoves.push_back(move_from_uci(pos, token));
248     }
249
250     Threads.start_searching(pos, limits, searchMoves, SetupStates);
251   }
252 }