-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathchess.h
104 lines (85 loc) · 3.79 KB
/
chess.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include "board.h"
#include <cassert>
enum class gen_mode {all_moves, only_captures, only_silent};
class chess : public board {
public:
using move_s = board::move_s;
chess();
chess(const std::string &fen);
static constexpr const char* start_pos =
"rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq -";
std::vector<board::move_s> gen_moves();
void gen_pseudo_legal_moves(std::vector<move_s> &moves,
const gen_mode mode) const;
bool setup_position(const std::string &fen);
bool enter_move(const std::string &str);
bool is_mate();
bool is_draw();
bool is_stalemate();
bool is_in_check(const bool king_side) const;
bool is_N_fold_repetition(const unsigned N) const;
bool is_draw_by_material() const;
bool game_over() {
return is_mate() || is_draw();
}
bool is_repetition() const {
return is_N_fold_repetition(1);
}
bool is_draw_by_N_move_rule(const int N) {
return reversible_halfmoves == 2*N && !is_mate();
}
void gen_pseudo_legal_moves(std::vector<move_s> &moves) {
gen_pseudo_legal_moves(moves, gen_mode::all_moves);
}
bool was_legal(const move_s &move) const {
if(move.index == king_ix && std::abs(i8(move.from_coord) -
i8(move.to_coord)) == 2)
return castling_was_legal(side, move.from_coord, move.to_coord);
return !is_in_check(!side);
}
protected:
std::array<std::array<u64, 2>, 2> castle_masks;
std::vector<move_s> gen_moves(size_t max_moves);
void gen_pawns(std::vector<move_s> &moves, const u64 occupancy,
const u64 opp_occupancy, const gen_mode mode) const;
void gen_pawns_silent(std::vector<move_s> &moves, const u64 pawn_bb,
const u64 occupancy) const;
void gen_pawns_captures_and_promotions(std::vector<move_s> &moves,
const u64 pawn_bb,
const u64 occupancy,
u64 opp_occupancy) const;
void push_pawn_moves(std::vector<move_s> &moves, u64 bitboard,
const int shift, const bool is_capt) const;
void gen_non_pawns(std::vector<move_s> &moves, const u8 piece_index,
const u64 occupancy, const u64 opp_occupancy,
const gen_mode mode) const;
void gen_castles(std::vector<move_s> &moves, const u64 occupancy) const;
bool is_attacked_by_slider(const u8 coord, const bool att_side) const;
bool is_attacked_by_non_slider(const u8 coord, const bool att_side) const;
bool castling_was_legal(const bool att_side, const u8 fr_crd,
const u8 to_crd) const;
bool is_pseudo_legal(const move_s move) const;
bool is_pseudo_legal_pawn(const u8 from_coord, const u64 to_bb) const;
bool is_pseudo_legal_king(const u8 from_coord, const u8 to_coord) const;
bool slider_maybe_attacks(const u8 coord, const bool att_side) const;
std::string game_text_result();
u64 all_non_pawn_attacks(const u8 index, const u8 from_coord,
const u64 occupancy) const {
if(index == knight_ix)
return knight_attacks(from_coord);
else if(index == bishop_ix)
return bishop_attacks(from_coord, occupancy);
else if(index == rook_ix)
return rook_attacks(from_coord, occupancy);
else if(index == queen_ix)
return queen_attacks(from_coord, occupancy);
else if(index == king_ix)
return king_attacks(from_coord);
assert(false);
return(false);
}
void gen_pawns(std::vector<move_s> &moves, const u64 occupancy,
const u64 opp_occupancy) {
gen_pawns(moves, occupancy, opp_occupancy, gen_mode::all_moves);
}
};