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