]> git.sesse.net Git - stockfish/blob - src/uci.cpp
Fix material key for King
[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-2014 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 <iomanip>
21 #include <iostream>
22 #include <sstream>
23 #include <string>
24
25 #include "evaluate.h"
26 #include "notation.h"
27 #include "position.h"
28 #include "search.h"
29 #include "thread.h"
30 #include "tt.h"
31 #include "ucioption.h"
32
33 using namespace std;
34
35 extern void benchmark(const Position& pos, istream& is);
36
37 namespace {
38
39   // FEN string of the initial position, normal chess
40   const char* StartFEN = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
41
42   // Keep a track of the position keys along the setup moves (from the start position
43   // to the position just before the search starts). This is needed by the repetition
44   // draw detection code.
45   Search::StateStackPtr SetupStates;
46
47   void setoption(istringstream& up);
48   void position(Position& pos, istringstream& up);
49   void go(const 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 call the appropriate functions. Also intercepts EOF from stdin to ensure
55 /// that we exit gracefully if the GUI dies unexpectedly. In addition to the UCI
56 /// commands, the function also supports a few debug commands.
57
58 void UCI::loop(const string& args) {
59
60   Position pos(StartFEN, false, Threads.main()); // The root position
61   string token, cmd = args;
62
63   do {
64       if (args.empty() && !getline(cin, cmd)) // Block here waiting for input
65           cmd = "quit";
66
67       istringstream is(cmd);
68
69       is >> skipws >> token;
70
71       if (token == "quit" || token == "stop" || token == "ponderhit")
72       {
73           // The GUI sends 'ponderhit' to tell us to ponder on the same move the
74           // opponent has played. In case Signals.stopOnPonderhit is set we are
75           // waiting for 'ponderhit' to stop the search (for instance because we
76           // already ran out of time), otherwise we should continue searching but
77           // switch from pondering to normal search.
78           if (token != "ponderhit" || Search::Signals.stopOnPonderhit)
79           {
80               Search::Signals.stop = true;
81               Threads.main()->notify_one(); // Could be sleeping
82           }
83           else
84               Search::Limits.ponder = false;
85       }
86       else if (token == "perft" && (is >> token)) // Read perft depth
87       {
88           stringstream ss;
89
90           ss << Options["Hash"]    << " "
91              << Options["Threads"] << " " << token << " current perft";
92
93           benchmark(pos, ss);
94       }
95       else if (token == "key")
96           sync_cout << hex << uppercase << setfill('0')
97                     << "position key: "   << setw(16) << pos.key()
98                     << "\nmaterial key: " << setw(16) << pos.material_key()
99                     << "\npawn key:     " << setw(16) << pos.pawn_key()
100                     << dec << sync_endl;
101
102       else if (token == "uci")
103           sync_cout << "id name " << engine_info(true)
104                     << "\n"       << Options
105                     << "\nuciok"  << sync_endl;
106
107       else if (token == "eval")
108       {
109           Search::RootColor = pos.side_to_move(); // Ensure it is set
110           sync_cout << Eval::trace(pos) << sync_endl;
111       }
112       else if (token == "ucinewgame") TT.clear();
113       else if (token == "go")         go(pos, is);
114       else if (token == "position")   position(pos, is);
115       else if (token == "setoption")  setoption(is);
116       else if (token == "flip")       pos.flip();
117       else if (token == "bench")      benchmark(pos, is);
118       else if (token == "d")          sync_cout << pos.pretty() << sync_endl;
119       else if (token == "isready")    sync_cout << "readyok" << sync_endl;
120       else
121           sync_cout << "Unknown command: " << cmd << sync_endl;
122
123   } while (token != "quit" && args.empty()); // Args have one-shot behaviour
124
125   Threads.wait_for_think_finished(); // Cannot quit whilst the search is running
126 }
127
128
129 namespace {
130
131   // position() is called when engine receives the "position" UCI command.
132   // The function sets up the position described in the given FEN string ("fen")
133   // or the starting position ("startpos") and then makes the moves given in the
134   // following move list ("moves").
135
136   void position(Position& pos, istringstream& is) {
137
138     Move m;
139     string token, fen;
140
141     is >> token;
142
143     if (token == "startpos")
144     {
145         fen = StartFEN;
146         is >> token; // Consume "moves" token if any
147     }
148     else if (token == "fen")
149         while (is >> token && token != "moves")
150             fen += token + " ";
151     else
152         return;
153
154     pos.set(fen, Options["UCI_Chess960"], Threads.main());
155     SetupStates = Search::StateStackPtr(new std::stack<StateInfo>());
156
157     // Parse move list (if any)
158     while (is >> token && (m = move_from_uci(pos, token)) != MOVE_NONE)
159     {
160         SetupStates->push(StateInfo());
161         pos.do_move(m, SetupStates->top());
162     }
163   }
164
165
166   // setoption() 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 setoption(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         sync_cout << "No such option: " << name << sync_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 starts
192   // the search.
193
194   void go(const Position& pos, istringstream& is) {
195
196     Search::LimitsType limits;
197     string token;
198
199     while (is >> token)
200     {
201         if (token == "searchmoves")
202             while (is >> token)
203                 limits.searchmoves.push_back(move_from_uci(pos, token));
204
205         else if (token == "wtime")     is >> limits.time[WHITE];
206         else if (token == "btime")     is >> limits.time[BLACK];
207         else if (token == "winc")      is >> limits.inc[WHITE];
208         else if (token == "binc")      is >> limits.inc[BLACK];
209         else if (token == "movestogo") is >> limits.movestogo;
210         else if (token == "depth")     is >> limits.depth;
211         else if (token == "nodes")     is >> limits.nodes;
212         else if (token == "movetime")  is >> limits.movetime;
213         else if (token == "mate")      is >> limits.mate;
214         else if (token == "infinite")  limits.infinite = true;
215         else if (token == "ponder")    limits.ponder = true;
216     }
217
218     Threads.start_thinking(pos, limits, SetupStates);
219   }
220 }