... but does not work as well!ZirconiumX wrote:In theory, it would have worked. In practice however...
The error for being ahead of the square should be fixed by replacing 8 with 7, and wraparound by ANDing with the file ahead. So we get this:Which is somewhat less elegant...Code: Select all
void InitPawnAttacks() { const Bitboard a = 0x5ULL; for (int i = a2; i < h7; i++) { PawnAttacks[0][i] = (a << (i + 7)) bitand (FileMaskForSquare(i) + 1); for (int i = a2; i < h7; i++) { PawnAttacks[1][i] = (a << (i - 7)) bitand (FileMaskForSquare(i) - 1); }
Your loop should terminate by "i <= h7", not "i < h7". I would also use only one loop for white and black.
For black the shifting by (i-7) looks wrong to me, I think it should be (i-9). Example: i=b3 (17), 0x5ULL << (17-7) = 0x1400ULL which is (c2|e2) but you want to get (a2|c2) and that can't be corrected by any subsequent ANDing. Shifting by "i" puts a three-bit pattern to square positions i, i+1, i+2 and you always want to move that pattern one file to the left and either one rank up or down, so (i+7) for white and (i-9) for black seems to be appropriate.
Also I am not sure about the proper handling of a- and h-file wraparound, your FileMaskForSquare(i) must be somewhat tricky to catch that case after having already shifted across the border.
Instead of always using the pattern 0x5ULL together with "FileMaskForSquare(i)" which complicates the usage I would use a pattern depending on the rank of square i, and simply always shift it by i+7 resp. i-9 (assuming a1=0 orientation):
Code: Select all
void InitPawnAttacks()
{
static Bitboard const a[8] = {
0x4ULL, 0x5ULL, 0x5ULL, 0x5ULL, 0x5ULL, 0x5ULL, 0x5ULL, 0x1ULL
};
for (int i = a2; i <= h7; i++) {
PawnAttacks[0][i] = a[rank(i)] << (i+7);
PawnAttacks[1][i] = a[rank(i)] << (i-9);
}
}