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