Chess Engine
A Chess Engine project written in C++.
Loading...
Searching...
No Matches
move.hpp
1#pragma once
2
3#include "castling.hpp"
4#include "navigation.hpp"
5
6struct Flag {
7 public:
8 enum Value : uint8_t {
9 USUAL,
10 PROMOTION,
11 DOUBLE_PUSH,
12 CASTLING,
13
14 NB = 4,
15 SIZE = 2,
16 MASK = (1 << SIZE) - 1
17 };
18
19 constexpr Flag(Value value) : m_value(value) {}
20 constexpr Flag(uint8_t value) : m_value(static_cast<Value>(value)) {}
21
22 [[nodiscard]] constexpr operator uint8_t() const { return m_value; }
23
24 [[nodiscard]] static constexpr uint8_t size() { return SIZE; }
25 [[nodiscard]] static constexpr uint8_t mask() { return MASK; }
26 [[nodiscard]] static constexpr uint8_t number() { return NB; }
27
28 private:
29 Value m_value;
30};
31
32struct Promotion {
33 public:
34 enum Value : uint8_t {
35 TO_KNIGHT,
36 TO_BISHOP,
37 TO_ROOK,
38 TO_QUEEN,
39
40 NB = 4,
41 SIZE = 2,
42 MASK = (1 << SIZE) - 1
43 };
44
45 constexpr Promotion(Value value) : m_value(value) {}
46 constexpr Promotion(uint8_t value) : m_value(static_cast<Value>(value)) {}
47
48 [[nodiscard]] constexpr operator uint8_t() const { return m_value; }
49
50 [[nodiscard]] static constexpr uint8_t size() { return SIZE; }
51 [[nodiscard]] static constexpr uint8_t mask() { return MASK; }
52 [[nodiscard]] static constexpr uint8_t number() { return NB; }
53
54 private:
55 Value m_value;
56};
57
58// Move layout (16 bits):
59// [15..14] Promotion (2 bits)
60// [13..12] Flag (2 bits)
61// [11..6 ] To square (6 bits)
62// [5 ..0 ] From square (6 bits)
63
64struct Move {
65 public:
66 constexpr Move() : m_value(0) {}
67 constexpr Move(uint16_t value) : m_value(value) {}
68
69 constexpr Move(Square from, Square to, Flag flag = Flag::USUAL, Promotion promotion = Promotion::TO_QUEEN)
70 : m_value((promotion << PROMO_SHIFT) | (flag << FLAG_SHIFT) | (to << TO_SHIFT) | from) {}
71
72 [[nodiscard]] constexpr Square from() const { return m_value & Square::mask(); }
73
74 [[nodiscard]] constexpr Square to() const { return (m_value >> TO_SHIFT) & Square::mask(); }
75
76 [[nodiscard]] constexpr Flag flag() const { return (m_value >> FLAG_SHIFT) & Flag::mask(); }
77
78 [[nodiscard]] constexpr Promotion promotion() const { return (m_value >> PROMO_SHIFT) & Promotion::mask(); }
79
80 constexpr operator uint16_t() const { return m_value; }
81
82 [[nodiscard]] constexpr bool isNull() const { return m_value == 0; }
83
84 private:
85 uint16_t m_value;
86
87 static constexpr int FROM_SHIFT = 0;
88 static constexpr int TO_SHIFT = Square::size();
89 static constexpr int FLAG_SHIFT = 2 * Square::size();
90 static constexpr int PROMO_SHIFT = FLAG_SHIFT + Flag::size();
91};
92
93// Delta layout (32 bits):
94// [3 ..0 ] Captured Piece (4 bits)
95// [7 ..4 ] Castling Rights (4 bits)
96// [13..8 ] En Passant Square (6 bits)
97// [21..14] Halfmove Clock (8 bits)
98// [23..22] Extra Flags (2 bits)
99
100struct Delta {
101 public:
102 constexpr Delta() : m_value(0) {}
103
104 constexpr Delta(Piece captured, Castling castling, uint8_t enpassant, uint8_t halfmoves, uint8_t extraFlags = 0)
105 : m_value((static_cast<uint32_t>(captured) << CAPTURE_SHIFT) |
106 (static_cast<uint32_t>(castling) << CASTLING_SHIFT) |
107 (static_cast<uint32_t>(enpassant) << ENPASSANT_SHIFT) |
108 (static_cast<uint32_t>(halfmoves) << HALFMOVES_SHIFT) |
109 (static_cast<uint32_t>(extraFlags) << EXTRA_SHIFT)) {}
110
111 constexpr Delta(uint32_t raw) : m_value(raw) {}
112
113 constexpr operator uint32_t() const { return m_value; }
114
115 [[nodiscard]] constexpr Piece captured() const { return (m_value >> CAPTURE_SHIFT) & CAPTURE_MASK; }
116 [[nodiscard]] constexpr Castling castling() const {
117 return static_cast<Castling>((m_value >> CASTLING_SHIFT) & CASTLING_MASK);
118 }
119 [[nodiscard]] constexpr uint8_t enpassant() const { return (m_value >> ENPASSANT_SHIFT) & ENPASSANT_MASK; }
120 [[nodiscard]] constexpr uint8_t halfmoves() const { return (m_value >> HALFMOVES_SHIFT) & HALFMOVES_MASK; }
121 [[nodiscard]] constexpr uint8_t extraFlags() const { return (m_value >> EXTRA_SHIFT) & EXTRA_MASK; }
122
123 private:
124 uint32_t m_value;
125
126 static constexpr int CAPTURE_SHIFT = 0;
127 static constexpr int CASTLING_SHIFT = 4;
128 static constexpr int ENPASSANT_SHIFT = 8;
129 static constexpr int HALFMOVES_SHIFT = 14;
130 static constexpr int EXTRA_SHIFT = 22;
131
132 static constexpr uint32_t CAPTURE_MASK = 0xF;
133 static constexpr uint32_t CASTLING_MASK = 0xF;
134 static constexpr uint32_t ENPASSANT_MASK = 0x3F;
135 static constexpr uint32_t HALFMOVES_MASK = 0xFF;
136 static constexpr uint32_t EXTRA_MASK = 0x3;
137};
Definition castling.hpp:5
Definition move.hpp:6
Definition piece.hpp:66
Definition move.hpp:32
Definition navigation.hpp:118