Skip to content

Commit

Permalink
Delay validation for passing an attacked field in castling
Browse files Browse the repository at this point in the history
  • Loading branch information
alex65536 committed Aug 5, 2023
1 parent 6f4b862 commit 0fcea38
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 13 deletions.
25 changes: 15 additions & 10 deletions src/core/movegen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -280,19 +280,17 @@ inline static size_t genCastling(const Board &b, Move *list, const GenFilter) {
constexpr coord_t castlingOffset = Private::castlingOffset(C);
if (b.isKingsideCastling(C)) {
constexpr bitboard_t castlingPass = Private::BB_CASTLING_KINGSIDE_PASS << castlingOffset;
constexpr coord_t src = makeCoord(x, 4);
constexpr coord_t tmp = makeCoord(x, 5);
constexpr coord_t dst = makeCoord(x, 6);
if (!(castlingPass & b.bbAll) && !isCellAttacked<invert(C)>(b, tmp)) {
if (!(castlingPass & b.bbAll)) {
constexpr coord_t src = makeCoord(x, 4);
constexpr coord_t dst = makeCoord(x, 6);
list[size++] = Move{MoveKind::CastlingKingside, src, dst, 0};
}
}
if (b.isQueensideCastling(C)) {
constexpr bitboard_t castlingPass = Private::BB_CASTLING_QUEENSIDE_PASS << castlingOffset;
constexpr coord_t src = makeCoord(x, 4);
constexpr coord_t tmp = makeCoord(x, 3);
constexpr coord_t dst = makeCoord(x, 2);
if (!(castlingPass & b.bbAll) && !isCellAttacked<invert(C)>(b, tmp)) {
if (!(castlingPass & b.bbAll)) {
constexpr coord_t src = makeCoord(x, 4);
constexpr coord_t dst = makeCoord(x, 2);
list[size++] = Move{MoveKind::CastlingQueenside, src, dst, 0};
}
}
Expand Down Expand Up @@ -417,12 +415,12 @@ inline static bool isMoveValidImpl(const Board &b, const Move move) {
if (SOF_UNLIKELY(move.kind == MoveKind::CastlingKingside)) {
constexpr bitboard_t castlingPass = Private::BB_CASTLING_KINGSIDE_PASS << castlingOffset;
return b.isKingsideCastling(C) && !(castlingPass & b.bbAll) &&
!isCellAttacked<invert(C)>(b, move.src) && !isCellAttacked<invert(C)>(b, move.src + 1);
!isCellAttacked<invert(C)>(b, move.src);
}
if (SOF_UNLIKELY(move.kind == MoveKind::CastlingQueenside)) {
constexpr bitboard_t castlingPass = Private::BB_CASTLING_QUEENSIDE_PASS << castlingOffset;
return b.isQueensideCastling(C) && !(castlingPass & b.bbAll) &&
!isCellAttacked<invert(C)>(b, move.src) && !isCellAttacked<invert(C)>(b, move.src - 1);
!isCellAttacked<invert(C)>(b, move.src);
}
const coord_t src = move.src;
const coord_t dst = move.dst;
Expand Down Expand Up @@ -508,6 +506,13 @@ inline static bool isMoveLegalImpl(const Board &b, const Move move) {

const coord_t src = move.src;
const coord_t dst = move.dst;

if (SOF_UNLIKELY(move.kind == MoveKind::CastlingKingside ||
move.kind == MoveKind::CastlingQueenside)) {
const coord_t tmp = (src + dst) >> 1;
return !isCellAttacked<invert(C)>(b, tmp) && !isCellAttacked<invert(C)>(b, dst);
}

const bitboard_t bbSrc = coordToBitboard(src);
const bitboard_t bbDst = coordToBitboard(dst);
const cell_t srcCell = b.cells[src];
Expand Down
6 changes: 3 additions & 3 deletions src/core/movegen.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ class MoveGen {
const Board &board() const { return b_; }

// All these functions generate all the legal moves plus some pseudo-legal moves. Thus, after
// applying some of the generated moves, the king may become under check, but are otherwise valid
// according to the rules of chess. The moves are written into `list`, and the return value
// indicates the number of moves generated.
// applying some of the generated moves, the king may become under check or pass though an
// attacked field in castling, but are otherwise valid according to the rules of chess. The moves
// are written into `list`, and the return value indicates the number of moves generated.
//
// To generate only legal moves you can use `isMoveLegal()` function
size_t genAllMoves(Move *list) const;
Expand Down

0 comments on commit 0fcea38

Please sign in to comment.