From dc887ee3c6a9cec95fd46a0270a95b4a862819af Mon Sep 17 00:00:00 2001 From: rektdie Date: Thu, 21 Mar 2024 08:44:49 +0100 Subject: [PATCH] Fixed a bug in attack generation on the fly + slider attack tables init --- source/bitboards.cpp | 6 +- source/main.cpp | 3 +- source/movegen.cpp | 72 +++++++++++++++++---- source/movegen.h | 150 ++++++++++++++++++++++++++++++++++++++++++- 4 files changed, 214 insertions(+), 17 deletions(-) diff --git a/source/bitboards.cpp b/source/bitboards.cpp index 827d061..794e629 100644 --- a/source/bitboards.cpp +++ b/source/bitboards.cpp @@ -61,9 +61,9 @@ int Bitboard::getLS1BIndex() const { Bitboard Bitboard::getOccupancy(int index, Bitboard attackMask) { Bitboard occupancy; - int squareNum = attackMask.popCount(); - - for (int count = 0; count < squareNum; count++) { + int squareCount = attackMask.popCount(); + + for (int count = 0; count < squareCount; count++) { int square = attackMask.getLS1BIndex(); attackMask.PopBit(square); diff --git a/source/main.cpp b/source/main.cpp index 0247e3c..bfc0c49 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -5,7 +5,8 @@ int main() { initLeaperAttacks(); - + initSliderAttacks(); + Board board; board.Init(); diff --git a/source/movegen.cpp b/source/movegen.cpp index 7f1cfff..7099195 100644 --- a/source/movegen.cpp +++ b/source/movegen.cpp @@ -4,6 +4,13 @@ Bitboard pawnAttacks[2][64]; Bitboard knightAttacks[64]; Bitboard kingAttacks[64]; + +Bitboard bishopMasks[64]; +Bitboard rookMasks[64]; + +Bitboard bishopAttacks[64][512]; +Bitboard rookAttacks[64][4096]; + static Bitboard notAfile = ~files[A]; static Bitboard notHfile = ~files[H]; static Bitboard notABfile = notAfile & ~files[B]; @@ -77,11 +84,11 @@ Bitboard maskBishopAttacks(int square) { for (int r = tr + 1, f = tf + 1; r <= 6 && f <= 6; r++, f++) { attacks |= (1ULL << (r * 8 + f)); } - //South West + // South West for (int r = tr - 1, f = tf - 1; r >= 1 && f >= 1; r--, f--) { attacks |= (1ULL << (r * 8 + f)); } - //South East + // South East for (int r = tr - 1, f = tf + 1; r >= 1 && f <= 6; r--, f++) { attacks |= (1ULL << (r * 8 + f)); } @@ -132,26 +139,26 @@ Bitboard bishopAttacksOnTheFly(int square, Bitboard block) { // North West for (int r = tr + 1, f = tf - 1; r <= 7 && f >= 0; r++, f--) { currentSquare = (1ULL << (r * 8 + f)); - if ((currentSquare & block).GetBoard()) break; attacks |= currentSquare; + if ((currentSquare & block).GetBoard()) break; } // North East for (int r = tr + 1, f = tf + 1; r <= 7 && f <= 7; r++, f++) { currentSquare = (1ULL << (r * 8 + f)); - if ((currentSquare & block).GetBoard()) break; attacks |= currentSquare; + if ((currentSquare & block).GetBoard()) break; } //South West for (int r = tr - 1, f = tf - 1; r >= 0 && f >= 0; r--, f--) { currentSquare = (1ULL << (r * 8 + f)); - if ((currentSquare & block).GetBoard()) break; attacks |= currentSquare; + if ((currentSquare & block).GetBoard()) break; } //South East for (int r = tr - 1, f = tf + 1; r >= 0 && f <= 7; r--, f++) { currentSquare = (1ULL << (r * 8 + f)); - if ((currentSquare & block).GetBoard()) break; attacks |= currentSquare; + if ((currentSquare & block).GetBoard()) break; } return attacks; @@ -171,26 +178,26 @@ Bitboard rookAttacksOnTheFly(int square, Bitboard block) { // North for (int r = tr + 1; r <= 7; r++) { currentSquare = (1ULL << (r * 8 + tf)); - if ((currentSquare & block).GetBoard()) break; attacks |= currentSquare; + if ((currentSquare & block).GetBoard()) break; } - //South + // South for (int r = tr - 1; r >= 0; r--) { currentSquare = (1ULL << (r * 8 + tf)); - if ((currentSquare & block).GetBoard()) break; attacks |= currentSquare; + if ((currentSquare & block).GetBoard()) break; } // East for (int f = tf + 1; f <= 7; f++) { currentSquare = (1ULL << (tr * 8 + f)); - if ((currentSquare & block).GetBoard()) break; attacks |= currentSquare; + if ((currentSquare & block).GetBoard()) break; } - //West + // West for (int f = tf - 1; f >= 0; f--) { currentSquare = (1ULL << (tr * 8 + f)); - if ((currentSquare & block).GetBoard()) break; attacks |= currentSquare; + if ((currentSquare & block).GetBoard()) break; } return attacks; @@ -204,4 +211,45 @@ void initLeaperAttacks() { knightAttacks[square] = maskKnightAttacks(square); kingAttacks[square] = maskKingAttacks(square); } +} + +void initSliderAttacks() { + for (int square = 0; square < 64; square++) { + bishopMasks[square] = maskBishopAttacks(square); + rookMasks[square] = maskRookAttacks(square); + + for (int index = 0; index < 512; index++) { + Bitboard occupancy = Bitboard::getOccupancy(index, bishopMasks[square]); + + int magicIndex = (occupancy.GetBoard() * bishopMagicNumbers[square]) + >> (64 - bishopRelevantBits[square]); + + bishopAttacks[square][magicIndex] = bishopAttacksOnTheFly(square, occupancy); + } + + for (int index = 0; index < 4096; index++) { + Bitboard occupancy = Bitboard::getOccupancy(index, rookMasks[square]); + + int magicIndex = (occupancy.GetBoard() * rookMagicNumbers[square]) + >> (64 - rookRelevantBits[square]); + + rookAttacks[square][magicIndex] = rookAttacksOnTheFly(square, occupancy); + } + } +} + +Bitboard getBishopAttack(int square, U64 occupancy) { + occupancy &= bishopMasks[square].GetBoard(); + occupancy *= bishopMagicNumbers[square]; + occupancy >>= (64 - bishopRelevantBits[square]); + + return bishopAttacks[square][occupancy]; +} + +Bitboard getRookAttack(int square, U64 occupancy) { + occupancy &= rookMasks[square].GetBoard(); + occupancy *= rookMagicNumbers[square]; + occupancy >>= (64 - rookRelevantBits[square]); + + return rookAttacks[square][occupancy]; } \ No newline at end of file diff --git a/source/movegen.h b/source/movegen.h index 843f3c0..8d59f7c 100644 --- a/source/movegen.h +++ b/source/movegen.h @@ -25,11 +25,155 @@ constexpr int rookRelevantBits[64] = { 12, 11, 11, 11, 11, 11, 11, 12 }; +constexpr U64 rookMagicNumbers[64] = { + 0x8a80104000800020ULL, + 0x140002000100040ULL, + 0x2801880a0017001ULL, + 0x100081001000420ULL, + 0x200020010080420ULL, + 0x3001c0002010008ULL, + 0x8480008002000100ULL, + 0x2080088004402900ULL, + 0x800098204000ULL, + 0x2024401000200040ULL, + 0x100802000801000ULL, + 0x120800800801000ULL, + 0x208808088000400ULL, + 0x2802200800400ULL, + 0x2200800100020080ULL, + 0x801000060821100ULL, + 0x80044006422000ULL, + 0x100808020004000ULL, + 0x12108a0010204200ULL, + 0x140848010000802ULL, + 0x481828014002800ULL, + 0x8094004002004100ULL, + 0x4010040010010802ULL, + 0x20008806104ULL, + 0x100400080208000ULL, + 0x2040002120081000ULL, + 0x21200680100081ULL, + 0x20100080080080ULL, + 0x2000a00200410ULL, + 0x20080800400ULL, + 0x80088400100102ULL, + 0x80004600042881ULL, + 0x4040008040800020ULL, + 0x440003000200801ULL, + 0x4200011004500ULL, + 0x188020010100100ULL, + 0x14800401802800ULL, + 0x2080040080800200ULL, + 0x124080204001001ULL, + 0x200046502000484ULL, + 0x480400080088020ULL, + 0x1000422010034000ULL, + 0x30200100110040ULL, + 0x100021010009ULL, + 0x2002080100110004ULL, + 0x202008004008002ULL, + 0x20020004010100ULL, + 0x2048440040820001ULL, + 0x101002200408200ULL, + 0x40802000401080ULL, + 0x4008142004410100ULL, + 0x2060820c0120200ULL, + 0x1001004080100ULL, + 0x20c020080040080ULL, + 0x2935610830022400ULL, + 0x44440041009200ULL, + 0x280001040802101ULL, + 0x2100190040002085ULL, + 0x80c0084100102001ULL, + 0x4024081001000421ULL, + 0x20030a0244872ULL, + 0x12001008414402ULL, + 0x2006104900a0804ULL, + 0x1004081002402ULL +}; + +constexpr U64 bishopMagicNumbers[64] = { + 0x40040844404084ULL, + 0x2004208a004208ULL, + 0x10190041080202ULL, + 0x108060845042010ULL, + 0x581104180800210ULL, + 0x2112080446200010ULL, + 0x1080820820060210ULL, + 0x3c0808410220200ULL, + 0x4050404440404ULL, + 0x21001420088ULL, + 0x24d0080801082102ULL, + 0x1020a0a020400ULL, + 0x40308200402ULL, + 0x4011002100800ULL, + 0x401484104104005ULL, + 0x801010402020200ULL, + 0x400210c3880100ULL, + 0x404022024108200ULL, + 0x810018200204102ULL, + 0x4002801a02003ULL, + 0x85040820080400ULL, + 0x810102c808880400ULL, + 0xe900410884800ULL, + 0x8002020480840102ULL, + 0x220200865090201ULL, + 0x2010100a02021202ULL, + 0x152048408022401ULL, + 0x20080002081110ULL, + 0x4001001021004000ULL, + 0x800040400a011002ULL, + 0xe4004081011002ULL, + 0x1c004001012080ULL, + 0x8004200962a00220ULL, + 0x8422100208500202ULL, + 0x2000402200300c08ULL, + 0x8646020080080080ULL, + 0x80020a0200100808ULL, + 0x2010004880111000ULL, + 0x623000a080011400ULL, + 0x42008c0340209202ULL, + 0x209188240001000ULL, + 0x400408a884001800ULL, + 0x110400a6080400ULL, + 0x1840060a44020800ULL, + 0x90080104000041ULL, + 0x201011000808101ULL, + 0x1a2208080504f080ULL, + 0x8012020600211212ULL, + 0x500861011240000ULL, + 0x180806108200800ULL, + 0x4000020e01040044ULL, + 0x300000261044000aULL, + 0x802241102020002ULL, + 0x20906061210001ULL, + 0x5a84841004010310ULL, + 0x4010801011c04ULL, + 0xa010109502200ULL, + 0x4a02012000ULL, + 0x500201010098b028ULL, + 0x8040002811040900ULL, + 0x28000010020204ULL, + 0x6000020202d0240ULL, + 0x8918844842082200ULL, + 0x4010011029020020ULL +}; + // pawn attacks table [side][square] extern Bitboard pawnAttacks[2][64]; + extern Bitboard knightAttacks[64]; extern Bitboard kingAttacks[64]; +extern Bitboard bishopMasks[64]; +extern Bitboard rookMasks[64]; + +// bishop attacks table [square][occupancies] +extern Bitboard bishopAttacks[64][512]; + +// rook attacks table [square][occupancies] +extern Bitboard rookAttacks[64][4096]; + // generate pawn masks Bitboard maskPawnAttacks(int square, int side); @@ -51,4 +195,8 @@ Bitboard maskRookAttacks(int square); //generate rook attacks Bitboard rookAttacksOnTheFly(int square, Bitboard block); -void initLeaperAttacks(); \ No newline at end of file +void initLeaperAttacks(); +void initSliderAttacks(); + +Bitboard getBishopAttack(int square, U64 occupancy); +Bitboard getRookAttack(int square, U64 occupancy); \ No newline at end of file