piece.hpp (1947B)
1 #ifndef STELLAR_PIECE_H 2 #define STELLAR_PIECE_H 3 4 #include "utils.hpp" 5 6 #include <cctype> 7 #include <exception> 8 9 namespace piece { 10 11 struct Piece { 12 const uint8_t index; 13 const Type type; 14 const Color color; 15 const char code; 16 }; 17 18 inline constexpr const Piece table[2][6] = { 19 // clang-format off 20 { 21 { .index = 0, .type = PAWN, .color = WHITE, .code = 'P' }, 22 { .index = 1, .type = KNIGHT, .color = WHITE, .code = 'N' }, 23 { .index = 2, .type = BISHOP, .color = WHITE, .code = 'B' }, 24 { .index = 3, .type = ROOK, .color = WHITE, .code = 'R' }, 25 { .index = 4, .type = QUEEN, .color = WHITE, .code = 'Q' }, 26 { .index = 5, .type = KING, .color = WHITE, .code = 'K' }, 27 }, { 28 { .index = 6, .type = PAWN, .color = BLACK, .code = 'p' }, 29 { .index = 7, .type = KNIGHT, .color = BLACK, .code = 'n' }, 30 { .index = 8, .type = BISHOP, .color = BLACK, .code = 'b' }, 31 { .index = 9, .type = ROOK, .color = BLACK, .code = 'r' }, 32 {.index = 10, .type = QUEEN, .color = BLACK, .code = 'q' }, 33 {.index = 11, .type = KING, .color = BLACK, .code = 'k' }, 34 }, 35 // clang-format on 36 }; 37 38 inline constexpr const Piece &get(const Type type, const Color color) { return table[color][type]; } 39 40 inline constexpr const char get_code(const Type type, const Color color = BLACK) { 41 return get(type, color).code; 42 } 43 44 inline constexpr const U64 get_index(const Type type, const Color color) { return get(type, color).index; } 45 46 inline constexpr const Piece &get_from_code(const char code) { 47 Color color = isupper(code) ? WHITE : BLACK; 48 49 for (Type type = PAWN; type <= KING; ++type) { 50 const Piece &piece = get(type, color); 51 if (piece.code == code) return piece; 52 } 53 54 throw std::exception(); 55 } 56 57 inline constexpr const Piece &get_from_index(const uint8_t index) { return table[index / 6][index % 6]; } 58 59 } // namespace piece 60 61 #endif