From aaad48464bc8a269634de371238826d09a6e240d Mon Sep 17 00:00:00 2001 From: Marco Costalba Date: Tue, 9 Dec 2008 11:20:47 +0100 Subject: [PATCH] Add a see() function that take only destination square In this case firstlocates the least valuable attacker, if any, then proceed as usual. This will be used by next patch. Signed-off-by: Marco Costalba --- src/bitboard.cpp | 6 +++-- src/bitboard.h | 4 +-- src/position.cpp | 63 +++++++++++++++++++++++++++++++++++------------- src/position.h | 1 + 4 files changed, 53 insertions(+), 21 deletions(-) diff --git a/src/bitboard.cpp b/src/bitboard.cpp index e0cb1c20..89b54321 100644 --- a/src/bitboard.cpp +++ b/src/bitboard.cpp @@ -250,8 +250,8 @@ Bitboard BMask[64]; int BAttackIndex[64]; Bitboard BAttacks[0x1480]; -Bitboard SetMaskBB[64]; -Bitboard ClearMaskBB[64]; +Bitboard SetMaskBB[65]; +Bitboard ClearMaskBB[65]; Bitboard StepAttackBB[16][64]; Bitboard RayBB[64][8]; @@ -433,6 +433,8 @@ namespace { // be necessary to touch any of them. void init_masks() { + SetMaskBB[SQ_NONE] = 0ULL; + ClearMaskBB[SQ_NONE] = ~SetMaskBB[SQ_NONE]; for(Square s = SQ_A1; s <= SQ_H8; s++) { SetMaskBB[s] = (1ULL << s); ClearMaskBB[s] = ~SetMaskBB[s]; diff --git a/src/bitboard.h b/src/bitboard.h index 1222ddfa..b193786c 100644 --- a/src/bitboard.h +++ b/src/bitboard.h @@ -113,8 +113,8 @@ extern const Bitboard RankBB[8]; extern const Bitboard RelativeRankBB[2][8]; extern const Bitboard InFrontBB[2][8]; -extern Bitboard SetMaskBB[64]; -extern Bitboard ClearMaskBB[64]; +extern Bitboard SetMaskBB[65]; +extern Bitboard ClearMaskBB[65]; extern Bitboard StepAttackBB[16][64]; extern Bitboard RayBB[64][8]; diff --git a/src/position.cpp b/src/position.cpp index 98e79df2..c24d5675 100644 --- a/src/position.cpp +++ b/src/position.cpp @@ -1582,10 +1582,16 @@ void Position::undo_null_move(const UndoInfo &u) { /// Position::see() is a static exchange evaluator: It tries to estimate the -/// material gain or loss resulting from a move. There are two versions of -/// this function: One which takes a move as input, and one which takes a -/// 'from' and a 'to' square. The function does not yet understand promotions -/// or en passant captures. +/// material gain or loss resulting from a move. There are three versions of +/// this function: One which takes a destination square as input, one takes a +/// move, and one which takes a 'from' and a 'to' square. The function does +/// not yet understand promotions or en passant captures. + +int Position::see(Square to) const { + + assert(square_is_ok(to)); + return see(SQ_NONE, to); +} int Position::see(Move m) const { @@ -1595,18 +1601,22 @@ int Position::see(Move m) const { int Position::see(Square from, Square to) const { - // Approximate material values, with pawn = 1 + // Material values static const int seeValues[18] = { - 0, 1, 3, 3, 5, 10, 100, 0, 0, 1, 3, 3, 5, 10, 100, 0, 0, 0 + 0, PawnValueMidgame, KnightValueMidgame, BishopValueMidgame, + RookValueMidgame, QueenValueMidgame, QueenValueMidgame*10, 0, + 0, PawnValueMidgame, KnightValueMidgame, BishopValueMidgame, + RookValueMidgame, QueenValueMidgame, QueenValueMidgame*10, 0, + 0, 0 }; Bitboard attackers, occ, b; - assert(square_is_ok(from)); + assert(square_is_ok(from) || from == SQ_NONE); assert(square_is_ok(to)); // Initialize colors - Color us = color_of_piece_on(from); + Color us = (from != SQ_NONE ? color_of_piece_on(from) : opposite_color(color_of_piece_on(to))); Color them = opposite_color(us); // Initialize pieces @@ -1616,15 +1626,34 @@ int Position::see(Square from, Square to) const { // Find all attackers to the destination square, with the moving piece // removed, but possibly an X-ray attacker added behind it. occ = occupied_squares(); - clear_bit(&occ, from); - attackers = (rook_attacks_bb(to, occ) & rooks_and_queens()) - | (bishop_attacks_bb(to, occ) & bishops_and_queens()) - | (piece_attacks(to) & knights()) - | (piece_attacks(to) & kings()) - | (pawn_attacks(WHITE, to) & pawns(BLACK)) - | (pawn_attacks(BLACK, to) & pawns(WHITE)); - - // If the opponent has no attackers, we are finished + while (true) + { + clear_bit(&occ, from); + attackers = (rook_attacks_bb(to, occ) & rooks_and_queens()) + | (bishop_attacks_bb(to, occ) & bishops_and_queens()) + | (piece_attacks(to) & knights()) + | (piece_attacks(to) & kings()) + | (pawn_attacks(WHITE, to) & pawns(BLACK)) + | (pawn_attacks(BLACK, to) & pawns(WHITE)); + + if (from != SQ_NONE) + break; + + // If we don't have any attacker we are finished + if ((attackers & pieces_of_color(us)) == EmptyBoardBB) + return 0; + + // Locate the least valuable attacker to the destination square + // and use it to initialize from square. + PieceType pt; + for (pt = PAWN; !(attackers & pieces_of_color_and_type(us, pt)); pt++) + assert(pt < KING); + + from = first_1(attackers & pieces_of_color_and_type(us, pt)); + piece = piece_on(from); + } + + // If the opponent has no attackers we are finished if ((attackers & pieces_of_color(them)) == EmptyBoardBB) return seeValues[capture]; diff --git a/src/position.h b/src/position.h index 743acf33..20c56ad3 100644 --- a/src/position.h +++ b/src/position.h @@ -253,6 +253,7 @@ public: // Static exchange evaluation int see(Square from, Square to) const; int see(Move m) const; + int see(Square to) const; // Accessing hash keys Key get_key() const; -- 2.39.2