stellarUCI Chess engine written in C++20 |
git clone git://git.dimitrijedobrota.com/stellar.git |
Log | Files | Refs | README | LICENSE |
move.cpp (4096B)
0 #include "move.hpp"
1 #include "utils.hpp"
3 #include <algorithm>
4 #include <iomanip>
6 void Move::piece_remove(Board &board, piece::Type type, color::Color color, square::Square square) const {
7 board.pop_piece(type, color, square);
8 board.xor_hash(Zobrist::key_piece(type, color, square));
9 }
11 void Move::piece_set(Board &board, piece::Type type, color::Color color, square::Square square) const {
12 board.set_piece(type, color, square);
13 board.xor_hash(Zobrist::key_piece(type, color, square));
14 }
16 void Move::piece_move(Board &board, piece::Type type, color::Color color, square::Square source,
17 square::Square target) const {
18 piece_remove(board, type, color, source);
19 piece_set(board, type, color, target);
20 }
22 using piece::Type::PAWN;
23 using piece::Type::ROOK;
25 bool Move::make(Board &board) const {
26 static constexpr const int castling_rights[64] = {
27 // clang-format off
28 13, 15, 15, 15, 12, 15, 15, 14,
29 15, 15, 15, 15, 15, 15, 15, 15,
30 15, 15, 15, 15, 15, 15, 15, 15,
31 15, 15, 15, 15, 15, 15, 15, 15,
32 15, 15, 15, 15, 15, 15, 15, 15,
33 15, 15, 15, 15, 15, 15, 15, 15,
34 15, 15, 15, 15, 15, 15, 15, 15,
35 7, 15, 15, 15, 3, 15, 15, 11,
36 // clang-format on
37 };
39 const color::Color color = board.get_side(), colorOther = color::other(color);
40 const square::Square source = this->source(), target = this->target();
42 const square::Square ntarget =
43 static_cast<square::Square>(to_underlying(this->target()) + (color == color::Color::WHITE ? -8 : +8));
45 const piece::Type piece = board.get_square_piece_type(source);
47 if (!is_capture()) {
48 if (is_promote()) {
49 piece_remove(board, piece, color, source);
50 piece_set(board, promoted(), color, target);
51 } else {
52 piece_move(board, piece, color, source, target);
53 }
54 } else {
55 const piece::Type captured = board.get_square_piece_type(target);
56 if (is_enpassant()) {
57 piece_move(board, piece, color, source, target);
58 piece_remove(board, PAWN, colorOther, ntarget);
59 } else if (is_promote()) {
60 piece_remove(board, piece, color, source);
61 piece_remove(board, captured, colorOther, target);
62 piece_set(board, promoted(), color, target);
63 } else {
64 piece_remove(board, captured, colorOther, target);
65 piece_move(board, piece, color, source, target);
66 }
67 }
69 board.set_enpassant(is_double() ? ntarget : square::Square::no_sq);
71 if (is_castle()) {
72 if (color == color::Color::WHITE) {
73 if (is_castle_king())
74 piece_move(board, ROOK, color::Color::WHITE, square::Square::h1, square::Square::f1);
75 if (is_castle_queen())
76 piece_move(board, ROOK, color::Color::WHITE, square::Square::a1, square::Square::d1);
77 } else {
78 if (is_castle_king())
79 piece_move(board, ROOK, color::Color::BLACK, square::Square::h8, square::Square::f8);
80 if (is_castle_queen())
81 piece_move(board, ROOK, color::Color::BLACK, square::Square::a8, square::Square::d8);
82 }
83 }
85 board.and_castle(castling_rights[to_underlying(this->source())] &
86 castling_rights[to_underlying(this->target())]);
88 if (!board.is_check()) {
89 board.switch_side();
90 return 1;
91 }
92 return 0;
93 }
95 void Move::print(void) const {
96 std::cout << square::to_coordinates(source()) << " ";
97 std::cout << square::to_coordinates(target()) << " ";
98 std::cout << (is_promote() ? piece::get_code(promoted()) : '.') << " ";
99 std::cout << is_double() << " ";
100 std::cout << is_enpassant() << " ";
101 std::cout << is_castle();
102 }
104 Move::operator std::string() const {
105 std::string res = square::to_coordinates(source()) + square::to_coordinates(target());
106 if (is_promote()) res += piece::get_code(promoted());
107 return res;
108 }
110 std::ostream &operator<<(std::ostream &os, Move move) { return os << (std::string)move; }