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