Fixed a bug in attack generation on the fly + slider attack tables init
This commit is contained in:
1 parent
67f68c7598
commit
dc887ee3c6
4 files changed
+214
-17
No files matched your search
+60
-12
@@ -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];
|
||||
}
|
||||
Reference in new issue
Block a user