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