knight.hpp (1233B)
1 #ifndef STELLAR_ATTACK_KNIGHT_H 2 #define STELLAR_ATTACK_KNIGHT_H 3 4 #include "bit.hpp" 5 #include "bitboard.hpp" 6 #include "utils.hpp" 7 8 #include <array> 9 10 namespace attack { 11 namespace knight { 12 13 static constexpr U64 mask(const Square square) { 14 U64 bitboard = C64(0), attacks = C64(0), tmp; 15 16 bit::set(bitboard, square); 17 tmp = bitboard::nortOne(bitboard::nortOne(bitboard)); 18 attacks |= bitboard::westOne(tmp) | bitboard::eastOne(tmp); 19 tmp = bitboard::soutOne(bitboard::soutOne(bitboard)); 20 attacks |= bitboard::westOne(tmp) | bitboard::eastOne(tmp); 21 tmp = bitboard::westOne(bitboard::westOne(bitboard)); 22 attacks |= bitboard::soutOne(tmp) | bitboard::nortOne(tmp); 23 tmp = bitboard::eastOne(bitboard::eastOne(bitboard)); 24 attacks |= bitboard::soutOne(tmp) | bitboard::nortOne(tmp); 25 26 return attacks; 27 } 28 29 typedef std::array<U64, 64> attack_array; 30 const attack_array attacks = []() -> attack_array { 31 std::array<U64, 64> attacks; 32 33 for (Square square = Square::a1; square <= Square::h8; ++square) { 34 attacks[square] = mask(square); 35 } 36 37 return attacks; 38 }(); 39 40 inline constexpr U64 attack(const Square square) { return attacks[square]; } 41 42 } // namespace knight 43 } // namespace attack 44 45 #endif