X-Git-Url: https://git.sesse.net/?p=stockfish;a=blobdiff_plain;f=src%2Fposition.cpp;h=99ff223bd4231cdecd64e659341c9683bc4a7d7c;hp=7b39be155a3b13d7ed5bcffc1dd7c959cc8bb7eb;hb=0d194377033c52ff41ff990ddf31950b576ff543;hpb=849809e97ec6fadd4e31353f473473e196323b5b diff --git a/src/position.cpp b/src/position.cpp index 7b39be15..99ff223b 100644 --- a/src/position.cpp +++ b/src/position.cpp @@ -308,7 +308,7 @@ Bitboard Position::pinned_pieces(Color c) const { sliders = rooks_and_queens(them) & ~checkers(); if(sliders & RookPseudoAttacks[ksq]) { - b2 = rook_attacks(ksq) & pieces_of_color(c); + b2 = piece_attacks(ksq) & pieces_of_color(c); pinners = rook_attacks_bb(ksq, b1 ^ b2) & sliders; while(pinners) { s = pop_1st_bit(&pinners); @@ -318,7 +318,7 @@ Bitboard Position::pinned_pieces(Color c) const { sliders = bishops_and_queens(them) & ~checkers(); if(sliders & BishopPseudoAttacks[ksq]) { - b2 = bishop_attacks(ksq) & pieces_of_color(c); + b2 = piece_attacks(ksq) & pieces_of_color(c); pinners = bishop_attacks_bb(ksq, b1 ^ b2) & sliders; while(pinners) { s = pop_1st_bit(&pinners); @@ -343,7 +343,7 @@ Bitboard Position::discovered_check_candidates(Color c) const { sliders = rooks_and_queens(c); if(sliders & RookPseudoAttacks[ksq]) { - b2 = rook_attacks(ksq) & pieces_of_color(c); + b2 = piece_attacks(ksq) & pieces_of_color(c); checkers = rook_attacks_bb(ksq, b1 ^ b2) & sliders; while(checkers) { s = pop_1st_bit(&checkers); @@ -353,7 +353,7 @@ Bitboard Position::discovered_check_candidates(Color c) const { sliders = bishops_and_queens(c); if(sliders & BishopPseudoAttacks[ksq]) { - b2 = bishop_attacks(ksq) & pieces_of_color(c); + b2 = piece_attacks(ksq) & pieces_of_color(c); checkers = bishop_attacks_bb(ksq, b1 ^ b2) & sliders; while(checkers) { s = pop_1st_bit(&checkers); @@ -371,10 +371,10 @@ Bitboard Position::discovered_check_candidates(Color c) const { bool Position::square_is_attacked(Square s, Color c) const { return (pawn_attacks(opposite_color(c), s) & pawns(c)) || - (knight_attacks(s) & knights(c)) || - (king_attacks(s) & kings(c)) || - (rook_attacks(s) & rooks_and_queens(c)) || - (bishop_attacks(s) & bishops_and_queens(c)); + (piece_attacks(s) & knights(c)) || + (piece_attacks(s) & kings(c)) || + (piece_attacks(s) & rooks_and_queens(c)) || + (piece_attacks(s) & bishops_and_queens(c)); } @@ -385,12 +385,12 @@ bool Position::square_is_attacked(Square s, Color c) const { Bitboard Position::attacks_to(Square s) const { return - (black_pawn_attacks(s) & pawns(WHITE)) | - (white_pawn_attacks(s) & pawns(BLACK)) | - (knight_attacks(s) & pieces_of_type(KNIGHT)) | - (rook_attacks(s) & rooks_and_queens()) | - (bishop_attacks(s) & bishops_and_queens()) | - (king_attacks(s) & pieces_of_type(KING)); + (pawn_attacks(BLACK, s) & pawns(WHITE)) | + (pawn_attacks(WHITE, s) & pawns(BLACK)) | + (piece_attacks(s) & pieces_of_type(KNIGHT)) | + (piece_attacks(s) & rooks_and_queens()) | + (piece_attacks(s) & bishops_and_queens()) | + (piece_attacks(s) & pieces_of_type(KING)); } Bitboard Position::attacks_to(Square s, Color c) const { @@ -406,13 +406,13 @@ bool Position::piece_attacks_square(Square f, Square t) const { assert(square_is_ok(t)); switch(piece_on(f)) { - case WP: return white_pawn_attacks_square(f, t); - case BP: return black_pawn_attacks_square(f, t); - case WN: case BN: return knight_attacks_square(f, t); - case WB: case BB: return bishop_attacks_square(f, t); - case WR: case BR: return rook_attacks_square(f, t); - case WQ: case BQ: return queen_attacks_square(f, t); - case WK: case BK: return king_attacks_square(f, t); + case WP: return pawn_attacks_square(WHITE, f, t); + case BP: return pawn_attacks_square(BLACK, f, t); + case WN: case BN: return piece_attacks_square(f, t); + case WB: case BB: return piece_attacks_square(f, t); + case WR: case BR: return piece_attacks_square(f, t); + case WQ: case BQ: return piece_attacks_square(f, t); + case WK: case BK: return piece_attacks_square(f, t); default: return false; } @@ -549,7 +549,7 @@ bool Position::move_is_check(Move m, Bitboard dcCandidates) const { switch(move_promotion(m)) { case KNIGHT: - return knight_attacks_square(to, ksq); + return piece_attacks_square(to, ksq); case BISHOP: return bit_is_set(bishop_attacks_bb(to, b), ksq); case ROOK: @@ -581,7 +581,7 @@ bool Position::move_is_check(Move m, Bitboard dcCandidates) const { return true; // Normal check? else - return bit_is_set(knight_attacks(ksq), to); + return bit_is_set(piece_attacks(ksq), to); case BISHOP: // Discovered check? @@ -589,7 +589,7 @@ bool Position::move_is_check(Move m, Bitboard dcCandidates) const { return true; // Normal check? else - return bit_is_set(bishop_attacks(ksq), to); + return bit_is_set(piece_attacks(ksq), to); case ROOK: // Discovered check? @@ -597,13 +597,13 @@ bool Position::move_is_check(Move m, Bitboard dcCandidates) const { return true; // Normal check? else - return bit_is_set(rook_attacks(ksq), to); + return bit_is_set(piece_attacks(ksq), to); case QUEEN: // Discovered checks are impossible! assert(!bit_is_set(dcCandidates, from)); // Normal check? - return bit_is_set(queen_attacks(ksq), to); + return bit_is_set(piece_attacks(ksq), to); case KING: // Discovered check? @@ -668,13 +668,13 @@ bool Position::move_attacks_square(Move m, Square s) const { assert(square_is_occupied(f)); switch(piece_on(f)) { - case WP: return white_pawn_attacks_square(t, s); - case BP: return black_pawn_attacks_square(t, s); - case WN: case BN: return knight_attacks_square(t, s); - case WB: case BB: return bishop_attacks_square(t, s); - case WR: case BR: return rook_attacks_square(t, s); - case WQ: case BQ: return queen_attacks_square(t, s); - case WK: case BK: return king_attacks_square(t, s); + case WP: return pawn_attacks_square(WHITE, t, s); + case BP: return pawn_attacks_square(BLACK, t, s); + case WN: case BN: return piece_attacks_square(t, s); + case WB: case BB: return piece_attacks_square(t, s); + case WR: case BR: return piece_attacks_square(t, s); + case WQ: case BQ: return piece_attacks_square(t, s); + case WK: case BK: return piece_attacks_square(t, s); default: assert(false); } @@ -847,9 +847,9 @@ void Position::do_move(Move m, UndoInfo &u, Bitboard dcCandidates) { } if(piece == PAWN) { if(abs(int(to) - int(from)) == 16) { - if((us == WHITE && (white_pawn_attacks(from + DELTA_N) & + if((us == WHITE && (pawn_attacks(WHITE, from + DELTA_N) & pawns(BLACK))) || - (us == BLACK && (black_pawn_attacks(from + DELTA_S) & + (us == BLACK && (pawn_attacks(BLACK, from + DELTA_S) & pawns(WHITE)))) { epSquare = Square((int(from) + int(to)) / 2); key ^= zobEp[epSquare]; @@ -882,45 +882,45 @@ void Position::do_move(Move m, UndoInfo &u, Bitboard dcCandidates) { set_bit(&checkersBB, to); if(bit_is_set(dcCandidates, from)) checkersBB |= - ((rook_attacks(ksq) & rooks_and_queens(us)) | - (bishop_attacks(ksq) & bishops_and_queens(us))); + ((piece_attacks(ksq) & rooks_and_queens(us)) | + (piece_attacks(ksq) & bishops_and_queens(us))); break; case KNIGHT: - if(bit_is_set(knight_attacks(ksq), to)) + if(bit_is_set(piece_attacks(ksq), to)) set_bit(&checkersBB, to); if(bit_is_set(dcCandidates, from)) checkersBB |= - ((rook_attacks(ksq) & rooks_and_queens(us)) | - (bishop_attacks(ksq) & bishops_and_queens(us))); + ((piece_attacks(ksq) & rooks_and_queens(us)) | + (piece_attacks(ksq) & bishops_and_queens(us))); break; case BISHOP: - if(bit_is_set(bishop_attacks(ksq), to)) + if(bit_is_set(piece_attacks(ksq), to)) set_bit(&checkersBB, to); if(bit_is_set(dcCandidates, from)) checkersBB |= - (rook_attacks(ksq) & rooks_and_queens(us)); + (piece_attacks(ksq) & rooks_and_queens(us)); break; case ROOK: - if(bit_is_set(rook_attacks(ksq), to)) + if(bit_is_set(piece_attacks(ksq), to)) set_bit(&checkersBB, to); if(bit_is_set(dcCandidates, from)) checkersBB |= - (bishop_attacks(ksq) & bishops_and_queens(us)); + (piece_attacks(ksq) & bishops_and_queens(us)); break; case QUEEN: - if(bit_is_set(queen_attacks(ksq), to)) + if(bit_is_set(piece_attacks(ksq), to)) set_bit(&checkersBB, to); break; case KING: if(bit_is_set(dcCandidates, from)) checkersBB |= - ((rook_attacks(ksq) & rooks_and_queens(us)) | - (bishop_attacks(ksq) & bishops_and_queens(us))); + ((piece_attacks(ksq) & rooks_and_queens(us)) | + (piece_attacks(ksq) & bishops_and_queens(us))); break; default: @@ -1630,10 +1630,10 @@ int Position::see(Square from, Square to) const { attackers = (rook_attacks_bb(to, occ) & rooks_and_queens()) | (bishop_attacks_bb(to, occ) & bishops_and_queens()) | - (knight_attacks(to) & knights()) | - (king_attacks(to) & kings()) | - (white_pawn_attacks(to) & pawns(BLACK)) | - (black_pawn_attacks(to) & pawns(WHITE)); + (piece_attacks(to) & knights()) | + (piece_attacks(to) & kings()) | + (pawn_attacks(WHITE, to) & pawns(BLACK)) | + (pawn_attacks(BLACK, to) & pawns(WHITE)); attackers &= occ; // If the opponent has no attackers, we are finished: @@ -2109,7 +2109,7 @@ void Position::flipped_copy(const Position &pos) { /// Position::is_ok() performs some consitency checks for the position object. /// This is meant to be helpful when debugging. -bool Position::is_ok() const { +bool Position::is_ok(int* failedStep) const { // What features of the position should be verified? static const bool debugBitboards = false; @@ -2124,23 +2124,30 @@ bool Position::is_ok() const { static const bool debugPieceCounts = false; static const bool debugPieceList = false; + if (failedStep) *failedStep = 1; + // Side to move OK? if(!color_is_ok(side_to_move())) return false; // Are the king squares in the position correct? + if (failedStep) (*failedStep)++; if(piece_on(king_square(WHITE)) != WK) return false; + + if (failedStep) (*failedStep)++; if(piece_on(king_square(BLACK)) != BK) return false; // Castle files OK? + if (failedStep) (*failedStep)++; if(!file_is_ok(initialKRFile)) return false; if(!file_is_ok(initialQRFile)) return false; // Do both sides have exactly one king? + if (failedStep) (*failedStep)++; if(debugKingCount) { int kingCount[2] = {0, 0}; for(Square s = SQ_A1; s <= SQ_H8; s++) @@ -2151,6 +2158,7 @@ bool Position::is_ok() const { } // Can the side to move capture the opponent's king? + if (failedStep) (*failedStep)++; if(debugKingCapture) { Color us = side_to_move(); Color them = opposite_color(us); @@ -2160,10 +2168,12 @@ bool Position::is_ok() const { } // Is there more than 2 checkers? + if (failedStep) (*failedStep)++; if(debugCheckerCount && count_1s(checkersBB) > 2) return false; // Bitboards OK? + if (failedStep) (*failedStep)++; if(debugBitboards) { // The intersection of the white and black pieces must be empty: if((pieces_of_color(WHITE) & pieces_of_color(BLACK)) @@ -2184,6 +2194,7 @@ bool Position::is_ok() const { } // En passant square OK? + if (failedStep) (*failedStep)++; if(ep_square() != SQ_NONE) { // The en passant square must be on rank 6, from the point of view of the // side to move. @@ -2192,18 +2203,22 @@ bool Position::is_ok() const { } // Hash key OK? + if (failedStep) (*failedStep)++; if(debugKey && key != compute_key()) return false; // Pawn hash key OK? + if (failedStep) (*failedStep)++; if(debugPawnKey && pawnKey != compute_pawn_key()) return false; // Material hash key OK? + if (failedStep) (*failedStep)++; if(debugMaterialKey && materialKey != compute_material_key()) return false; // Incremental eval OK? + if (failedStep) (*failedStep)++; if(debugIncrementalEval) { if(mgValue != compute_mg_value()) return false; @@ -2212,6 +2227,7 @@ bool Position::is_ok() const { } // Non-pawn material OK? + if (failedStep) (*failedStep)++; if(debugNonPawnMaterial) { if(npMaterial[WHITE] != compute_non_pawn_material(WHITE)) return false; @@ -2220,12 +2236,14 @@ bool Position::is_ok() const { } // Piece counts OK? + if (failedStep) (*failedStep)++; if(debugPieceCounts) for(Color c = WHITE; c <= BLACK; c++) for(PieceType pt = PAWN; pt <= KING; pt++) if(pieceCount[c][pt] != count_1s(pieces_of_color_and_type(c, pt))) return false; + if (failedStep) (*failedStep)++; if(debugPieceList) { for(Color c = WHITE; c <= BLACK; c++) for(PieceType pt = PAWN; pt <= KING; pt++) @@ -2237,6 +2255,6 @@ bool Position::is_ok() const { return false; } } - + if (failedStep) *failedStep = 0; return true; }