-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboard.cpp
More file actions
412 lines (347 loc) · 12.6 KB
/
Copy pathboard.cpp
File metadata and controls
412 lines (347 loc) · 12.6 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
#include <bitbishop/board.hpp>
#include <bitbishop/constants.hpp>
#include <cassert>
#include <format>
#include <sstream>
Board::Board() : Board("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1") {}
Board::Board(const std::string& fen) {
/*
* https://en.wikipedia.org/wiki/Forsyth%E2%80%93Edwards_Notation
* https://www.chess.com/terms/fen-chess
*
* FEN (Forsyth-Edwards Notation) has 6 fields separated by slashes and spaces:
*
* 1. Piece placement (ranks 8 → 1, separated by /).
* 2. Side to move (w or b).
* 3. Castling availability (KQkq or -).
* 4. En passant target square (like e3 or -).
* 5. Halfmove clock (for 50-move rule).
* 6. Fullmove number (starts at 1).
*
* Example FEN for starting position: "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"
*/
using namespace Const;
std::istringstream iss(fen);
std::string token;
iss >> token;
int index = BOARD_SIZE - BOARD_WIDTH;
for (unsigned char character : token) {
if (character == '/') {
index -= 2 * BOARD_WIDTH;
continue;
}
if (std::isdigit(character) != 0) {
const int skip = character - '0';
index += skip;
continue;
}
const Square square(index);
Piece piece(character);
set_piece(square, piece);
index++;
}
// Second token: side to move
iss >> token;
m_state.m_is_white_turn = (token == "w");
// Third token: Castling Rights
iss >> token;
m_state.m_white_castle_kingside = token.find('K') != std::string::npos;
m_state.m_white_castle_queenside = token.find('Q') != std::string::npos;
m_state.m_black_castle_kingside = token.find('k') != std::string::npos;
m_state.m_black_castle_queenside = token.find('q') != std::string::npos;
// Fourth token: en passant
iss >> token;
if (token == "-") {
m_state.m_en_passant_sq = std::nullopt;
} else {
m_state.m_en_passant_sq = Square(token);
}
// Fifth token: Halfmove clock
iss >> m_state.m_halfmove_clock;
// Sixth token: Fullmove number
iss >> m_state.m_fullmove_number;
m_zobrist_hash = Zobrist::compute_hash(*this);
}
std::ostream& operator<<(std::ostream& out, const Board& board) {
using namespace Const;
const std::string separator = "+---+---+---+---+---+---+---+---+\n";
const std::string files = " a b c d e f g h \n";
out << " " << files;
out << " " << separator;
for (int rank = RANK_8_IND; rank >= RANK_1_IND; --rank) {
out << " " << (rank + 1) << " |";
for (int file = FILE_A_IND; file <= FILE_H_IND; ++file) {
Square square(file, rank);
std::optional<Piece> opt_p = board.get_piece(square);
const char piece_char = opt_p ? opt_p->to_char() : ' ';
out << " " << piece_char << " |";
}
out << " " << (rank + 1) << "\n";
out << " " << separator;
}
out << " " << files;
return out;
}
Bitboard Board::white_pieces() const {
Bitboard bitboard;
bitboard |= m_w_pawns;
bitboard |= m_w_rooks;
bitboard |= m_w_bishops;
bitboard |= m_w_knights;
bitboard |= m_w_king;
bitboard |= m_w_queens;
return bitboard;
}
Bitboard Board::black_pieces() const {
Bitboard bitboard;
bitboard |= m_b_pawns;
bitboard |= m_b_rooks;
bitboard |= m_b_bishops;
bitboard |= m_b_knights;
bitboard |= m_b_king;
bitboard |= m_b_queens;
return bitboard;
}
Bitboard Board::occupied() const {
Bitboard bitboard;
bitboard |= m_b_pawns;
bitboard |= m_b_rooks;
bitboard |= m_b_bishops;
bitboard |= m_b_knights;
bitboard |= m_b_king;
bitboard |= m_b_queens;
bitboard |= m_w_pawns;
bitboard |= m_w_rooks;
bitboard |= m_w_bishops;
bitboard |= m_w_knights;
bitboard |= m_w_king;
bitboard |= m_w_queens;
return bitboard;
}
std::optional<Piece> Board::get_piece(Square square) const {
// clang-format off
if (m_w_pawns.test(square)) { return Pieces::WHITE_PAWN; }
if (m_w_knights.test(square)) { return Pieces::WHITE_KNIGHT; }
if (m_w_bishops.test(square)) { return Pieces::WHITE_BISHOP; }
if (m_w_rooks.test(square)) { return Pieces::WHITE_ROOK; }
if (m_w_queens.test(square)) { return Pieces::WHITE_QUEEN; }
if (m_w_king.test(square)) { return Pieces::WHITE_KING; }
if (m_b_pawns.test(square)) { return Pieces::BLACK_PAWN; }
if (m_b_knights.test(square)) { return Pieces::BLACK_KNIGHT; }
if (m_b_bishops.test(square)) { return Pieces::BLACK_BISHOP; }
if (m_b_rooks.test(square)) { return Pieces::BLACK_ROOK; }
if (m_b_queens.test(square)) { return Pieces::BLACK_QUEEN; }
if (m_b_king.test(square)) { return Pieces::BLACK_KING; }
// clang-format on
return std::nullopt; // NO_PIECE
}
void Board::move_piece(Square from, Square to) {
if (from == to) {
return;
}
if (auto captured = get_piece(to)) {
remove_piece(to);
}
auto moving_piece = get_piece(from);
if (!moving_piece) {
return;
}
remove_piece(from);
set_piece(to, moving_piece.value());
}
void Board::set_piece(Square square, Piece piece) {
// Remove any existing piece if existent
const std::optional<Piece> existing_piece = get_piece(square);
if (existing_piece.has_value()) {
remove_piece(square);
}
// Use color to select white or black bitboards, then type for the specific piece
if (piece.is_white()) {
switch (piece.type()) {
// clang-format off
case Piece::PAWN: m_w_pawns.set(square); break;
case Piece::KNIGHT: m_w_knights.set(square); break;
case Piece::BISHOP: m_w_bishops.set(square); break;
case Piece::ROOK: m_w_rooks.set(square); break;
case Piece::QUEEN: m_w_queens.set(square); break;
case Piece::KING: m_w_king.set(square); break;
// clang-format on
}
} else { // is_black()
switch (piece.type()) {
// clang-format off
case Piece::PAWN: m_b_pawns.set(square); break;
case Piece::KNIGHT: m_b_knights.set(square); break;
case Piece::BISHOP: m_b_bishops.set(square); break;
case Piece::ROOK: m_b_rooks.set(square); break;
case Piece::QUEEN: m_b_queens.set(square); break;
case Piece::KING: m_b_king.set(square); break;
// clang-format on
}
}
Zobrist::mutate_piece(square, piece, m_zobrist_hash);
}
void Board::remove_piece(Square square) {
if (const std::optional<Piece> existing_piece = get_piece(square)) {
Zobrist::mutate_piece(square, *existing_piece, m_zobrist_hash);
}
// clear the square from ALL bitboards (only one will match)
m_w_pawns.clear(square);
m_w_knights.clear(square);
m_w_bishops.clear(square);
m_w_rooks.clear(square);
m_w_queens.clear(square);
m_w_king.clear(square);
m_b_pawns.clear(square);
m_b_knights.clear(square);
m_b_bishops.clear(square);
m_b_rooks.clear(square);
m_b_queens.clear(square);
m_b_king.clear(square);
}
void Board::print() const { std::cout << *this; }
bool Board::can_castle_kingside(Color side) const noexcept {
if (!this->has_kingside_castling_rights(side)) {
return false;
}
Square king_sq = (side == Color::WHITE) ? Squares::E1 : Squares::E8;
Square rook_sq = (side == Color::WHITE) ? Squares::H1 : Squares::H8;
Square f_sq = (side == Color::WHITE) ? Squares::F1 : Squares::F8;
Square g_sq = (side == Color::WHITE) ? Squares::G1 : Squares::G8;
// Check if king is on the starting square
if (!this->king(side).test(king_sq)) {
return false;
}
// Check if rook is on the starting square
if (!this->rooks(side).test(rook_sq)) {
return false;
}
// Check if squares between king and rook are empty
const Bitboard occupied = this->occupied();
return !occupied.test(f_sq) && !occupied.test(g_sq);
}
bool Board::can_castle_queenside(Color side) const noexcept {
if (!this->has_queenside_castling_rights(side)) {
return false;
}
Square king_sq = (side == Color::WHITE) ? Squares::E1 : Squares::E8;
Square rook_sq = (side == Color::WHITE) ? Squares::A1 : Squares::A8;
Square b_sq = (side == Color::WHITE) ? Squares::B1 : Squares::B8;
Square c_sq = (side == Color::WHITE) ? Squares::C1 : Squares::C8;
Square d_sq = (side == Color::WHITE) ? Squares::D1 : Squares::D8;
// Check that the king is on the starting square
if (!this->king(side).test(king_sq)) {
return false;
}
// Check if the rook is on the starting square
if (!this->rooks(side).test(rook_sq)) {
return false;
}
// Check if the squares between king and rook are empty
const Bitboard occupied = this->occupied();
return !occupied.test(b_sq) && !occupied.test(c_sq) && !occupied.test(d_sq);
}
bool Board::has_insufficient_material() const noexcept {
// A valid board must always have exactly one king per side.
// If this assertion fails, there is a bug upstream.
assert(m_w_king.count() == 1 && m_b_king.count() == 1);
const std::size_t nb_pieces = pieces_count();
// K vs K
if (nb_pieces == 2) {
return true;
}
if (nb_pieces == 3) {
// K + B vs K or K + N vs K
const bool white_has_sole_minor = (m_w_bishops.count() + m_w_knights.count()) == 1;
const bool black_has_sole_minor = (m_b_bishops.count() + m_b_knights.count()) == 1;
return white_has_sole_minor || black_has_sole_minor;
}
if (nb_pieces == 4) {
const bool have_one_bishop_each = m_w_bishops.count() == 1 && m_b_bishops.count() == 1;
const bool have_one_knight_each = m_w_knights.count() == 1 && m_b_knights.count() == 1;
// K + B vs K + B: insufficient only if bishops share the same square color
if (have_one_bishop_each) {
return m_w_bishops.lsb()->same_color(*m_b_bishops.lsb());
}
// K + N vs K + N: not theoretically impossible to force mate but
// not recognized as sufficient material by FIDE - treated as a draw in practice.
if (have_one_knight_each) {
return true;
}
}
return false;
}
std::string Board::get_fen() const noexcept {
using namespace Const;
std::string fen;
fen.reserve(FEN_NOTATION_MAX_CHAR_COUNT);
// Board position
for (int rank = RANK_8_IND; rank >= RANK_1_IND; --rank) {
std::size_t empty = 0;
for (int file = FILE_A_IND; file <= FILE_H_IND; ++file) {
const auto opt_p = get_piece(Square(file, rank));
if (opt_p) {
if (empty > 0) {
fen += std::to_string(empty);
empty = 0;
}
fen += opt_p->to_char();
} else {
++empty;
}
}
if (empty > 0) {
fen += std::to_string(empty);
}
if (rank != RANK_1_IND) {
fen += "/";
}
}
fen += " ";
// Side to move
fen += (m_state.m_is_white_turn ? "w" : "b");
fen += " ";
// Castling availability
bool has_castling = false;
// clang-format off
if (m_state.m_white_castle_kingside) { fen += 'K'; has_castling = true; }
if (m_state.m_white_castle_queenside) { fen += 'Q'; has_castling = true; }
if (m_state.m_black_castle_kingside) { fen += 'k'; has_castling = true; }
if (m_state.m_black_castle_queenside) { fen += 'q'; has_castling = true; }
if (!has_castling) { fen += '-'; }
// clang-format on
fen += " ";
// En passant target
fen += (m_state.m_en_passant_sq ? m_state.m_en_passant_sq->to_string() : "-");
fen += " ";
// Halfmove clock
fen += std::to_string(m_state.m_halfmove_clock);
fen += " ";
// Fullmove number
fen += std::to_string(m_state.m_fullmove_number);
return fen;
}
bool Board::operator==(const Board& other) const {
if (this == &other) {
return true;
}
// Zobrist key is a fast, deterministic fingerprint of the position identity
// fields we compare below (piece placement + side/castling/en-passant).
// If keys differ, boards cannot be equal.
if (m_zobrist_hash != other.m_zobrist_hash) {
return false;
}
// Do not compare half-move clock and full-move number
// This is not relevant for position identity and we don't care about game history equality
return m_w_pawns == other.m_w_pawns && m_w_rooks == other.m_w_rooks && m_w_bishops == other.m_w_bishops &&
m_w_knights == other.m_w_knights && m_w_king == other.m_w_king && m_w_queens == other.m_w_queens &&
m_b_pawns == other.m_b_pawns && m_b_rooks == other.m_b_rooks && m_b_bishops == other.m_b_bishops &&
m_b_knights == other.m_b_knights && m_b_king == other.m_b_king && m_b_queens == other.m_b_queens &&
m_state.m_is_white_turn == other.m_state.m_is_white_turn &&
m_state.m_en_passant_sq == other.m_state.m_en_passant_sq &&
m_state.m_white_castle_kingside == other.m_state.m_white_castle_kingside &&
m_state.m_white_castle_queenside == other.m_state.m_white_castle_queenside &&
m_state.m_black_castle_kingside == other.m_state.m_black_castle_kingside &&
m_state.m_black_castle_queenside == other.m_state.m_black_castle_queenside;
}
bool Board::operator!=(const Board& other) const { return !this->operator==(other); }