X-Git-Url: https://git.sesse.net/?p=stockfish;a=blobdiff_plain;f=src%2Fposition.cpp;h=c26ab1b6884abe0c17066dac51ada332f900033c;hp=f055b04c9a6563a6f243464e2cd90c1ea9a6a5d7;hb=3361ad42420e578362dcafb94b639738609bf7d7;hpb=456f37b8aba74018fc532099c13a9af4e6714867 diff --git a/src/position.cpp b/src/position.cpp index f055b04c..c26ab1b6 100644 --- a/src/position.cpp +++ b/src/position.cpp @@ -63,7 +63,7 @@ const Value PieceValueEndgame[17] = { }; // To convert a Piece to and from a FEN char -static const string PieceToChar(" PNBRQK pnbrqk ."); +static const string PieceToChar(" PNBRQK pnbrqk"); /// CheckInfo c'tor @@ -154,7 +154,7 @@ void Position::from_fen(const string& fenStr, bool isChess960, Thread* th) { sq += Square(token - '0'); // Advance the given number of files else if (token == '/') - sq = make_square(FILE_A, rank_of(sq) - Rank(2)); + sq -= Square(16); else if ((p = PieceToChar.find(token)) != string::npos) { @@ -201,7 +201,7 @@ void Position::from_fen(const string& fenStr, bool isChess960, Thread* th) { { st->epSquare = make_square(File(col - 'a'), Rank(row - '1')); - if (!(attackers_to(st->epSquare) & pieces(PAWN, sideToMove))) + if (!(attackers_to(st->epSquare) & pieces(sideToMove, PAWN))) st->epSquare = SQ_NONE; } @@ -319,7 +319,11 @@ const string Position::to_fen() const { void Position::print(Move move) const { - const char* dottedLine = "\n+---+---+---+---+---+---+---+---+\n"; + const string dottedLine = "\n+---+---+---+---+---+---+---+---+"; + const string twoRows = dottedLine + "\n| | . | | . | | . | | . |" + + dottedLine + "\n| . | | . | | . | | . | |"; + + string brd = twoRows + twoRows + twoRows + twoRows + dottedLine; if (move) { @@ -327,22 +331,11 @@ void Position::print(Move move) const { cout << "\nMove is: " << (sideToMove == BLACK ? ".." : "") << move_to_san(p, move); } - for (Rank rank = RANK_8; rank >= RANK_1; rank--) - { - cout << dottedLine << '|'; - for (File file = FILE_A; file <= FILE_H; file++) - { - Square sq = make_square(file, rank); - Piece piece = piece_on(sq); - char c = (color_of(piece) == BLACK ? '=' : ' '); - - if (piece == NO_PIECE && !opposite_colors(sq, SQ_A1)) - piece++; // Index the dot + for (Square sq = SQ_A1; sq <= SQ_H8; sq++) + if (piece_on(sq) != NO_PIECE) + brd[513 - 68*rank_of(sq) + 4*file_of(sq)] = PieceToChar[piece_on(sq)]; - cout << c << PieceToChar[piece] << c << '|'; - } - } - cout << dottedLine << "Fen is: " << to_fen() << "\nKey is: " << st->key << endl; + cout << brd << "\nFen is: " << to_fen() << "\nKey is: " << st->key << endl; } @@ -382,8 +375,8 @@ template Bitboard Position::hidden_checkers() const; Bitboard Position::attackers_to(Square s, Bitboard occ) const { - return (attacks_from(s, BLACK) & pieces(PAWN, WHITE)) - | (attacks_from(s, WHITE) & pieces(PAWN, BLACK)) + return (attacks_from(s, BLACK) & pieces(WHITE, PAWN)) + | (attacks_from(s, WHITE) & pieces(BLACK, PAWN)) | (attacks_from(s) & pieces(KNIGHT)) | (attacks_bb(s, occ) & pieces(ROOK, QUEEN)) | (attacks_bb(s, occ) & pieces(BISHOP, QUEEN)) @@ -431,8 +424,8 @@ bool Position::move_attacks_square(Move m, Square s) const { return true; // Scan for possible X-ray attackers behind the moved piece - xray = (attacks_bb(s, occ) & pieces(ROOK, QUEEN, color_of(piece))) - |(attacks_bb(s, occ) & pieces(BISHOP, QUEEN, color_of(piece))); + xray = (attacks_bb< ROOK>(s, occ) & pieces(color_of(piece), QUEEN, ROOK)) + | (attacks_bb(s, occ) & pieces(color_of(piece), QUEEN, BISHOP)); // Verify attackers are triggered by our move and not already existing return xray && (xray ^ (xray & attacks_from(s))); @@ -468,8 +461,8 @@ bool Position::pl_move_is_legal(Move m, Bitboard pinned) const { assert(piece_on(capsq) == make_piece(them, PAWN)); assert(piece_on(to) == NO_PIECE); - return !(attacks_bb(ksq, b) & pieces(ROOK, QUEEN, them)) - && !(attacks_bb(ksq, b) & pieces(BISHOP, QUEEN, them)); + return !(attacks_bb< ROOK>(ksq, b) & pieces(them, QUEEN, ROOK)) + && !(attacks_bb(ksq, b) & pieces(them, QUEEN, BISHOP)); } // If the moving piece is a king, check whether the destination @@ -641,7 +634,7 @@ bool Position::move_gives_check(Move m, const CheckInfo& ci) const { if (ci.dcCandidates && (ci.dcCandidates & from)) { // For pawn and king moves we need to verify also direction - if ( (pt != PAWN && pt != KING) + if ( (pt != PAWN && pt != KING) || !squares_aligned(from, to, king_square(~sideToMove))) return true; } @@ -666,8 +659,8 @@ bool Position::move_gives_check(Move m, const CheckInfo& ci) const { Square capsq = make_square(file_of(to), rank_of(from)); Bitboard b = (pieces() ^ from ^ capsq) | to; - return (attacks_bb< ROOK>(ksq, b) & pieces( ROOK, QUEEN, us)) - | (attacks_bb(ksq, b) & pieces(BISHOP, QUEEN, us)); + return (attacks_bb< ROOK>(ksq, b) & pieces(us, QUEEN, ROOK)) + | (attacks_bb(ksq, b) & pieces(us, QUEEN, BISHOP)); } // Castling with check ? @@ -707,14 +700,6 @@ void Position::do_move(Move m, StateInfo& newSt, const CheckInfo& ci, bool moveI // Copy some fields of old state to our new StateInfo object except the ones // which are recalculated from scratch anyway, then switch our state pointer // to point to the new, ready to be updated, state. - struct ReducedStateInfo { - Key pawnKey, materialKey; - Value npMaterial[2]; - int castleRights, rule50, pliesFromNull; - Score psq_score; - Square epSquare; - }; - memcpy(&newSt, st, sizeof(ReducedStateInfo)); newSt.previous = st; @@ -841,7 +826,7 @@ void Position::do_move(Move m, StateInfo& newSt, const CheckInfo& ci, bool moveI { // Set en-passant square, only if moved pawn can be captured if ( (int(to) ^ int(from)) == 16 - && (attacks_from(from + pawn_push(us), us) & pieces(PAWN, them))) + && (attacks_from(from + pawn_push(us), us) & pieces(them, PAWN))) { st->epSquare = Square((from + to) / 2); k ^= zobEp[file_of(st->epSquare)]; @@ -919,10 +904,10 @@ void Position::do_move(Move m, StateInfo& newSt, const CheckInfo& ci, bool moveI if (ci.dcCandidates && (ci.dcCandidates & from)) { if (pt != ROOK) - st->checkersBB |= attacks_from(king_square(them)) & pieces(ROOK, QUEEN, us); + st->checkersBB |= attacks_from(king_square(them)) & pieces(us, QUEEN, ROOK); if (pt != BISHOP) - st->checkersBB |= attacks_from(king_square(them)) & pieces(BISHOP, QUEEN, us); + st->checkersBB |= attacks_from(king_square(them)) & pieces(us, QUEEN, BISHOP); } } } @@ -1651,7 +1636,7 @@ bool Position::pos_is_ok(int* failedStep) const { if ((*step)++, debugPieceCounts) for (Color c = WHITE; c <= BLACK; c++) for (PieceType pt = PAWN; pt <= KING; pt++) - if (pieceCount[c][pt] != popcount(pieces(pt, c))) + if (pieceCount[c][pt] != popcount(pieces(c, pt))) return false; if ((*step)++, debugPieceList)