Stockfish, a UCI chess playing engine derived from Glaurung 2.1
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
Copyright (C) 2008-2015 Marco Costalba, Joona Kiiski, Tord Romstad
+ Copyright (C) 2015-2016 Marco Costalba, Joona Kiiski, Gary Linscott, Tord Romstad
Stockfish is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
#include <iomanip>
#include <sstream>
-#include "bitcount.h"
+#include "bitboard.h"
#include "misc.h"
#include "movegen.h"
#include "position.h"
using std::string;
-Value PieceValue[PHASE_NB][PIECE_NB] = {
-{ VALUE_ZERO, PawnValueMg, KnightValueMg, BishopValueMg, RookValueMg, QueenValueMg },
-{ VALUE_ZERO, PawnValueEg, KnightValueEg, BishopValueEg, RookValueEg, QueenValueEg } };
-
namespace Zobrist {
Key psq[COLOR_NB][PIECE_TYPE_NB][SQUARE_NB];
Key enpassant[FILE_NB];
Key castling[CASTLING_RIGHT_NB];
Key side;
- Key exclusion;
}
-Key Position::exclusion_key() const { return st->key ^ Zobrist::exclusion; }
-
namespace {
const string PieceToChar(" PNBRQK pnbrqk");
} // namespace
-/// CheckInfo c'tor
-
-CheckInfo::CheckInfo(const Position& pos) {
-
- Color them = ~pos.side_to_move();
- ksq = pos.square<KING>(them);
-
- pinned = pos.pinned_pieces(pos.side_to_move());
- dcCandidates = pos.discovered_check_candidates();
-
- checkSquares[PAWN] = pos.attacks_from<PAWN>(ksq, them);
- checkSquares[KNIGHT] = pos.attacks_from<KNIGHT>(ksq);
- checkSquares[BISHOP] = pos.attacks_from<BISHOP>(ksq);
- checkSquares[ROOK] = pos.attacks_from<ROOK>(ksq);
- checkSquares[QUEEN] = checkSquares[BISHOP] | checkSquares[ROOK];
- checkSquares[KING] = 0;
-}
-
-
/// operator<<(Position) returns an ASCII representation of the position
std::ostream& operator<<(std::ostream& os, const Position& pos) {
}
os << "\nFen: " << pos.fen() << "\nKey: " << std::hex << std::uppercase
- << std::setfill('0') << std::setw(16) << pos.st->key << std::dec << "\nCheckers: ";
+ << std::setfill('0') << std::setw(16) << pos.key() << std::dec << "\nCheckers: ";
for (Bitboard b = pos.checkers(); b; )
os << UCI::square(pop_lsb(&b)) << " ";
}
Zobrist::side = rng.rand<Key>();
- Zobrist::exclusion = rng.rand<Key>();
-}
-
-
-/// Position::operator=() creates a copy of 'pos' but detaching the state pointer
-/// from the source to be self-consistent and not depending on any external data.
-
-Position& Position::operator=(const Position& pos) {
-
- std::memcpy(this, &pos, sizeof(Position));
- std::memcpy(&startState, st, sizeof(StateInfo));
- st = &startState;
- nodes = 0;
-
- assert(pos_is_ok());
-
- return *this;
-}
-
-
-/// Position::clear() erases the position object to a pristine state, with an
-/// empty board, white to move, and no castling rights.
-
-void Position::clear() {
-
- std::memset(this, 0, sizeof(Position));
- startState.epSquare = SQ_NONE;
- st = &startState;
-
- for (int i = 0; i < PIECE_TYPE_NB; ++i)
- for (int j = 0; j < 16; ++j)
- pieceList[WHITE][i][j] = pieceList[BLACK][i][j] = SQ_NONE;
}
/// This function is not very robust - make sure that input FENs are correct,
/// this is assumed to be the responsibility of the GUI.
-void Position::set(const string& fenStr, bool isChess960, Thread* th) {
+Position& Position::set(const string& fenStr, bool isChess960, StateInfo* si, Thread* th) {
/*
A FEN string defines a particular position using only the ASCII character set.
Square sq = SQ_A8;
std::istringstream ss(fenStr);
- clear();
+ std::memset(this, 0, sizeof(Position));
+ std::memset(si, 0, sizeof(StateInfo));
+ std::fill_n(&pieceList[0][0][0], sizeof(pieceList) / sizeof(Square), SQ_NONE);
+ st = si;
+
ss >> std::noskipws;
// 1. Piece placement
if (!(attackers_to(st->epSquare) & pieces(sideToMove, PAWN)))
st->epSquare = SQ_NONE;
}
+ else
+ st->epSquare = SQ_NONE;
// 5-6. Halfmove clock and fullmove number
ss >> std::skipws >> st->rule50 >> gamePly;
set_state(st);
assert(pos_is_ok());
+
+ return *this;
}
}
+/// Position::set_check_info() sets king attacks to detect if a move gives check
+
+void Position::set_check_info(StateInfo* si) const {
+
+ si->blockersForKing[WHITE] = slider_blockers(pieces(BLACK), square<KING>(WHITE));
+ si->blockersForKing[BLACK] = slider_blockers(pieces(WHITE), square<KING>(BLACK));
+
+ Square ksq = square<KING>(~sideToMove);
+
+ si->checkSquares[PAWN] = attacks_from<PAWN>(ksq, ~sideToMove);
+ si->checkSquares[KNIGHT] = attacks_from<KNIGHT>(ksq);
+ si->checkSquares[BISHOP] = attacks_from<BISHOP>(ksq);
+ si->checkSquares[ROOK] = attacks_from<ROOK>(ksq);
+ si->checkSquares[QUEEN] = si->checkSquares[BISHOP] | si->checkSquares[ROOK];
+ si->checkSquares[KING] = 0;
+}
+
+
/// Position::set_state() computes the hash keys of the position, and other
/// data that once computed is updated incrementally as moves are made.
/// The function is only used when a new position is set up, and to verify
si->key = si->pawnKey = si->materialKey = 0;
si->nonPawnMaterial[WHITE] = si->nonPawnMaterial[BLACK] = VALUE_ZERO;
si->psq = SCORE_ZERO;
-
si->checkersBB = attackers_to(square<KING>(sideToMove)) & pieces(~sideToMove);
+ set_check_info(si);
+
for (Bitboard b = pieces(); b; )
{
Square s = pop_lsb(&b);
}
-/// 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
-/// position where the king is in check. A check blocking piece can be either a
-/// pinned or a discovered check piece, according if its color 'c' is the same
-/// or the opposite of 'kingColor'.
+/// Position::slider_blockers() returns a bitboard of all the pieces (both colors) that
+/// are blocking attacks on the square 's' from 'sliders'. A piece blocks a slider
+/// if removing that piece from the board would result in a position where square 's'
+/// is attacked. For example, a king-attack blocking piece can be either a pinned or
+/// a discovered check piece, according if its color is the opposite or the same of
+/// the color of the slider.
-Bitboard Position::check_blockers(Color c, Color kingColor) const {
+Bitboard Position::slider_blockers(Bitboard sliders, Square s) const {
Bitboard b, pinners, result = 0;
- Square ksq = square<KING>(kingColor);
- // Pinners are sliders that give check when a pinned piece is removed
- pinners = ( (pieces( ROOK, QUEEN) & PseudoAttacks[ROOK ][ksq])
- | (pieces(BISHOP, QUEEN) & PseudoAttacks[BISHOP][ksq])) & pieces(~kingColor);
+ // Pinners are sliders that attack 's' when a pinned piece is removed
+ pinners = ( (PseudoAttacks[ROOK ][s] & pieces(QUEEN, ROOK))
+ | (PseudoAttacks[BISHOP][s] & pieces(QUEEN, BISHOP))) & sliders;
while (pinners)
{
- b = between_bb(ksq, pop_lsb(&pinners)) & pieces();
+ b = between_bb(s, pop_lsb(&pinners)) & pieces();
if (!more_than_one(b))
- result |= b & pieces(c);
+ result |= b;
}
return result;
}
/// Position::legal() tests whether a pseudo-legal move is legal
-bool Position::legal(Move m, Bitboard pinned) const {
+bool Position::legal(Move m) const {
assert(is_ok(m));
- assert(pinned == pinned_pieces(sideToMove));
Color us = sideToMove;
Square from = from_sq(m);
// A non-king move is legal if and only if it is not pinned or it
// is moving along the ray towards or away from the king.
- return !pinned
- || !(pinned & from)
+ return !(pinned_pieces(us) & from)
|| aligned(from, to_sq(m), square<KING>(us));
}
/// Position::gives_check() tests whether a pseudo-legal move gives a check
-bool Position::gives_check(Move m, const CheckInfo& ci) const {
+bool Position::gives_check(Move m) const {
assert(is_ok(m));
- assert(ci.dcCandidates == discovered_check_candidates());
assert(color_of(moved_piece(m)) == sideToMove);
Square from = from_sq(m);
Square to = to_sq(m);
// Is there a direct check?
- if (ci.checkSquares[type_of(piece_on(from))] & to)
+ if (st->checkSquares[type_of(piece_on(from))] & to)
return true;
// Is there a discovered check?
- if ( ci.dcCandidates
- && (ci.dcCandidates & from)
- && !aligned(from, to, ci.ksq))
+ if ( (discovered_check_candidates() & from)
+ && !aligned(from, to, square<KING>(~sideToMove)))
return true;
switch (type_of(m))
return false;
case PROMOTION:
- return attacks_bb(Piece(promotion_type(m)), to, pieces() ^ from) & ci.ksq;
+ return attacks_bb(Piece(promotion_type(m)), to, pieces() ^ from) & square<KING>(~sideToMove);
// En passant capture with check? We have already handled the case
// of direct checks and ordinary discovered check, so the only case we
Square capsq = make_square(file_of(to), rank_of(from));
Bitboard b = (pieces() ^ from ^ capsq) | to;
- return (attacks_bb< ROOK>(ci.ksq, b) & pieces(sideToMove, QUEEN, ROOK))
- | (attacks_bb<BISHOP>(ci.ksq, b) & pieces(sideToMove, QUEEN, BISHOP));
+ return (attacks_bb< ROOK>(square<KING>(~sideToMove), b) & pieces(sideToMove, QUEEN, ROOK))
+ | (attacks_bb<BISHOP>(square<KING>(~sideToMove), b) & pieces(sideToMove, QUEEN, BISHOP));
}
case CASTLING:
{
Square kto = relative_square(sideToMove, rfrom > kfrom ? SQ_G1 : SQ_C1);
Square rto = relative_square(sideToMove, rfrom > kfrom ? SQ_F1 : SQ_D1);
- return (PseudoAttacks[ROOK][rto] & ci.ksq)
- && (attacks_bb<ROOK>(rto, (pieces() ^ kfrom ^ rfrom) | rto | kto) & ci.ksq);
+ return (PseudoAttacks[ROOK][rto] & square<KING>(~sideToMove))
+ && (attacks_bb<ROOK>(rto, (pieces() ^ kfrom ^ rfrom) | rto | kto) & square<KING>(~sideToMove));
}
default:
assert(false);
sideToMove = ~sideToMove;
+ // Update king attacks used for fast check detection
+ set_check_info(st);
+
assert(pos_is_ok());
}
sideToMove = ~sideToMove;
+ set_check_info(st);
+
assert(pos_is_ok());
}
std::getline(ss, token); // Half and full moves
f += token;
- set(f, is_chess960(), this_thread());
+ set(f, is_chess960(), st, this_thread());
assert(pos_is_ok());
}
for (Color c = WHITE; c <= BLACK; ++c)
for (PieceType pt = PAWN; pt <= KING; ++pt)
{
- if (pieceCount[c][pt] != popcount<Full>(pieces(c, pt)))
+ if (pieceCount[c][pt] != popcount(pieces(c, pt)))
return false;
for (int i = 0; i < pieceCount[c][pt]; ++i)