move.hpp (2427B)
1 #ifndef STELLAR_MOVES_H 2 #define STELLAR_MOVES_H 3 4 #include "board.hpp" 5 #include "piece.hpp" 6 #include "utils.hpp" 7 8 #include <iostream> 9 #include <vector> 10 11 struct Move { 12 enum Flag : uint8_t { 13 QUIET, 14 DOUBLE, 15 CASTLEK, 16 CASTLEQ, 17 CAPTURE, 18 ENPASSANT, 19 PQUIET, 20 PKNIGHT = 8, 21 PBISHOP, 22 PROOK, 23 PQUEEN, 24 PCKNIGHT, 25 PCBISHOP, 26 PCROOK, 27 PCQUEEN, 28 }; 29 30 Move() : source_i(0), target_i(0), flags_i(0) {} 31 Move(Square source, Square target, Flag flags) : source_i(source), target_i(target), flags_i(flags) {} 32 33 friend bool operator==(const Move a, const Move b) { 34 return a.source_i == b.source_i && a.target_i == b.target_i && a.flags_i == b.flags_i; 35 } 36 37 [[nodiscard]] constexpr Square source() const { return static_cast<Square>(source_i); } 38 [[nodiscard]] constexpr Square target() const { return static_cast<Square>(target_i); } 39 40 [[nodiscard]] constexpr bool is_capture() const { return flags_i != PQUIET && (flags_i & CAPTURE); } 41 [[nodiscard]] constexpr bool is_promote() const { return flags_i & 0x8; } 42 43 [[nodiscard]] constexpr bool is_double() const { return flags_i == DOUBLE; } 44 [[nodiscard]] constexpr bool is_repeatable() const { return flags_i == QUIET; } 45 [[nodiscard]] constexpr bool is_quiet() const { return flags_i == QUIET || flags_i == PQUIET; } 46 47 [[nodiscard]] constexpr bool is_castle() const { return flags_i == CASTLEK || flags_i == CASTLEQ; } 48 [[nodiscard]] constexpr bool is_castle_king() const { return flags_i == CASTLEK; } 49 [[nodiscard]] constexpr bool is_castle_queen() const { return flags_i == CASTLEQ; } 50 51 [[nodiscard]] constexpr bool is_enpassant() const { return flags_i == ENPASSANT; } 52 53 [[nodiscard]] constexpr const Type promoted() const { return static_cast<Type>((flags_i & 0x3) + 1); } 54 55 bool make(Board &board) const; 56 57 operator std::string() const; 58 friend std::ostream &operator<<(std::ostream &os, Move move); 59 void print() const; 60 61 private: 62 inline void piece_remove(Board &board, Type type, Color color, Square square) const; 63 inline void piece_set(Board &board, Type type, Color color, Square square) const; 64 inline void piece_move(Board &board, Type type, Color color, Square source, Square target) const; 65 66 unsigned source_i : 6; 67 unsigned target_i : 6; 68 unsigned flags_i : 4; 69 }; 70 71 #endif