stellar

UCI 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
3 #include "utils.hpp"
5 #include <cctype>
6 #include <exception>
8 namespace piece {
10 struct Piece {
11 const uint8_t index;
12 const Type type;
13 const Color color;
14 const char code;
15 };
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 };
37 inline constexpr const Piece &get(const Type type, const Color color) { return table[color][type]; }
39 inline constexpr const char get_code(const Type type, const Color color = BLACK) {
40 return get(type, color).code;
41 }
43 inline constexpr const U64 get_index(const Type type, const Color color) { return get(type, color).index; }
45 inline constexpr const Piece &get_from_code(const char code) {
46 Color color = isupper(code) ? WHITE : BLACK;
48 for (Type type = PAWN; type <= KING; ++type) {
49 const Piece &piece = get(type, color);
50 if (piece.code == code) return piece;
51 }
53 throw std::exception();
54 }
56 inline constexpr const Piece &get_from_index(const uint8_t index) { return table[index / 6][index % 6]; }
58 } // namespace piece
60 #endif