X-Git-Url: https://git.sesse.net/?p=stockfish;a=blobdiff_plain;f=src%2Fposition.cpp;h=cd91b07fe773e5b9ba766d4055a36867c72b5c5f;hp=c432bb90e9f51fcf93d7f246a04b98916aeb4366;hb=c73706243672bf36b0fef58e817f843cb341d8ca;hpb=80d7556af785f57a4f90d121c57c9c2f85a2963e diff --git a/src/position.cpp b/src/position.cpp index c432bb90..cd91b07f 100644 --- a/src/position.cpp +++ b/src/position.cpp @@ -2,6 +2,7 @@ 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 @@ -23,7 +24,7 @@ #include #include -#include "bitcount.h" +#include "bitboard.h" #include "misc.h" #include "movegen.h" #include "position.h" @@ -33,10 +34,6 @@ 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]; @@ -118,7 +115,7 @@ 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)) << " "; @@ -158,42 +155,11 @@ void Position::init() { } -/// 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; -} - - /// Position::set() initializes the position object with the given FEN string. /// 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. @@ -233,7 +199,11 @@ void Position::set(const string& fenStr, bool isChess960, Thread* th) { 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 @@ -294,6 +264,8 @@ void Position::set(const string& fenStr, bool isChess960, Thread* th) { 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; @@ -307,6 +279,8 @@ void Position::set(const string& fenStr, bool isChess960, Thread* th) { set_state(st); assert(pos_is_ok()); + + return *this; } @@ -527,8 +501,7 @@ bool Position::legal(Move m, Bitboard pinned) const { // 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 & from) || aligned(from, to_sq(m), square(us)); } @@ -621,8 +594,7 @@ bool Position::gives_check(Move m, const CheckInfo& ci) const { return true; // Is there a discovered check? - if ( ci.dcCandidates - && (ci.dcCandidates & from) + if ( (ci.dcCandidates & from) && !aligned(from, to, ci.ksq)) return true; @@ -1111,7 +1083,7 @@ void Position::flip() { 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()); } @@ -1169,7 +1141,7 @@ bool Position::pos_is_ok(int* failedStep) const { for (Color c = WHITE; c <= BLACK; ++c) for (PieceType pt = PAWN; pt <= KING; ++pt) { - if (pieceCount[c][pt] != popcount(pieces(c, pt))) + if (pieceCount[c][pt] != popcount(pieces(c, pt))) return false; for (int i = 0; i < pieceCount[c][pt]; ++i)