I know that to store the knight’s attacks for example I begin by initializing an array of 64 uint64s representing all the possible knight attacks and fill it up then whenever I need the move of a knight I can just get its index in the array of attacks but that only works for knights, how would I do it for other pieces such as pawns?
Example of getting knight attacks:
std::array<uint64_t, 64> getKnightAttacks()
{
const uint64_t notTopMask = 0x0000ffffffffffff;
const uint64_t notBottomMask = 0xffffffffffff0000;
const uint64_t notLeftMask = 0xfcfcfcfcfcfcfcfc;
const uint64_t notRightMask = 0x3f3f3f3f3f3f3f3f;
std::array<uint64_t, 64> knight_attacks{};
for (uint64_t i = 0; i < 64; i++)
{
uint64_t temp_attacks = 0;
temp_attacks |= (i << 6) & notBottomMask & notRightMask; // noWeWe
temp_attacks |= (i << 15) & notBottomMask & notRightMask; // noNoWe
temp_attacks |= (i << 17) & notBottomMask & notLeftMask; // noNoEa
temp_attacks |= (i << 10) & notBottomMask & notLeftMask; // noEaEa
temp_attacks |= (i >> 6) & notTopMask & notLeftMask; // soEaEa
temp_attacks |= (i >> 15) & notTopMask & notLeftMask; // soSoEa
temp_attacks |= (i >> 17) & notTopMask & notRightMask; // soSoWe
temp_attacks |= (i >> 10) & notTopMask & notRightMask; // soWeWe
knight_attacks[i] = temp_attacks;
}
return knight_attacks;
}
New contributor
ns8 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.