]> git.sesse.net Git - stockfish/blob - src/uci.cpp
Use UCI names in Search::LimitsType
[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       {
71           Search::Signals.stop = true;
72
73           if (token == "quit") // Cannot quit while threads are still running
74               Threads.wait_for_search_finished();
75       }
76
77       else if (token == "ponderhit")
78       {
79           // The opponent has played the expected move. GUI sends "ponderhit" if
80           // we were told to ponder on the same move the opponent has played. We
81           // should continue searching but switching from pondering to normal search.
82           Search::Limits.ponder = false;
83
84           if (Search::Signals.stopOnPonderhit)
85               Search::Signals.stop = true;
86       }
87
88       else if (token == "go")
89           go(pos, is);
90
91       else if (token == "isready")
92           cout << "readyok" << endl;
93
94       else if (token == "position")
95           set_position(pos, is);
96
97       else if (token == "setoption")
98           set_option(is);
99
100       else if (token == "perft")
101           perft(pos, is);
102
103       else if (token == "d")
104           pos.print();
105
106       else if (token == "flip")
107           pos.flip_me();
108
109       else if (token == "eval")
110           cout << Eval::trace(pos) << endl;
111
112       else if (token == "key")
113           cout << "key: " << hex     << pos.key()
114                << "\nmaterial key: " << pos.material_key()
115                << "\npawn key: "     << pos.pawn_key() << endl;
116
117       else if (token == "uci")
118           cout << "id name "     << engine_info(true)
119                << "\n"           << Options
120                << "\nuciok"      << endl;
121       else
122           cout << "Unknown command: " << cmd << endl;
123   }
124 }
125
126
127 namespace {
128
129   // set_position() is called when engine receives the "position" UCI
130   // command. The function sets up the position described in the given
131   // fen string ("fen") or the starting position ("startpos") and then
132   // makes the moves given in the following move list ("moves").
133
134   void set_position(Position& pos, istringstream& is) {
135
136     Move m;
137     string token, fen;
138
139     is >> token;
140
141     if (token == "startpos")
142     {
143         fen = StartFEN;
144         is >> token; // Consume "moves" token if any
145     }
146     else if (token == "fen")
147         while (is >> token && token != "moves")
148             fen += token + " ";
149     else
150         return;
151
152     pos.from_fen(fen, Options["UCI_Chess960"]);
153
154     // Parse move list (if any)
155     while (is >> token && (m = move_from_uci(pos, token)) != MOVE_NONE)
156     {
157         pos.do_move(m, *SetupState);
158
159         // Increment pointer to StateRingBuf circular buffer
160         if (++SetupState - StateRingBuf >= 102)
161             SetupState = StateRingBuf;
162     }
163   }
164
165
166   // set_option() is called when engine receives the "setoption" UCI command. The
167   // function updates the UCI option ("name") to the given value ("value").
168
169   void set_option(istringstream& is) {
170
171     string token, name, value;
172
173     is >> token; // Consume "name" token
174
175     // Read option name (can contain spaces)
176     while (is >> token && token != "value")
177         name += string(" ", !name.empty()) + token;
178
179     // Read option value (can contain spaces)
180     while (is >> token)
181         value += string(" ", !value.empty()) + token;
182
183     if (Options.count(name))
184         Options[name] = value;
185     else
186         cout << "No such option: " << name << endl;
187   }
188
189
190   // go() is called when engine receives the "go" UCI command. The function sets
191   // the thinking time and other parameters from the input string, and then starts
192   // the search.
193
194   void go(Position& pos, istringstream& is) {
195
196     Search::LimitsType limits;
197     std::set<Move> searchMoves;
198     string token;
199
200     while (is >> token)
201     {
202         if (token == "wtime")
203             is >> limits.times[WHITE];
204         else if (token == "btime")
205             is >> limits.times[BLACK];
206         else if (token == "winc")
207             is >> limits.incs[WHITE];
208         else if (token == "binc")
209             is >> limits.incs[BLACK];
210         else if (token == "movestogo")
211             is >> limits.movestogo;
212         else if (token == "depth")
213             is >> limits.depth;
214         else if (token == "nodes")
215             is >> limits.nodes;
216         else if (token == "movetime")
217             is >> limits.movetime;
218         else if (token == "infinite")
219             limits.infinite = true;
220         else if (token == "ponder")
221             limits.ponder = true;
222         else if (token == "searchmoves")
223             while (is >> token)
224                 searchMoves.insert(move_from_uci(pos, token));
225     }
226
227     Threads.start_searching(pos, limits, searchMoves);
228   }
229
230
231   // perft() is called when engine receives the "perft" command. The function
232   // calls perft() with the required search depth then prints counted leaf nodes
233   // and elapsed time.
234
235   void perft(Position& pos, istringstream& is) {
236
237     int depth;
238
239     if (!(is >> depth))
240         return;
241
242     Time time = Time::current_time();
243
244     int64_t n = Search::perft(pos, depth * ONE_PLY);
245
246     int e = time.elapsed();
247
248     std::cout << "\nNodes " << n
249               << "\nTime (ms) " << e
250               << "\nNodes/second " << int(n / (e / 1000.0)) << std::endl;
251   }
252 }