Skip to content

Commit 7f62320

Browse files
protonspringsnicolet
authored andcommitted
Rewrite initialization of PseudoMoves
This is a non-functional code style change. I believe master is a bit convoluted here and propose this version for clarity. No functional change
1 parent 384bff4 commit 7f62320

File tree

1 file changed

+23
-19
lines changed

1 file changed

+23
-19
lines changed

src/bitboard.cpp

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -76,25 +76,29 @@ void Bitboards::init() {
7676

7777
for (Square s1 = SQ_A1; s1 <= SQ_H8; ++s1)
7878
for (Square s2 = SQ_A1; s2 <= SQ_H8; ++s2)
79-
SquareDistance[s1][s2] = std::max(distance<File>(s1, s2), distance<Rank>(s1, s2));
80-
81-
int steps[][5] = { {}, { 7, 9 }, { 6, 10, 15, 17 }, {}, {}, {}, { 1, 7, 8, 9 } };
82-
83-
for (Color c : { WHITE, BLACK })
84-
for (PieceType pt : { PAWN, KNIGHT, KING })
85-
for (Square s = SQ_A1; s <= SQ_H8; ++s)
86-
for (int i = 0; steps[pt][i]; ++i)
87-
{
88-
Square to = s + Direction(c == WHITE ? steps[pt][i] : -steps[pt][i]);
89-
90-
if (is_ok(to) && distance(s, to) < 3)
91-
{
92-
if (pt == PAWN)
93-
PawnAttacks[c][s] |= to;
94-
else
95-
PseudoAttacks[pt][s] |= to;
96-
}
97-
}
79+
SquareDistance[s1][s2] = std::max(distance<File>(s1, s2), distance<Rank>(s1, s2));
80+
81+
for (Square s = SQ_A1; s <= SQ_H8; ++s)
82+
{
83+
PawnAttacks[WHITE][s] = pawn_attacks_bb<WHITE>(square_bb(s));
84+
PawnAttacks[BLACK][s] = pawn_attacks_bb<BLACK>(square_bb(s));
85+
}
86+
87+
// Helper returning the target bitboard of a step from a square
88+
auto landing_square_bb = [&](Square s, int step)
89+
{
90+
Square to = Square(s + step);
91+
return is_ok(to) && distance(s, to) <= 2 ? square_bb(to) : Bitboard(0);
92+
};
93+
94+
for (Square s = SQ_A1; s <= SQ_H8; ++s)
95+
{
96+
for (int step : {-9, -8, -7, -1, 1, 7, 8, 9} )
97+
PseudoAttacks[KING][s] |= landing_square_bb(s, step);
98+
99+
for (int step : {-17, -15, -10, -6, 6, 10, 15, 17} )
100+
PseudoAttacks[KNIGHT][s] |= landing_square_bb(s, step);
101+
}
98102

99103
Direction RookDirections[] = { NORTH, EAST, SOUTH, WEST };
100104
Direction BishopDirections[] = { NORTH_EAST, SOUTH_EAST, SOUTH_WEST, NORTH_WEST };

0 commit comments

Comments
 (0)