Skip to content

Commit 561ec27

Browse files
committed
fixed pawn attacks wrapping around A and H files
1 parent 25f6674 commit 561ec27

File tree

2 files changed

+70
-8
lines changed

2 files changed

+70
-8
lines changed

include/bitbishop/lookups/pawn_attacks.hpp

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,19 @@ constexpr std::array<Bitboard, Const::BOARD_SIZE> WHITE_PAWN_ATTACKS = []() cons
9898
for (int sq = 0; sq < BOARD_SIZE; ++sq) {
9999
uint64_t bitboard = 1ULL << sq;
100100
uint64_t attacks = 0;
101-
if (sq / BOARD_WIDTH < RANK_8_IND) { // rank 1..7
102-
attacks |= (bitboard << (BOARD_WIDTH - 1)); // NW
103-
attacks |= (bitboard << (BOARD_WIDTH + 1)); // NE
101+
102+
const int rank = sq / BOARD_WIDTH;
103+
const int file = sq % BOARD_WIDTH;
104+
105+
if (rank < RANK_8_IND) {
106+
if (file > FILE_A_IND) {
107+
attacks |= (bitboard << (BOARD_WIDTH - 1)); // NW
108+
}
109+
if (file < FILE_H_IND) {
110+
attacks |= (bitboard << (BOARD_WIDTH + 1)); // NE
111+
}
104112
}
113+
105114
table[sq] = Bitboard(attacks);
106115
}
107116
return table;
@@ -115,19 +124,28 @@ constexpr std::array<Bitboard, Const::BOARD_SIZE> WHITE_PAWN_ATTACKS = []() cons
115124
*/
116125
constexpr std::array<Bitboard, Const::BOARD_SIZE> BLACK_PAWN_ATTACKS = []() constexpr {
117126
using namespace Const;
118-
using namespace Bitmasks;
119127

120128
std::array<Bitboard, BOARD_SIZE> table{};
121129

122130
for (int sq = 0; sq < BOARD_SIZE; ++sq) {
123-
uint64_t bitboard = 1ULL << sq;
131+
uint64_t bb = 1ULL << sq;
124132
uint64_t attacks = 0;
125-
if (sq / BOARD_WIDTH > RANK_1_IND) { // rank 2..8
126-
attacks |= (bitboard >> (BOARD_WIDTH + 1)); // SW
127-
attacks |= (bitboard >> (BOARD_WIDTH - 1)); // SE
133+
134+
const int rank = sq / BOARD_WIDTH;
135+
const int file = sq % BOARD_WIDTH;
136+
137+
if (rank > RANK_1_IND) {
138+
if (file > FILE_A_IND) {
139+
attacks |= bb >> (BOARD_WIDTH + 1); // SW
140+
}
141+
if (file < FILE_H_IND) {
142+
attacks |= bb >> (BOARD_WIDTH - 1); // SE
143+
}
128144
}
145+
129146
table[sq] = Bitboard(attacks);
130147
}
148+
131149
return table;
132150
}();
133151

tests/bitbishop/lookups/test_pawn_attacks.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,47 @@ TEST(PawnAttackTest, BlackPawnAttacksFromE5) {
7373

7474
EXPECT_EQ(BLACK_PAWN_ATTACKS[Square::E5], expected);
7575
}
76+
77+
/**
78+
* @test White pawn attacks from an H4 edge square
79+
* Expected: No wrapping, single attack
80+
*/
81+
TEST(PawnAttackTest, WhitePawnAttacksFromH4) {
82+
Bitboard expected;
83+
expected.set(Square::G5);
84+
85+
EXPECT_EQ(WHITE_PAWN_ATTACKS[Square::H4], expected);
86+
}
87+
88+
/**
89+
* @test Black pawn attacks from H4 edge square
90+
* Expected: No wrapping, single attack
91+
*/
92+
TEST(PawnAttackTest, BlackPawnAttacksFromH4) {
93+
Bitboard expected;
94+
expected.set(Square::G3);
95+
96+
EXPECT_EQ(BLACK_PAWN_ATTACKS[Square::H4], expected);
97+
}
98+
99+
/**
100+
* @test White pawn attacks from an A5 edge square
101+
* Expected: No wrapping, single attack
102+
*/
103+
TEST(PawnAttackTest, WhitePawnAttacksFromA5) {
104+
Bitboard expected;
105+
expected.set(Square::B6);
106+
107+
EXPECT_EQ(WHITE_PAWN_ATTACKS[Square::A5], expected);
108+
}
109+
110+
/**
111+
* @test Black pawn attacks from A5 edge square
112+
* Expected: No wrapping, single attack
113+
*/
114+
TEST(PawnAttackTest, BlackPawnAttacksFromA5) {
115+
Bitboard expected;
116+
expected.set(Square::B4);
117+
118+
EXPECT_EQ(BLACK_PAWN_ATTACKS[Square::A5], expected);
119+
}

0 commit comments

Comments
 (0)