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