2012-09-16 17:16:15 +02:00
|
|
|
/*
|
|
|
|
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
|
|
|
|
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
|
2014-05-31 14:23:03 +02:00
|
|
|
Copyright (C) 2008-2014 Marco Costalba, Joona Kiiski, Tord Romstad
|
2012-09-16 17:16:15 +02:00
|
|
|
|
|
|
|
Stockfish is free software: you can redistribute it and/or modify
|
|
|
|
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.
|
|
|
|
|
|
|
|
Stockfish is distributed in the hope that it will be useful,
|
|
|
|
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.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <cassert>
|
|
|
|
#include <iomanip>
|
|
|
|
#include <sstream>
|
2013-05-03 19:03:42 +02:00
|
|
|
#include <stack>
|
2012-09-16 17:16:15 +02:00
|
|
|
|
|
|
|
#include "movegen.h"
|
|
|
|
#include "notation.h"
|
|
|
|
#include "position.h"
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
2013-05-03 19:03:42 +02:00
|
|
|
static const char* PieceToChar[COLOR_NB] = { " PNBRQK", " pnbrqk" };
|
2012-09-16 17:16:15 +02:00
|
|
|
|
|
|
|
|
|
|
|
/// score_to_uci() converts a value to a string suitable for use with the UCI
|
|
|
|
/// protocol specifications:
|
|
|
|
///
|
|
|
|
/// 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.
|
|
|
|
|
|
|
|
string score_to_uci(Value v, Value alpha, Value beta) {
|
|
|
|
|
2014-05-31 14:23:03 +02:00
|
|
|
stringstream ss;
|
2012-09-16 17:16:15 +02:00
|
|
|
|
|
|
|
if (abs(v) < VALUE_MATE_IN_MAX_PLY)
|
2014-05-31 14:23:03 +02:00
|
|
|
ss << "cp " << v * 100 / PawnValueEg;
|
2012-09-16 17:16:15 +02:00
|
|
|
else
|
2014-05-31 14:23:03 +02:00
|
|
|
ss << "mate " << (v > 0 ? VALUE_MATE - v + 1 : -VALUE_MATE - v) / 2;
|
2012-09-16 17:16:15 +02:00
|
|
|
|
2014-05-31 14:23:03 +02:00
|
|
|
ss << (v >= beta ? " lowerbound" : v <= alpha ? " upperbound" : "");
|
2012-09-16 17:16:15 +02:00
|
|
|
|
2014-05-31 14:23:03 +02:00
|
|
|
return ss.str();
|
2012-09-16 17:16:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// move_to_uci() converts a move to a string in coordinate notation
|
|
|
|
/// (g1f3, a7a8q, etc.). The only special case is castling moves, where we print
|
|
|
|
/// in the e1g1 notation in normal chess mode, and in e1h1 notation in chess960
|
2014-05-31 14:23:03 +02:00
|
|
|
/// mode. Internally castling moves are always encoded as "king captures rook".
|
2012-09-16 17:16:15 +02:00
|
|
|
|
|
|
|
const string move_to_uci(Move m, bool chess960) {
|
|
|
|
|
|
|
|
Square from = from_sq(m);
|
|
|
|
Square to = to_sq(m);
|
|
|
|
|
|
|
|
if (m == MOVE_NONE)
|
|
|
|
return "(none)";
|
|
|
|
|
|
|
|
if (m == MOVE_NULL)
|
|
|
|
return "0000";
|
|
|
|
|
2014-05-31 14:23:03 +02:00
|
|
|
if (type_of(m) == CASTLING && !chess960)
|
|
|
|
to = make_square(to > from ? FILE_G : FILE_C, rank_of(from));
|
2012-09-16 17:16:15 +02:00
|
|
|
|
2014-05-31 14:23:03 +02:00
|
|
|
string move = to_string(from) + to_string(to);
|
2012-09-16 17:16:15 +02:00
|
|
|
|
|
|
|
if (type_of(m) == PROMOTION)
|
2013-05-03 19:03:42 +02:00
|
|
|
move += PieceToChar[BLACK][promotion_type(m)]; // Lower case
|
2012-09-16 17:16:15 +02:00
|
|
|
|
|
|
|
return move;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// move_from_uci() takes a position and a string representing a move in
|
|
|
|
/// simple coordinate notation and returns an equivalent legal Move if any.
|
|
|
|
|
|
|
|
Move move_from_uci(const Position& pos, string& str) {
|
|
|
|
|
|
|
|
if (str.length() == 5) // Junior could send promotion piece in uppercase
|
|
|
|
str[4] = char(tolower(str[4]));
|
|
|
|
|
2013-08-20 20:02:33 +02:00
|
|
|
for (MoveList<LEGAL> it(pos); *it; ++it)
|
|
|
|
if (str == move_to_uci(*it, pos.is_chess960()))
|
|
|
|
return *it;
|
2012-09-16 17:16:15 +02:00
|
|
|
|
|
|
|
return MOVE_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// move_to_san() takes a position and a legal Move as input and returns its
|
|
|
|
/// short algebraic notation representation.
|
|
|
|
|
|
|
|
const string move_to_san(Position& pos, Move m) {
|
|
|
|
|
|
|
|
if (m == MOVE_NONE)
|
|
|
|
return "(none)";
|
|
|
|
|
|
|
|
if (m == MOVE_NULL)
|
|
|
|
return "(null)";
|
|
|
|
|
2013-05-03 19:03:42 +02:00
|
|
|
assert(MoveList<LEGAL>(pos).contains(m));
|
2012-09-16 17:16:15 +02:00
|
|
|
|
2013-05-03 19:03:42 +02:00
|
|
|
Bitboard others, b;
|
2012-09-16 17:16:15 +02:00
|
|
|
string san;
|
|
|
|
Color us = pos.side_to_move();
|
|
|
|
Square from = from_sq(m);
|
|
|
|
Square to = to_sq(m);
|
|
|
|
Piece pc = pos.piece_on(from);
|
|
|
|
PieceType pt = type_of(pc);
|
|
|
|
|
2014-05-31 14:23:03 +02:00
|
|
|
if (type_of(m) == CASTLING)
|
2012-09-16 17:16:15 +02:00
|
|
|
san = to > from ? "O-O" : "O-O-O";
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (pt != PAWN)
|
|
|
|
{
|
2013-05-03 19:03:42 +02:00
|
|
|
san = PieceToChar[WHITE][pt]; // Upper case
|
2012-09-16 17:16:15 +02:00
|
|
|
|
2014-05-31 14:23:03 +02:00
|
|
|
// A disambiguation occurs if we have more then one piece of type 'pt'
|
|
|
|
// that can reach 'to' with a legal move.
|
2013-05-03 19:03:42 +02:00
|
|
|
others = b = (pos.attacks_from(pc, to) & pos.pieces(us, pt)) ^ from;
|
2012-09-16 17:16:15 +02:00
|
|
|
|
2013-05-03 19:03:42 +02:00
|
|
|
while (b)
|
2012-09-16 17:16:15 +02:00
|
|
|
{
|
2014-05-31 14:23:03 +02:00
|
|
|
Square s = pop_lsb(&b);
|
|
|
|
if (!pos.legal(make_move(s, to), pos.pinned_pieces(us)))
|
|
|
|
others ^= s;
|
2012-09-16 17:16:15 +02:00
|
|
|
}
|
|
|
|
|
2014-05-31 14:23:03 +02:00
|
|
|
if (!others)
|
|
|
|
{ /* Disambiguation is not needed */ }
|
2012-09-16 17:16:15 +02:00
|
|
|
|
2014-05-31 14:23:03 +02:00
|
|
|
else if (!(others & file_bb(from)))
|
|
|
|
san += to_char(file_of(from));
|
2012-09-16 17:16:15 +02:00
|
|
|
|
2014-05-31 14:23:03 +02:00
|
|
|
else if (!(others & rank_bb(from)))
|
|
|
|
san += to_char(rank_of(from));
|
|
|
|
|
|
|
|
else
|
|
|
|
san += to_string(from);
|
2012-09-16 17:16:15 +02:00
|
|
|
}
|
2013-11-30 20:12:34 +01:00
|
|
|
else if (pos.capture(m))
|
2014-05-31 14:23:03 +02:00
|
|
|
san = to_char(file_of(from));
|
2012-09-16 17:16:15 +02:00
|
|
|
|
2013-11-30 20:12:34 +01:00
|
|
|
if (pos.capture(m))
|
2012-09-16 17:16:15 +02:00
|
|
|
san += 'x';
|
|
|
|
|
2014-05-31 14:23:03 +02:00
|
|
|
san += to_string(to);
|
2012-09-16 17:16:15 +02:00
|
|
|
|
|
|
|
if (type_of(m) == PROMOTION)
|
2013-05-03 19:03:42 +02:00
|
|
|
san += string("=") + PieceToChar[WHITE][promotion_type(m)];
|
2012-09-16 17:16:15 +02:00
|
|
|
}
|
|
|
|
|
2013-11-30 20:12:34 +01:00
|
|
|
if (pos.gives_check(m, CheckInfo(pos)))
|
2012-09-16 17:16:15 +02:00
|
|
|
{
|
|
|
|
StateInfo st;
|
|
|
|
pos.do_move(m, st);
|
|
|
|
san += MoveList<LEGAL>(pos).size() ? "+" : "#";
|
|
|
|
pos.undo_move(m);
|
|
|
|
}
|
|
|
|
|
|
|
|
return san;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// pretty_pv() formats human-readable search information, typically to be
|
|
|
|
/// appended to the search log file. It uses the two helpers below to pretty
|
2014-05-31 14:23:03 +02:00
|
|
|
/// format the time and score respectively.
|
2012-09-16 17:16:15 +02:00
|
|
|
|
2014-05-31 14:23:03 +02:00
|
|
|
static string format(int64_t msecs) {
|
2012-09-16 17:16:15 +02:00
|
|
|
|
|
|
|
const int MSecMinute = 1000 * 60;
|
|
|
|
const int MSecHour = 1000 * 60 * 60;
|
|
|
|
|
|
|
|
int64_t hours = msecs / MSecHour;
|
|
|
|
int64_t minutes = (msecs % MSecHour) / MSecMinute;
|
|
|
|
int64_t seconds = ((msecs % MSecHour) % MSecMinute) / 1000;
|
|
|
|
|
2014-05-31 14:23:03 +02:00
|
|
|
stringstream ss;
|
2012-09-16 17:16:15 +02:00
|
|
|
|
|
|
|
if (hours)
|
2014-05-31 14:23:03 +02:00
|
|
|
ss << hours << ':';
|
2012-09-16 17:16:15 +02:00
|
|
|
|
2014-05-31 14:23:03 +02:00
|
|
|
ss << setfill('0') << setw(2) << minutes << ':' << setw(2) << seconds;
|
2012-09-16 17:16:15 +02:00
|
|
|
|
2014-05-31 14:23:03 +02:00
|
|
|
return ss.str();
|
2012-09-16 17:16:15 +02:00
|
|
|
}
|
|
|
|
|
2014-05-31 14:23:03 +02:00
|
|
|
static string format(Value v) {
|
2012-09-16 17:16:15 +02:00
|
|
|
|
2014-05-31 14:23:03 +02:00
|
|
|
stringstream ss;
|
2012-09-16 17:16:15 +02:00
|
|
|
|
|
|
|
if (v >= VALUE_MATE_IN_MAX_PLY)
|
2014-05-31 14:23:03 +02:00
|
|
|
ss << "#" << (VALUE_MATE - v + 1) / 2;
|
2012-09-16 17:16:15 +02:00
|
|
|
|
|
|
|
else if (v <= VALUE_MATED_IN_MAX_PLY)
|
2014-05-31 14:23:03 +02:00
|
|
|
ss << "-#" << (VALUE_MATE + v) / 2;
|
2012-09-16 17:16:15 +02:00
|
|
|
|
|
|
|
else
|
2014-05-31 14:23:03 +02:00
|
|
|
ss << setprecision(2) << fixed << showpos << double(v) / PawnValueEg;
|
2012-09-16 17:16:15 +02:00
|
|
|
|
2014-05-31 14:23:03 +02:00
|
|
|
return ss.str();
|
2012-09-16 17:16:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
string pretty_pv(Position& pos, int depth, Value value, int64_t msecs, Move pv[]) {
|
|
|
|
|
2014-05-31 14:23:03 +02:00
|
|
|
const uint64_t K = 1000;
|
|
|
|
const uint64_t M = 1000000;
|
2012-09-16 17:16:15 +02:00
|
|
|
|
2013-05-03 19:03:42 +02:00
|
|
|
std::stack<StateInfo> st;
|
2012-09-16 17:16:15 +02:00
|
|
|
Move* m = pv;
|
2014-05-31 14:23:03 +02:00
|
|
|
string san, str, padding;
|
|
|
|
stringstream ss;
|
2012-09-16 17:16:15 +02:00
|
|
|
|
2014-05-31 14:23:03 +02:00
|
|
|
ss << setw(2) << depth << setw(8) << format(value) << setw(8) << format(msecs);
|
2012-09-16 17:16:15 +02:00
|
|
|
|
|
|
|
if (pos.nodes_searched() < M)
|
2014-05-31 14:23:03 +02:00
|
|
|
ss << setw(8) << pos.nodes_searched() / 1 << " ";
|
2012-09-16 17:16:15 +02:00
|
|
|
|
|
|
|
else if (pos.nodes_searched() < K * M)
|
2014-05-31 14:23:03 +02:00
|
|
|
ss << setw(7) << pos.nodes_searched() / K << "K ";
|
2012-09-16 17:16:15 +02:00
|
|
|
|
|
|
|
else
|
2014-05-31 14:23:03 +02:00
|
|
|
ss << setw(7) << pos.nodes_searched() / M << "M ";
|
2012-09-16 17:16:15 +02:00
|
|
|
|
2014-05-31 14:23:03 +02:00
|
|
|
str = ss.str();
|
|
|
|
padding = string(str.length(), ' ');
|
2012-09-16 17:16:15 +02:00
|
|
|
|
|
|
|
while (*m != MOVE_NONE)
|
|
|
|
{
|
2014-05-31 14:23:03 +02:00
|
|
|
san = move_to_san(pos, *m) + ' ';
|
2012-09-16 17:16:15 +02:00
|
|
|
|
2014-05-31 14:23:03 +02:00
|
|
|
if ((str.length() + san.length()) % 80 <= san.length()) // Exceed 80 cols
|
|
|
|
str += "\n" + padding;
|
2012-09-16 17:16:15 +02:00
|
|
|
|
2014-05-31 14:23:03 +02:00
|
|
|
str += san;
|
2012-09-16 17:16:15 +02:00
|
|
|
|
2013-05-03 19:03:42 +02:00
|
|
|
st.push(StateInfo());
|
|
|
|
pos.do_move(*m++, st.top());
|
2012-09-16 17:16:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
while (m != pv)
|
|
|
|
pos.undo_move(*--m);
|
|
|
|
|
2014-05-31 14:23:03 +02:00
|
|
|
return str;
|
2012-09-16 17:16:15 +02:00
|
|
|
}
|