Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: attacks_from[] and attacks_to[] info

Author: Russell Reagan

Date: 12:51:16 10/22/02

Go up one level in this thread


On October 22, 2002 at 03:26:37, Steve Maughan wrote:

>Interesting.  I use bitboards in Monarch but havew trouble getting the attack_to
>type info without table lookups.  I can see how you could do it for knights,
>pawns and kings but how do woudl you do it for a bishop?

My attacks.h file...

#ifndef ATTACKS_H
#define ATTACKS_H

#include "chess.h"

// ---------------------------------------------------------------------------
// A word about the attack generation functions...
//
// In each of the attack generation functions, b is the piece bitboard for the
// piece we are moving (ex. whiteRooks, whiteBishops, etc.). c is a bitboard
// describing the available squares on the board (ex. ~allPieces).
// ---------------------------------------------------------------------------

// -------------
// Attack macros
// -------------

#define whiteAttacks            (whitePawnAttacks | whiteKnightAttacks | whiteKi
ngAttacks | whiteRankFileAttacks | whiteDiagonalAttacks)
#define whitePawnAttacks        (WhitePawnAttacks(whitePawns))
#define whiteKnightAttacks      (KnightAttacks(whiteKnights))
#define whiteBishopAttacks      (DiagonalAttacks(whiteBishops, ~allPieces))
#define whiteRookAttacks        (RankFileAttacks(whiteRooks, ~allPieces))
#define whiteQueenAttacks       (RankFileAttacks(whiteQueens, ~allPieces) | Diag
onalAttacks(whiteQueens, ~allPieces))
#define whiteKingAttacks        (KingAttacks(whiteKing))
#define whiteRankFileAttacks    (RankFileAttacks(whiteRooks|whiteQueens, ~allPie
ces))
#define whiteDiagonalAttacks    (DiagonalAttacks(whiteBishops|whiteQueens, ~allP
ieces))

#define blackAttacks            (blackPawnAttacks | blackKnightAttacks | blackKi
ngAttacks | blackRankFileAttacks | blackDiagonalAttacks)
#define blackPawnAttacks        (WhitePawnAttacks(blackPawns))
#define blackKnightAttacks      (KnightAttacks(blackKnights))
#define blackBishopAttacks      (DiagonalAttacks(blackBishops, ~allPieces))
#define blackRookAttacks        (RankFileAttacks(blackRooks, ~allPieces))
#define blackQueenAttacks       (RankFileAttacks(blackQueens, ~allPieces) | Diag
onalAttacks(blackQueens, ~allPieces))
#define blackKingAttacks        (KingAttacks(blackKing))
#define blackRankFileAttacks    (RankFileAttacks(blackRooks|blackQueens, ~allPie
ces))
#define blackDiagonalAttacks    (DiagonalAttacks(blackBishops|blackQueens, ~allP
ieces))

// ------------
// Pawn Attacks
// ------------

bitboard WhitePawnAttacks (bitboard b)
{
        bitboard c = b;
        b ^= c;
        b |= (c << 7) & 0x7F7F7F7F7F7F7F7F;
        b |= (c << 9) & 0xFEFEFEFEFEFEFEFE;
        return b;
}

bitboard BlackPawnAttacks (bitboard b)
{
        bitboard c = b;
        b ^= c;
        b |= (c >> 7) & 0xFEFEFEFEFEFEFEFE;
        b |= (c >> 9) & 0x7F7F7F7F7F7F7F7F;
        return b;;
}

bitboard WhitePawnNonAttacks (bitboard b, bitboard c)
{
        b |= (b << 8) & c;
        c &= (c << 8);
        return b |= ((b & 0x0000000000FF0000) << 16) & c;
}

bitboard BlackPawnNonAttacks (bitboard b, bitboard c)
{
        b |= (b >> 8) & c;
        c &= (c >> 8);
        return b |= ((b & 0x0000FF0000000000) >> 16) & c;
}

// ------------
// King Attacks
// ------------

bitboard KingAttacks (bitboard b)
{
        bitboard c = b;
        b |= (b >> 1) & 0x7F7F7F7F7F7F7F7F;
        b |= (b << 1) & 0xFEFEFEFEFEFEFEFE;
        b |=  b << 8;
        b |=  b >> 8;
        return b ^ c;
}

// --------------
// Knight Attacks
// --------------

bitboard KnightAttacks (bitboard b)
{
        bitboard c = b;
        b ^= c;
        b |= (c >> 17) & 0x7F7F7F7F7F7F7F7F;
        b |= (c << 15) & 0x7F7F7F7F7F7F7F7F;
        b |= (c >> 15) & 0xFEFEFEFEFEFEFEFE;
        b |= (c << 17) & 0xFEFEFEFEFEFEFEFE;
        b |= (c >> 10) & 0x3F3F3F3F3F3F3F3F;
        b |= (c <<  6) & 0x3F3F3F3F3F3F3F3F;
        b |= (c >>  6) & 0xFCFCFCFCFCFCFCFC;
        b |= (c << 10) & 0xFCFCFCFCFCFCFCFC;
        return b;
}

// ---------------------
// Rank and file attacks
// ---------------------

bitboard UpAttacks (bitboard b, bitboard c)
{
        b |= (b <<  8) & c;
        c &= (c <<  8);
        b |= (b << 16) & c;
        c &= (c << 16);
        b |= (b << 32) & c;
        return b << 8;
}

bitboard DownAttacks (bitboard b, bitboard c)
{
        b |= (b >>  8) & c;
        c &= (c >>  8);
        b |= (b >> 16) & c;
        c &= (c >> 16);
        b |= (b >> 32) & c;
        return b >> 8;
}

bitboard LeftAttacks (bitboard b, bitboard c)
{
        c &= 0x7F7F7F7F7F7F7F7F;
        b |= (b >> 1) & c;
        c &= (c >> 1);
        b |= (b >> 2) & c;
        c &= (c >> 2);
        b |= (b >> 4) & c;
        c &= (c >> 4);
        return (b >> 1) & 0x7F7F7F7F7F7F7F7F;
}

bitboard RightAttacks (bitboard b, bitboard c)
{
        c &= 0xFEFEFEFEFEFEFEFE;
        b |= (b << 1) & c;
        c &= (c << 1);
        b |= (b << 2) & c;
        c &= (c << 2);
        b |= (b << 4) & c;
        c &= (c << 4);
        return (b << 1) & 0xFEFEFEFEFEFEFEFE;
}

bitboard RankFileAttacks (bitboard b, bitboard c)
{
        return  UpAttacks(b,c)   |
                DownAttacks(b,c) |
                LeftAttacks(b,c) |
                RightAttacks(b,c);
}

// ----------------
// Diagonal attacks
// ----------------

bitboard DownLeftAttacks (bitboard b, bitboard c)
{
        c &= 0x7F7F7F7F7F7F7F7F;
        b |= (b >>  9) & c;
        c &= (c >>  9);
        b |= (b >> 18) & c;
        c &= (c >> 18);
        b |= (b >> 27) & c;
        c &= (c >> 27);
        return (b >> 9) & 0x7F7F7F7F7F7F7F7F;
}

bitboard UpLeftAttacks (bitboard b, bitboard c)
{
        c &= 0x7F7F7F7F7F7F7F7F;
        b |= (b <<  7) & c;
        c &= (c <<  7);
        b |= (b << 14) & c;
        c &= (c << 14);
        b |= (b << 21) & c;
        c &= (c << 21);
        return (b << 7) & 0x7F7F7F7F7F7F7F7F;
}

bitboard DownRightAttacks (bitboard b, bitboard c)
{
        c &= 0xFEFEFEFEFEFEFEFE;
        b |= (b >>  7) & c;
        c &= (c >>  7);
        b |= (b >> 14) & c;
        c &= (c >> 14);
        b |= (b >> 21) & c;
        c &= (c >> 21);
        return (b >> 7) & 0xFEFEFEFEFEFEFEFE;
}

bitboard UpRightAttacks (bitboard b, bitboard c)
{
        c &= 0xFEFEFEFEFEFEFEFE;
        b |= (b <<  9) & c;
        c &= (c <<  9);
        b |= (b << 18) & c;

        c &= (c << 18);
        b |= (b << 27) & c;
        c &= (c << 27);
        return (b << 9) & 0xFEFEFEFEFEFEFEFE;
}

bitboard DiagonalAttacks (bitboard b, bitboard c)
{
        return  DownLeftAttacks(b,c) |
                UpLeftAttacks(b,c)   |
                DownRightAttacks(b,c)|
                UpRightAttacks(b,c);
}

#endif

I don't know if these are optimal, but they work correctly for me. I have to
thank Steffan Westcott for his explainations and his examples, and for answering
all of my questions. Anyway, hope this helps. If you find any bugs or ways to
make these more efficient let me know.

Russell



This page took 0.06 seconds to execute

Last modified: Thu, 07 Jul 11 08:48:38 -0700

Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.