make_move now checks castle rights after each move

This commit is contained in:
Karma Riuk 2025-02-02 23:52:24 +01:00
parent f0467bc516
commit 8da349579d

View File

@ -201,8 +201,8 @@ Board Board::make_move(Move move) const {
}
// -- Handle castling (just move the rook over)
Coords c = Coords::from_index(move.source_square);
if (move.castle_side & KingSide) {
Coords c = Coords::from_index(move.source_square);
Coords rook_source{7, c.y};
int8_t old_rook = ret.squares[rook_source.to_index()];
ret.squares[rook_source.to_index()] = Piece::None;
@ -217,6 +217,49 @@ Board Board::make_move(Move move) const {
ret.squares[rook_dest.to_index()] = old_rook;
}
// -- Check for castling rights
ret.w_castle_rights = w_castle_rights;
ret.b_castle_rights = b_castle_rights;
if (white_to_play) {
if ((squares[move.source_square] & 0b111) == King)
ret.w_castle_rights = NeitherSide;
if ((squares[move.source_square] & 0b111) == Rook) {
if (c.x == 0 && (ret.w_castle_rights & QueenSide))
ret.w_castle_rights &= ~(QueenSide);
if (c.x == 7 && (ret.w_castle_rights & KingSide))
ret.w_castle_rights &= ~(KingSide);
}
Coords target = Coords::from_index(move.target_square);
if (move.is_capturing && c.y == 7
&& (squares[move.target_square] & 0b111) == Rook) {
if (target.x == 0 && (ret.b_castle_rights & QueenSide))
ret.b_castle_rights &= ~(QueenSide);
if (target.x == 7 && (ret.b_castle_rights & KingSide))
ret.b_castle_rights &= ~(KingSide);
}
} else {
if ((squares[move.source_square] & 0b111) == King)
ret.b_castle_rights = NeitherSide;
if ((squares[move.source_square] & 0b111) == Rook) {
if (c.x == 0 && (ret.w_castle_rights & QueenSide))
ret.b_castle_rights &= ~(QueenSide);
if (c.x == 7 && (ret.w_castle_rights & KingSide))
ret.b_castle_rights &= ~(KingSide);
}
Coords target = Coords::from_index(move.target_square);
if (move.is_capturing && c.y == 7
&& (squares[move.target_square] & 0b111) == Rook) {
if (target.x == 0 && (ret.w_castle_rights & QueenSide))
ret.w_castle_rights &= ~(QueenSide);
if (target.x == 7 && (ret.w_castle_rights & KingSide))
ret.w_castle_rights &= ~(KingSide);
}
}
return ret;
}