#include "bitcount.h"
#include "movegen.h"
-#include "notation.h"
#include "position.h"
#include "psqtab.h"
#include "rkiss.h"
#include "thread.h"
#include "tt.h"
+#include "uci.h"
using std::string;
-static const string PieceToChar(" PNBRQK pnbrqk");
-
CACHE_LINE_ALIGNMENT
Value PieceValue[PHASE_NB][PIECE_NB] = {
{ VALUE_ZERO, PawnValueMg, KnightValueMg, BishopValueMg, RookValueMg, QueenValueMg },
{ VALUE_ZERO, PawnValueEg, KnightValueEg, BishopValueEg, RookValueEg, QueenValueEg } };
-static Score psq[COLOR_NB][PIECE_TYPE_NB][SQUARE_NB];
-
namespace Zobrist {
Key psq[COLOR_NB][PIECE_TYPE_NB][SQUARE_NB];
namespace {
+const string PieceToChar(" PNBRQK pnbrqk");
+Score psq[COLOR_NB][PIECE_TYPE_NB][SQUARE_NB];
+
// min_attacker() is a helper function used by see() to locate the least
// valuable attacker for the side to move, remove the attacker we just found
// from the bitboards and scan for new X-ray attacks behind it.
incremented after Black's move.
*/
- char col, row, token;
+ unsigned char col, row, token;
size_t idx;
Square sq = SQ_A8;
std::istringstream ss(fenStr);
// Convert from fullmove starting from 1 to ply starting from 0,
// handle also common incorrect FEN with fullmove = 0.
- gamePly = std::max(2 * (gamePly - 1), 0) + int(sideToMove == BLACK);
+ gamePly = std::max(2 * (gamePly - 1), 0) + (sideToMove == BLACK);
chess960 = isChess960;
thisThread = th;
ss << (sideToMove == WHITE ? " w " : " b ");
if (can_castle(WHITE_OO))
- ss << (chess960 ? to_char(file_of(castling_rook_square(WHITE | KING_SIDE)), false) : 'K');
+ ss << (chess960 ? 'A' + file_of(castling_rook_square(WHITE | KING_SIDE)) : 'K');
if (can_castle(WHITE_OOO))
- ss << (chess960 ? to_char(file_of(castling_rook_square(WHITE | QUEEN_SIDE)), false) : 'Q');
+ ss << (chess960 ? 'A' + file_of(castling_rook_square(WHITE | QUEEN_SIDE)) : 'Q');
if (can_castle(BLACK_OO))
- ss << (chess960 ? to_char(file_of(castling_rook_square(BLACK | KING_SIDE)), true) : 'k');
+ ss << (chess960 ? 'a' + file_of(castling_rook_square(BLACK | KING_SIDE)) : 'k');
if (can_castle(BLACK_OOO))
- ss << (chess960 ? to_char(file_of(castling_rook_square(BLACK | QUEEN_SIDE)), true) : 'q');
+ ss << (chess960 ? 'a' + file_of(castling_rook_square(BLACK | QUEEN_SIDE)) : 'q');
if (!can_castle(WHITE) && !can_castle(BLACK))
ss << '-';
- ss << (ep_square() == SQ_NONE ? " - " : " " + to_string(ep_square()) + " ")
- << st->rule50 << " " << 1 + (gamePly - int(sideToMove == BLACK)) / 2;
+ ss << (ep_square() == SQ_NONE ? " - " : " " + UCI::format_square(ep_square()) + " ")
+ << st->rule50 << " " << 1 + (gamePly - (sideToMove == BLACK)) / 2;
return ss.str();
}
-/// Position::pretty() returns an ASCII representation of the position to be
-/// printed to the standard output together with the move's san notation.
+/// Position::pretty() returns an ASCII representation of the position
-const string Position::pretty(Move m) const {
+const string Position::pretty() const {
std::ostringstream ss;
- if (m)
- ss << "\nMove: " << (sideToMove == BLACK ? ".." : "")
- << move_to_san(*const_cast<Position*>(this), m);
-
ss << "\n +---+---+---+---+---+---+---+---+\n";
for (Rank r = RANK_8; r >= RANK_1; --r)
<< std::setfill('0') << std::setw(16) << st->key << "\nCheckers: ";
for (Bitboard b = checkers(); b; )
- ss << to_string(pop_lsb(&b)) << " ";
-
- ss << "\nLegal moves: ";
- for (MoveList<LEGAL> it(*this); *it; ++it)
- ss << move_to_san(*const_cast<Position*>(this), *it) << " ";
+ ss << UCI::format_square(pop_lsb(&b)) << " ";
return ss.str();
}
+/// Position::game_phase() calculates the game phase interpolating total non-pawn
+/// material between endgame and midgame limits.
+
+Phase Position::game_phase() const {
+
+ Value npm = st->npMaterial[WHITE] + st->npMaterial[BLACK];
+
+ npm = std::max(EndgameLimit, std::min(npm, MidgameLimit));
+
+ return Phase(((npm - EndgameLimit) * 128) / (MidgameLimit - EndgameLimit));
+}
+
+
/// Position::check_blockers() returns a bitboard of all the pieces with color
/// 'c' that are blocking check on the king with color 'kingColor'. A piece
/// blocks a check if removing that piece from the board would result in a
st->castlingRights &= ~cr;
}
- // Prefetch TT access as soon as we know the new hash key
- prefetch((char*)TT.first_entry(k));
-
// Move the piece. The tricky Chess960 castling is handled earlier
if (type_of(m) != CASTLING)
move_piece(from, to, us, pt);
k ^= Zobrist::enpassant[file_of(st->epSquare)];
}
- if (type_of(m) == PROMOTION)
+ else if (type_of(m) == PROMOTION)
{
PieceType promotion = promotion_type(m);
}
+/// Position::key_after() computes the new hash key after the given moven. Needed
+/// for speculative prefetch. It doesn't recognize special moves like castling,
+/// en-passant and promotions.
+
+Key Position::key_after(Move m) const {
+
+ Color us = sideToMove;
+ Square from = from_sq(m);
+ Square to = to_sq(m);
+ PieceType pt = type_of(piece_on(from));
+ PieceType captured = type_of(piece_on(to));
+ Key k = st->key ^ Zobrist::side;
+
+ if (captured)
+ k ^= Zobrist::psq[~us][captured][to];
+
+ return k ^ Zobrist::psq[us][pt][to] ^ Zobrist::psq[us][pt][from];
+}
+
+
/// Position::see() is a static exchange evaluator: It tries to estimate the
/// material gain or loss resulting from a move.
bool Position::is_draw() const {
- if ( !pieces(PAWN)
- && (non_pawn_material(WHITE) + non_pawn_material(BLACK) <= BishopValueMg))
- return true;
-
if (st->rule50 > 99 && (!checkers() || MoveList<LEGAL>(*this).size()))
return true;