stellar

UCI Chess engine written in C++20
git clone git://git.dimitrijedobrota.com/stellar.git
Log | Files | Refs | README | LICENSE |

piece.hpp (2969B)


1 #ifndef STELLAR_PIECE_H 2 #define STELLAR_PIECE_H 3 4 #include "attack.hpp" 5 #include "color.hpp" 6 #include "utils.hpp" 7 8 #include <cctype> 9 10 namespace piece { 11 12 enum Type { 13 PAWN = 0, 14 KNIGHT, 15 BISHOP, 16 ROOK, 17 QUEEN, 18 KING, 19 NONE = 7, 20 }; 21 typedef Iterator<Type, Type::PAWN, Type::KING> TypeIter; 22 23 struct Piece { 24 const uint8_t index; 25 const Type type; 26 const color::Color color; 27 const char code; 28 const attack::attack_f attack; 29 }; 30 31 inline constexpr const Piece table[2][6] = { 32 // clang-format off 33 { 34 { .index = 0, .type = PAWN, .color = color::WHITE, .code = 'P', .attack = attack::pawnw::attack }, 35 { .index = 1, .type = KNIGHT, .color = color::WHITE, .code = 'N', .attack = attack::knight::attack }, 36 { .index = 2, .type = BISHOP, .color = color::WHITE, .code = 'B', .attack = attack::bishop::attack }, 37 { .index = 3, .type = ROOK, .color = color::WHITE, .code = 'R', .attack = attack::rook::attack }, 38 { .index = 4, .type = QUEEN, .color = color::WHITE, .code = 'Q', .attack = attack::queen::attack }, 39 { .index = 5, .type = KING, .color = color::WHITE, .code = 'K', .attack = attack::king::attack }, 40 }, { 41 { .index = 6, .type = PAWN, .color = color::BLACK, .code = 'p', .attack = attack::pawnb::attack }, 42 { .index = 7, .type = KNIGHT, .color = color::BLACK, .code = 'n', .attack = attack::knight::attack }, 43 { .index = 8, .type = BISHOP, .color = color::BLACK, .code = 'b', .attack = attack::bishop::attack }, 44 { .index = 9, .type = ROOK, .color = color::BLACK, .code = 'r', .attack = attack::rook::attack }, 45 {.index = 10, .type = QUEEN, .color = color::BLACK, .code = 'q', .attack = attack::queen::attack }, 46 {.index = 11, .type = KING, .color = color::BLACK, .code = 'k', .attack = attack::king::attack }, 47 }, 48 // clang-format on 49 }; 50 51 inline constexpr const Piece &get(const Type type, const color::Color color) { 52 return table[static_cast<uint8_t>(color)][static_cast<uint8_t>(type)]; 53 } 54 55 inline constexpr const U64 get_attack(const Type type, const color::Color color, const square::Square from, 56 const U64 occupancy) { 57 return get(type, color).attack(from, occupancy); 58 } 59 60 inline constexpr const char get_code(const Type type, const color::Color color = color::BLACK) { 61 return get(type, color).code; 62 } 63 64 inline constexpr const U64 get_index(const Type type, const color::Color color) { 65 return get(type, color).index; 66 } 67 68 inline constexpr const Piece &get_from_code(const char code) { 69 color::Color color = isupper(code) ? color::WHITE : color::BLACK; 70 71 for (Type type : TypeIter()) { 72 const Piece &piece = get(type, color); 73 if (piece.code == code) return piece; 74 } 75 76 throw std::exception(); 77 } 78 79 inline constexpr const Piece &get_from_index(const uint8_t index) { return table[index / 6][index % 6]; } 80 81 } // namespace piece 82 83 #endif