Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ```cpp
- template<Color color>
- bool static_exchange_eval(Position& board, Move move, const int threshold) {
- if (move.is_promotion()) return true;
- Square to = move.to();
- Square from = move.from();
- PieceType piece_captured = type_of(board.piece_at(to));
- PieceType piece_capturing = type_of(board.piece_at(from));
- // If we make the capture and don't loose our piece, we should beat the threshold.
- // If we don't it's likely a bad exchange.
- int value = ORDERING_PIECE_VALUES[piece_captured] - threshold;
- if (value < 0) return false;
- // If we loose our piece and are still positive, this is a good exchange.
- value -= ORDERING_PIECE_VALUES[piece_capturing];
- if (value >= 0) return true;
- // We already know the piece on 'from' is an attacker. Ignore it. The piece on to will be captured. Ignore it.
- Bitboard occupied = board.occupancy() ^ square_to_bitboard(from) ^ square_to_bitboard(to);
- // This will be updated to only contain pieces that only have pieces we are concerned with.
- Bitboard attackers = board.attackers_of(to, occupied);
- Bitboard bishops = board.occupancy<BISHOP>() | board.occupancy<QUEEN>();
- Bitboard rooks = board.occupancy<ROOK>() | board.occupancy<QUEEN>();
- Color side_to_play = ~color;
- while (true) {
- attackers &= occupied;
- Bitboard our_attackers = attackers & board.occupancy(side_to_play);
- if (!our_attackers) break; // If no remaining attackers for us, break.
- u32 ipt;
- for (ipt = PAWN; ipt < KING; ipt++) { // pt == king when we break if all others didn't cause break.
- if (our_attackers & board.occupancy(side_to_play, PieceType(ipt))) break;
- }
- auto piece_type = PieceType(ipt);
- side_to_play = ~side_to_play;
- value = -value - 1 - ORDERING_PIECE_VALUES[piece_type];
- if (value >= 0) {
- if (piece_type == KING && (attackers & board.occupancy(side_to_play))) side_to_play = ~side_to_play;
- break;
- }
- occupied ^= square_to_bitboard(lsb(our_attackers & board.occupancy(piece_type)));
- // Add discovered attacks.
- if (piece_type == PAWN || piece_type == BISHOP || piece_type == QUEEN)
- attackers |= tables::attacks<BISHOP>(to, occupied) & bishops;
- if (piece_type == ROOK || piece_type == QUEEN)
- attackers |= tables::attacks<ROOK>(to, occupied) & rooks;
- }
- return side_to_play != color_of(board.piece_at(from));
- }
- ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement