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