2008-08-31 23:59:13 -06:00
|
|
|
/*
|
2008-10-19 10:56:28 -06:00
|
|
|
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
|
|
|
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
2016-01-02 02:43:25 -07:00
|
|
|
Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
|
2018-11-19 03:18:21 -07:00
|
|
|
Copyright (C) 2015-2019 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
|
2008-08-31 23:59:13 -06:00
|
|
|
|
2008-10-19 10:56:28 -06:00
|
|
|
Stockfish is free software: you can redistribute it and/or modify
|
2008-08-31 23:59:13 -06:00
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
2008-09-01 06:05:02 -06:00
|
|
|
|
2008-10-19 10:56:28 -06:00
|
|
|
Stockfish is distributed in the hope that it will be useful,
|
2008-08-31 23:59:13 -06:00
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
2008-09-01 06:05:02 -06:00
|
|
|
|
2008-08-31 23:59:13 -06:00
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2017-05-03 20:46:40 -06:00
|
|
|
#include <cassert>
|
2008-08-31 23:59:13 -06:00
|
|
|
#include <iostream>
|
2008-09-01 00:06:34 -06:00
|
|
|
#include <sstream>
|
2008-08-31 23:59:13 -06:00
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include "evaluate.h"
|
2014-10-26 00:50:09 -06:00
|
|
|
#include "movegen.h"
|
2008-08-31 23:59:13 -06:00
|
|
|
#include "position.h"
|
|
|
|
#include "search.h"
|
2011-11-05 00:53:19 -06:00
|
|
|
#include "thread.h"
|
Add support for playing in 'nodes as time' mode
When running more games in parallel, or simply when running a game
with a background process, due to how OS scheduling works, there is no
guarantee that the CPU resources allocated evenly between the two
players. This introduces noise in the result that leads to unreliable
result and in the worst cases can even invalidate the result. For
instance in SF test framework we avoid running from clouds virtual
machines because are a known source of very unstable CPU speed.
To overcome this issue, without requiring changes to the GUI, the idea
is to use searched nodes instead of time, and to convert time to
available nodes upfront, at the beginning of the game.
When nodestime UCI option is set at a given nodes per milliseconds
(npmsec), at the beginning of the game (and only once), the engine
reads the available time to think, sent by the GUI with 'go wtime x'
UCI command. Then it translates time in available nodes (nodes =
npmsec * x), then feeds available nodes instead of time to the time
management logic and starts the search. During the search the engine
checks the searched nodes against the available ones in such a way
that all the time management logic still fully applies, and the game
mimics a real one played on real time. When the search finishes,
before returning best move, the total available nodes are updated,
subtracting the real searched nodes. After the first move, the time
information sent by the GUI is ignored, and the engine fully relies on
the updated total available nodes to feed time management.
To avoid time losses, the speed of the engine (npms) must be set to a
value lower than real speed so that if the real TC is for instance 30
secs, and npms is half of the real speed, the game will last on
average 15 secs, so much less than the TC limit, providing for a
safety 'time buffer'.
There are 2 main limitations with this mode.
1. Engine speed should be the same for both players, and this limits
the approach to mainly parameter tuning patches.
2. Because npms is fixed while, in real engines, the speed increases
toward endgame, this introduces an artifact that is equivalent to an
altered time management. Namely it is like the time management gives
less available time than what should be in standard case.
May be the second limitation could be mitigated in a future with a
smarter 'dynamic npms' approach.
Tests shows that the standard deviation of the results with 'nodestime'
is lower than in standard TC, as is expected because now all the introduced
noise due the random speed variability of the engines during the game is
fully removed.
Original NIT idea by Michael Hoffman that shows how to play in NIT mode
without requiring changes to the GUI. This implementation goes a bit
further, the key difference is that we read TC from GUI only once upfront
instead of re-reading after every move as in Michael's implementation.
No functional change.
2015-03-22 14:15:44 -06:00
|
|
|
#include "timeman.h"
|
2018-03-20 18:26:12 -06:00
|
|
|
#include "tt.h"
|
2014-10-26 00:09:19 -06:00
|
|
|
#include "uci.h"
|
2016-07-16 00:10:45 -06:00
|
|
|
#include "syzygy/tbprobe.h"
|
2008-08-31 23:59:13 -06:00
|
|
|
|
2009-05-20 04:40:07 -06:00
|
|
|
using namespace std;
|
2008-08-31 23:59:13 -06:00
|
|
|
|
2017-08-15 03:51:14 -06:00
|
|
|
extern vector<string> setup_bench(const Position&, istream&);
|
2012-04-01 02:25:07 -06:00
|
|
|
|
2008-08-31 23:59:13 -06:00
|
|
|
namespace {
|
|
|
|
|
2011-12-03 02:41:50 -07:00
|
|
|
// FEN string of the initial position, normal chess
|
|
|
|
const char* StartFEN = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
|
2010-08-19 10:55:32 -06:00
|
|
|
|
2008-08-31 23:59:13 -06:00
|
|
|
|
2013-06-22 04:45:14 -06:00
|
|
|
// position() is called when engine receives the "position" UCI command.
|
2013-12-04 23:18:12 -07:00
|
|
|
// The function sets up the position described in the given FEN string ("fen")
|
2012-08-26 10:07:54 -06:00
|
|
|
// or the starting position ("startpos") and then makes the moves given in the
|
|
|
|
// following move list ("moves").
|
2008-08-31 23:59:13 -06:00
|
|
|
|
2017-08-13 00:38:39 -06:00
|
|
|
void position(Position& pos, istringstream& is, StateListPtr& states) {
|
2008-09-01 00:06:34 -06:00
|
|
|
|
2011-07-17 03:22:08 -06:00
|
|
|
Move m;
|
2011-04-26 03:19:57 -06:00
|
|
|
string token, fen;
|
2008-08-31 23:59:13 -06:00
|
|
|
|
2011-07-24 00:22:37 -06:00
|
|
|
is >> token;
|
2008-09-01 00:16:17 -06:00
|
|
|
|
|
|
|
if (token == "startpos")
|
2010-12-13 03:17:06 -07:00
|
|
|
{
|
2011-12-03 02:41:50 -07:00
|
|
|
fen = StartFEN;
|
2011-07-24 00:22:37 -06:00
|
|
|
is >> token; // Consume "moves" token if any
|
2010-12-13 03:17:06 -07:00
|
|
|
}
|
2011-01-09 06:45:49 -07:00
|
|
|
else if (token == "fen")
|
2011-07-24 00:22:37 -06:00
|
|
|
while (is >> token && token != "moves")
|
2011-01-09 06:45:49 -07:00
|
|
|
fen += token + " ";
|
2011-07-24 00:22:37 -06:00
|
|
|
else
|
|
|
|
return;
|
2011-01-09 03:22:59 -07:00
|
|
|
|
2017-08-13 00:38:39 -06:00
|
|
|
states = StateListPtr(new std::deque<StateInfo>(1)); // Drop old and create a new one
|
|
|
|
pos.set(fen, Options["UCI_Chess960"], &states->back(), Threads.main());
|
2008-08-31 23:59:13 -06:00
|
|
|
|
2011-07-20 02:01:12 -06:00
|
|
|
// Parse move list (if any)
|
2014-10-26 00:41:25 -06:00
|
|
|
while (is >> token && (m = UCI::to_move(pos, token)) != MOVE_NONE)
|
2011-07-18 02:20:37 -06:00
|
|
|
{
|
2017-08-13 00:38:39 -06:00
|
|
|
states->emplace_back();
|
|
|
|
pos.do_move(m, states->back());
|
2011-07-18 02:20:37 -06:00
|
|
|
}
|
2008-08-31 23:59:13 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-22 04:45:14 -06:00
|
|
|
// setoption() is called when engine receives the "setoption" UCI command. The
|
2011-12-03 02:41:50 -07:00
|
|
|
// function updates the UCI option ("name") to the given value ("value").
|
2008-08-31 23:59:13 -06:00
|
|
|
|
2013-06-22 04:45:14 -06:00
|
|
|
void setoption(istringstream& is) {
|
2008-09-01 00:06:34 -06:00
|
|
|
|
2011-07-17 05:26:50 -06:00
|
|
|
string token, name, value;
|
2008-09-01 00:16:17 -06:00
|
|
|
|
2011-07-24 00:22:37 -06:00
|
|
|
is >> token; // Consume "name" token
|
2010-01-07 03:59:32 -07:00
|
|
|
|
2011-07-17 05:26:50 -06:00
|
|
|
// Read option name (can contain spaces)
|
2011-07-24 00:22:37 -06:00
|
|
|
while (is >> token && token != "value")
|
2018-02-09 17:44:05 -07:00
|
|
|
name += (name.empty() ? "" : " ") + token;
|
2009-07-10 10:34:56 -06:00
|
|
|
|
2011-07-17 05:26:50 -06:00
|
|
|
// Read option value (can contain spaces)
|
2011-07-24 00:22:37 -06:00
|
|
|
while (is >> token)
|
2018-02-09 17:44:05 -07:00
|
|
|
value += (value.empty() ? "" : " ") + token;
|
2010-11-03 02:32:14 -06:00
|
|
|
|
2012-03-17 14:18:02 -06:00
|
|
|
if (Options.count(name))
|
2011-12-28 10:27:18 -07:00
|
|
|
Options[name] = value;
|
2012-03-17 14:18:02 -06:00
|
|
|
else
|
2012-08-29 05:28:59 -06:00
|
|
|
sync_cout << "No such option: " << name << sync_endl;
|
2008-08-31 23:59:13 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-12-03 02:41:50 -07:00
|
|
|
// go() is called when engine receives the "go" UCI command. The function sets
|
2015-01-03 02:51:38 -07:00
|
|
|
// the thinking time and other parameters from the input string, then starts
|
2012-03-27 04:27:57 -06:00
|
|
|
// the search.
|
2008-08-31 23:59:13 -06:00
|
|
|
|
2017-08-13 00:38:39 -06:00
|
|
|
void go(Position& pos, istringstream& is, StateListPtr& states) {
|
2008-09-01 00:06:34 -06:00
|
|
|
|
2011-11-27 09:07:17 -07:00
|
|
|
Search::LimitsType limits;
|
2012-03-27 04:27:57 -06:00
|
|
|
string token;
|
2017-08-10 13:32:50 -06:00
|
|
|
bool ponderMode = false;
|
2008-08-31 23:59:13 -06:00
|
|
|
|
Lazy SMP
Start all threads searching on root position and
use only the shared TT table as synching scheme.
It seems this scheme scales better than YBWC for
high number of threads.
Verified for nor regression at STC 3 threads
LLR: -2.95 (-2.94,2.94) [-3.00,1.00]
Total: 40232 W: 6908 L: 7130 D: 26194
Verified for nor regression at LTC 3 threads
LLR: 2.95 (-2.94,2.94) [-3.00,1.00]
Total: 28186 W: 3908 L: 3798 D: 20480
Verified for nor regression at STC 7 threads
LLR: 2.95 (-2.94,2.94) [-3.00,1.00]
Total: 3607 W: 674 L: 526 D: 2407
Verified for nor regression at LTC 7 threads
LLR: 2.95 (-2.94,2.94) [-3.00,1.00]
Total: 4235 W: 671 L: 528 D: 3036
Tested with fixed games at LTC with 20 threads
ELO: 44.75 +-7.6 (95%) LOS: 100.0%
Total: 2069 W: 407 L: 142 D: 1520
Tested with fixed games at XLTC (120secs) with 20 threads
ELO: 28.01 +-6.7 (95%) LOS: 100.0%
Total: 2275 W: 349 L: 166 D: 1760
Original patch of mbootsector, with additional work
from Ivan Ivec (log formula), Joerg Oster (id loop
simplification) and Marco Costalba (assorted formatting
and rework).
Bench: 8116244
2015-10-06 00:15:17 -06:00
|
|
|
limits.startTime = now(); // As early as possible!
|
|
|
|
|
2011-07-24 00:22:37 -06:00
|
|
|
while (is >> token)
|
2012-10-28 04:09:47 -06:00
|
|
|
if (token == "searchmoves")
|
2011-07-24 00:22:37 -06:00
|
|
|
while (is >> token)
|
2014-10-26 00:41:25 -06:00
|
|
|
limits.searchmoves.push_back(UCI::to_move(pos, token));
|
2012-10-28 04:09:47 -06:00
|
|
|
|
|
|
|
else if (token == "wtime") is >> limits.time[WHITE];
|
|
|
|
else if (token == "btime") is >> limits.time[BLACK];
|
|
|
|
else if (token == "winc") is >> limits.inc[WHITE];
|
|
|
|
else if (token == "binc") is >> limits.inc[BLACK];
|
|
|
|
else if (token == "movestogo") is >> limits.movestogo;
|
|
|
|
else if (token == "depth") is >> limits.depth;
|
|
|
|
else if (token == "nodes") is >> limits.nodes;
|
|
|
|
else if (token == "movetime") is >> limits.movetime;
|
2012-12-30 03:40:20 -07:00
|
|
|
else if (token == "mate") is >> limits.mate;
|
2017-08-15 02:05:22 -06:00
|
|
|
else if (token == "perft") is >> limits.perft;
|
2015-09-27 02:53:39 -06:00
|
|
|
else if (token == "infinite") limits.infinite = 1;
|
2017-08-10 13:32:50 -06:00
|
|
|
else if (token == "ponder") ponderMode = true;
|
2011-12-03 02:41:50 -07:00
|
|
|
|
2017-08-13 00:38:39 -06:00
|
|
|
Threads.start_thinking(pos, states, limits, ponderMode);
|
2008-08-31 23:59:13 -06:00
|
|
|
}
|
2014-04-12 04:05:25 -06:00
|
|
|
|
2017-08-14 11:41:04 -06:00
|
|
|
|
|
|
|
// bench() is called when engine receives the "bench" command. Firstly
|
|
|
|
// a list of UCI commands is setup according to bench parameters, then
|
2017-08-15 02:05:22 -06:00
|
|
|
// it is run one by one printing a summary at the end.
|
2017-08-14 11:41:04 -06:00
|
|
|
|
|
|
|
void bench(Position& pos, istream& args, StateListPtr& states) {
|
|
|
|
|
|
|
|
string token;
|
|
|
|
uint64_t num, nodes = 0, cnt = 1;
|
|
|
|
|
|
|
|
vector<string> list = setup_bench(pos, args);
|
2017-08-15 02:05:22 -06:00
|
|
|
num = count_if(list.begin(), list.end(), [](string s) { return s.find("go ") == 0; });
|
2017-08-14 11:41:04 -06:00
|
|
|
|
|
|
|
TimePoint elapsed = now();
|
|
|
|
|
|
|
|
for (const auto& cmd : list)
|
|
|
|
{
|
|
|
|
istringstream is(cmd);
|
|
|
|
is >> skipws >> token;
|
|
|
|
|
|
|
|
if (token == "go")
|
|
|
|
{
|
|
|
|
cerr << "\nPosition: " << cnt++ << '/' << num << endl;
|
|
|
|
go(pos, is, states);
|
|
|
|
Threads.main()->wait_for_search_finished();
|
|
|
|
nodes += Threads.nodes_searched();
|
|
|
|
}
|
|
|
|
else if (token == "setoption") setoption(is);
|
|
|
|
else if (token == "position") position(pos, is, states);
|
2019-06-30 07:16:20 -06:00
|
|
|
else if (token == "ucinewgame") { Search::clear(); elapsed = now(); } // Search::clear() may take some while
|
2017-08-14 11:41:04 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
elapsed = now() - elapsed + 1; // Ensure positivity to avoid a 'divide by zero'
|
|
|
|
|
|
|
|
dbg_print(); // Just before exiting
|
|
|
|
|
|
|
|
cerr << "\n==========================="
|
|
|
|
<< "\nTotal time (ms) : " << elapsed
|
|
|
|
<< "\nNodes searched : " << nodes
|
|
|
|
<< "\nNodes/second : " << 1000 * nodes / elapsed << endl;
|
|
|
|
}
|
|
|
|
|
2014-04-12 04:05:25 -06:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
|
2015-01-03 02:51:38 -07:00
|
|
|
/// UCI::loop() waits for a command from stdin, parses it and calls the appropriate
|
|
|
|
/// function. Also intercepts EOF from stdin to ensure gracefully exiting if the
|
|
|
|
/// GUI dies unexpectedly. When called with some command line arguments, e.g. to
|
|
|
|
/// run 'bench', once the command is executed the function returns immediately.
|
|
|
|
/// In addition to the UCI ones, also some additional debug commands are supported.
|
2014-04-12 04:05:25 -06:00
|
|
|
|
2014-04-12 04:52:10 -06:00
|
|
|
void UCI::loop(int argc, char* argv[]) {
|
2014-04-12 04:05:25 -06:00
|
|
|
|
2016-04-11 08:45:36 -06:00
|
|
|
Position pos;
|
2014-04-12 04:52:10 -06:00
|
|
|
string token, cmd;
|
2017-08-13 00:38:39 -06:00
|
|
|
StateListPtr states(new std::deque<StateInfo>(1));
|
|
|
|
auto uiThread = std::make_shared<Thread>(0);
|
2014-04-12 04:52:10 -06:00
|
|
|
|
2017-08-13 00:38:39 -06:00
|
|
|
pos.set(StartFEN, false, &states->back(), uiThread.get());
|
2016-04-11 08:45:36 -06:00
|
|
|
|
2014-04-12 04:52:10 -06:00
|
|
|
for (int i = 1; i < argc; ++i)
|
|
|
|
cmd += std::string(argv[i]) + " ";
|
2014-04-12 04:05:25 -06:00
|
|
|
|
|
|
|
do {
|
2015-01-03 02:51:38 -07:00
|
|
|
if (argc == 1 && !getline(cin, cmd)) // Block here waiting for input or EOF
|
2014-04-12 04:05:25 -06:00
|
|
|
cmd = "quit";
|
|
|
|
|
|
|
|
istringstream is(cmd);
|
|
|
|
|
2017-08-13 00:38:39 -06:00
|
|
|
token.clear(); // Avoid a stale if getline() returns empty or blank line
|
2014-04-12 04:05:25 -06:00
|
|
|
is >> skipws >> token;
|
|
|
|
|
2015-01-03 02:51:38 -07:00
|
|
|
if ( token == "quit"
|
Simplify pondering time management (#1899)
stopOnPonderhit is used to stop search quickly on a ponderhit. It is set by mainThread as part of its time management. However, master employs it as a signal between mainThread and the UCI thread. This is not necessary, it is sufficient for the UCI thread to signal that pondering finished, and mainThread should do its usual time-keeping job, and in this case stop immediately.
This patch implements this, removing stopOnPonderHit as an atomic variable from the ThreadPool,
and moving it as a normal variable to mainThread, reducing its scope. In MainThread::check_time() the search is stopped immediately if ponder switches to false, and the variable stopOnPonderHit is set.
Furthermore, ponder has been moved to mainThread, as the variable is only used to exchange signals between the UCI thread and mainThread.
The version has been tested locally (as fishtest doesn't support ponder):
Score of ponderSimp vs master: 2616 - 2528 - 8630 [0.503] 13774
Elo difference: 2.22 +/- 3.54
which indicates no regression.
No functional change.
2019-01-20 11:14:24 -07:00
|
|
|
|| token == "stop")
|
2017-07-13 17:07:19 -06:00
|
|
|
Threads.stop = true;
|
2017-08-04 11:48:07 -06:00
|
|
|
|
Simplify pondering time management (#1899)
stopOnPonderhit is used to stop search quickly on a ponderhit. It is set by mainThread as part of its time management. However, master employs it as a signal between mainThread and the UCI thread. This is not necessary, it is sufficient for the UCI thread to signal that pondering finished, and mainThread should do its usual time-keeping job, and in this case stop immediately.
This patch implements this, removing stopOnPonderHit as an atomic variable from the ThreadPool,
and moving it as a normal variable to mainThread, reducing its scope. In MainThread::check_time() the search is stopped immediately if ponder switches to false, and the variable stopOnPonderHit is set.
Furthermore, ponder has been moved to mainThread, as the variable is only used to exchange signals between the UCI thread and mainThread.
The version has been tested locally (as fishtest doesn't support ponder):
Score of ponderSimp vs master: 2616 - 2528 - 8630 [0.503] 13774
Elo difference: 2.22 +/- 3.54
which indicates no regression.
No functional change.
2019-01-20 11:14:24 -07:00
|
|
|
// The GUI sends 'ponderhit' to tell us the user has played the expected move.
|
|
|
|
// So 'ponderhit' will be sent if we were told to ponder on the same move the
|
|
|
|
// user has played. We should continue searching but switch from pondering to
|
|
|
|
// normal search.
|
2015-01-03 02:51:38 -07:00
|
|
|
else if (token == "ponderhit")
|
Simplify pondering time management (#1899)
stopOnPonderhit is used to stop search quickly on a ponderhit. It is set by mainThread as part of its time management. However, master employs it as a signal between mainThread and the UCI thread. This is not necessary, it is sufficient for the UCI thread to signal that pondering finished, and mainThread should do its usual time-keeping job, and in this case stop immediately.
This patch implements this, removing stopOnPonderHit as an atomic variable from the ThreadPool,
and moving it as a normal variable to mainThread, reducing its scope. In MainThread::check_time() the search is stopped immediately if ponder switches to false, and the variable stopOnPonderHit is set.
Furthermore, ponder has been moved to mainThread, as the variable is only used to exchange signals between the UCI thread and mainThread.
The version has been tested locally (as fishtest doesn't support ponder):
Score of ponderSimp vs master: 2616 - 2528 - 8630 [0.503] 13774
Elo difference: 2.22 +/- 3.54
which indicates no regression.
No functional change.
2019-01-20 11:14:24 -07:00
|
|
|
Threads.main()->ponder = false; // Switch to normal search
|
2014-04-12 04:05:25 -06:00
|
|
|
|
|
|
|
else if (token == "uci")
|
|
|
|
sync_cout << "id name " << engine_info(true)
|
|
|
|
<< "\n" << Options
|
|
|
|
<< "\nuciok" << sync_endl;
|
|
|
|
|
2017-08-13 00:38:39 -06:00
|
|
|
else if (token == "setoption") setoption(is);
|
|
|
|
else if (token == "go") go(pos, is, states);
|
|
|
|
else if (token == "position") position(pos, is, states);
|
2017-08-06 05:43:02 -06:00
|
|
|
else if (token == "ucinewgame") Search::clear();
|
2015-01-03 02:51:38 -07:00
|
|
|
else if (token == "isready") sync_cout << "readyok" << sync_endl;
|
|
|
|
|
2017-08-13 00:38:39 -06:00
|
|
|
// Additional custom non-UCI commands, mainly for debugging
|
|
|
|
else if (token == "flip") pos.flip();
|
2017-08-14 11:41:04 -06:00
|
|
|
else if (token == "bench") bench(pos, is, states);
|
2017-08-13 00:38:39 -06:00
|
|
|
else if (token == "d") sync_cout << pos << sync_endl;
|
|
|
|
else if (token == "eval") sync_cout << Eval::trace(pos) << sync_endl;
|
2014-04-12 04:05:25 -06:00
|
|
|
else
|
|
|
|
sync_cout << "Unknown command: " << cmd << sync_endl;
|
|
|
|
|
2017-08-13 00:38:39 -06:00
|
|
|
} while (token != "quit" && argc == 1); // Command line args are one-shot
|
2008-08-31 23:59:13 -06:00
|
|
|
}
|
2014-10-26 00:50:09 -06:00
|
|
|
|
|
|
|
|
2015-01-03 02:51:38 -07:00
|
|
|
/// UCI::value() converts a Value to a string suitable for use with the UCI
|
|
|
|
/// protocol specification:
|
2014-10-26 00:50:09 -06:00
|
|
|
///
|
2015-01-03 02:51:38 -07:00
|
|
|
/// cp <x> The score from the engine's point of view in centipawns.
|
|
|
|
/// mate <y> Mate in y moves, not plies. If the engine is getting mated
|
|
|
|
/// use negative values for y.
|
2014-10-26 00:50:09 -06:00
|
|
|
|
2015-01-03 02:51:38 -07:00
|
|
|
string UCI::value(Value v) {
|
2014-10-26 00:50:09 -06:00
|
|
|
|
2017-05-03 20:46:40 -06:00
|
|
|
assert(-VALUE_INFINITE < v && v < VALUE_INFINITE);
|
|
|
|
|
2014-10-26 00:50:09 -06:00
|
|
|
stringstream ss;
|
|
|
|
|
2015-01-18 00:05:05 -07:00
|
|
|
if (abs(v) < VALUE_MATE - MAX_PLY)
|
2014-10-26 00:50:09 -06:00
|
|
|
ss << "cp " << v * 100 / PawnValueEg;
|
|
|
|
else
|
|
|
|
ss << "mate " << (v > 0 ? VALUE_MATE - v + 1 : -VALUE_MATE - v) / 2;
|
|
|
|
|
|
|
|
return ss.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-01-03 02:51:38 -07:00
|
|
|
/// UCI::square() converts a Square to a string in algebraic notation (g1, a7, etc.)
|
2014-10-26 00:50:09 -06:00
|
|
|
|
2014-12-14 01:31:13 -07:00
|
|
|
std::string UCI::square(Square s) {
|
2015-01-18 03:04:51 -07:00
|
|
|
return std::string{ char('a' + file_of(s)), char('1' + rank_of(s)) };
|
2014-10-26 00:50:09 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-01-03 02:51:38 -07:00
|
|
|
/// UCI::move() converts a Move to a string in coordinate notation (g1f3, a7a8q).
|
|
|
|
/// The only special case is castling, where we print in the e1g1 notation in
|
|
|
|
/// normal chess mode, and in e1h1 notation in chess960 mode. Internally all
|
|
|
|
/// castling moves are always encoded as 'king captures rook'.
|
2014-10-26 00:50:09 -06:00
|
|
|
|
2014-12-14 01:31:13 -07:00
|
|
|
string UCI::move(Move m, bool chess960) {
|
2014-10-26 00:50:09 -06:00
|
|
|
|
|
|
|
Square from = from_sq(m);
|
|
|
|
Square to = to_sq(m);
|
|
|
|
|
|
|
|
if (m == MOVE_NONE)
|
|
|
|
return "(none)";
|
|
|
|
|
|
|
|
if (m == MOVE_NULL)
|
|
|
|
return "0000";
|
|
|
|
|
|
|
|
if (type_of(m) == CASTLING && !chess960)
|
|
|
|
to = make_square(to > from ? FILE_G : FILE_C, rank_of(from));
|
|
|
|
|
2014-12-14 01:31:13 -07:00
|
|
|
string move = UCI::square(from) + UCI::square(to);
|
2014-10-26 00:50:09 -06:00
|
|
|
|
|
|
|
if (type_of(m) == PROMOTION)
|
|
|
|
move += " pnbrqk"[promotion_type(m)];
|
|
|
|
|
|
|
|
return move;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-01-03 02:51:38 -07:00
|
|
|
/// UCI::to_move() converts a string representing a move in coordinate notation
|
|
|
|
/// (g1f3, a7a8q) to the corresponding legal Move, if any.
|
2014-10-26 00:50:09 -06:00
|
|
|
|
|
|
|
Move UCI::to_move(const Position& pos, string& str) {
|
|
|
|
|
|
|
|
if (str.length() == 5) // Junior could send promotion piece in uppercase
|
|
|
|
str[4] = char(tolower(str[4]));
|
|
|
|
|
2015-01-31 10:39:51 -07:00
|
|
|
for (const auto& m : MoveList<LEGAL>(pos))
|
|
|
|
if (str == UCI::move(m, pos.is_chess960()))
|
|
|
|
return m;
|
2014-10-26 00:50:09 -06:00
|
|
|
|
|
|
|
return MOVE_NONE;
|
|
|
|
}
|