/// or the FEN string, we want the new born Position object do not depend
/// on any external data so we detach state pointer from the source one.
-Position::Position(int th) : threadID(th) {}
-
Position::Position(const Position& pos, int th) {
memcpy(this, &pos, sizeof(Position));
detach(); // Always detach() in copy c'tor to avoid surprises
threadID = th;
+ nodes = 0;
}
Position::Position(const string& fen, int th) {
/// Position::print() prints an ASCII representation of the position to
-/// the standard output. If a move is given then also the san is print.
+/// the standard output. If a move is given then also the san is printed.
void Position::print(Move move) const {
assert(move_is_ok(m));
assert(square_is_ok(s));
+ Bitboard occ, xray;
Square f = move_from(m), t = move_to(m);
assert(square_is_occupied(f));
return true;
// Move the piece and scan for X-ray attacks behind it
- Bitboard occ = occupied_squares();
- Color us = color_of_piece_on(f);
- clear_bit(&occ, f);
- set_bit(&occ, t);
- Bitboard xray = ( (rook_attacks_bb(s, occ) & pieces(ROOK, QUEEN))
- |(bishop_attacks_bb(s, occ) & pieces(BISHOP, QUEEN))) & pieces_of_color(us);
+ occ = occupied_squares();
+ do_move_bb(&occ, make_move_bb(f, t));
+ xray = ( (rook_attacks_bb(s, occ) & pieces(ROOK, QUEEN))
+ |(bishop_attacks_bb(s, occ) & pieces(BISHOP, QUEEN)))
+ & pieces_of_color(color_of_piece_on(f));
// If we have attacks we need to verify that are caused by our move
// and are not already existent ones.
assert(is_ok());
assert(move_is_ok(m));
+ nodes++;
Key key = st->key;
// Copy some fields of old state to our new StateInfo object except the
Value npMaterial[2];
};
- memcpy(&newSt, st, sizeof(ReducedStateInfo));
+ if (&newSt != st)
+ memcpy(&newSt, st, sizeof(ReducedStateInfo));
+
newSt.previous = st;
st = &newSt;
memset(st, 0, sizeof(StateInfo));
st->epSquare = SQ_NONE;
startPosPlyCounter = 0;
+ nodes = 0;
memset(byColorBB, 0, sizeof(Bitboard) * 2);
memset(byTypeBB, 0, sizeof(Bitboard) * 8);
Key Position::compute_key() const {
- Key result = Key(0ULL);
+ Key result = 0;
for (Square s = SQ_A1; s <= SQ_H8; s++)
if (square_is_occupied(s))
Key Position::compute_pawn_key() const {
- Key result = Key(0ULL);
+ Key result = 0;
Bitboard b;
Square s;
Key Position::compute_material_key() const {
- Key result = Key(0ULL);
+ Key result = 0;
for (Color c = WHITE; c <= BLACK; c++)
for (PieceType pt = PAWN; pt <= QUEEN; pt++)
{
/// Position::is_draw() tests whether the position is drawn by material,
/// repetition, or the 50 moves rule. It does not detect stalemates, this
/// must be done by the search.
-// FIXME: Currently we are not handling 50 move rule correctly when in check
bool Position::is_draw() const {
return true;
// Draw by the 50 moves rule?
- if (st->rule50 > 100 || (st->rule50 == 100 && !is_check()))
+ if (st->rule50 > 99 && (st->rule50 > 100 || !is_mate()))
return true;
// Draw by repetition?
bool Position::is_mate() const {
- MoveStack moves[256];
+ MoveStack moves[MOVES_MAX];
return is_check() && (generate_moves(*this, moves) == moves);
}
bool Position::has_mate_threat() {
- MoveStack mlist[256], *last, *cur;
+ MoveStack mlist[MOVES_MAX], *last, *cur;
StateInfo st1, st2;
bool mateFound = false;
// Is there more than 2 checkers?
if (failedStep) (*failedStep)++;
- if (debugCheckerCount && count_1s(st->checkersBB) > 2)
+ if (debugCheckerCount && count_1s<CNT32>(st->checkersBB) > 2)
return false;
// Bitboards OK?
if (debugPieceCounts)
for (Color c = WHITE; c <= BLACK; c++)
for (PieceType pt = PAWN; pt <= KING; pt++)
- if (pieceCount[c][pt] != count_1s(pieces(pt, c)))
+ if (pieceCount[c][pt] != count_1s<CNT32>(pieces(pt, c)))
return false;
if (failedStep) (*failedStep)++;