8-add-legal-move-filter (#16)
All checks were successful
Python tests (make) / test (push) Successful in 11s
All checks were successful
Python tests (make) / test (push) Successful in 11s
works on #8 Reviewed-on: #16 Co-authored-by: Josh <josh@joshuaschuett.com> Co-committed-by: Josh <josh@joshuaschuett.com>
This commit is contained in:
@@ -220,6 +220,9 @@ void gen_white_pawn_push_promotions(const struct Board *board, struct Move *out,
|
||||
int to = pop_lsb_index(&promos);
|
||||
int from = to - 8;
|
||||
push_move(out, count, from, to, P, Q, MF_PROMO);
|
||||
push_move(out, count, from, to, P, R, MF_PROMO);
|
||||
push_move(out, count, from, to, P, B, MF_PROMO);
|
||||
push_move(out, count, from, to, P, N, MF_PROMO);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -235,6 +238,9 @@ void gen_black_pawn_push_promotions(const struct Board *board, struct Move *out,
|
||||
int to = pop_lsb_index(&promos);
|
||||
int from = to + 8;
|
||||
push_move(out, count, from, to, p, q, MF_PROMO);
|
||||
push_move(out, count, from, to, p, r, MF_PROMO);
|
||||
push_move(out, count, from, to, p, b, MF_PROMO);
|
||||
push_move(out, count, from, to, p, n, MF_PROMO);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -245,14 +251,20 @@ void gen_white_pawn_capture_promotions(const struct Board *board, struct Move *o
|
||||
while (left) {
|
||||
int to = pop_lsb_index(&left);
|
||||
int from = to - 7;
|
||||
push_move(out, count,from, to, P, Q, MF_CAPTURE | MF_PROMO);
|
||||
push_move(out, count, from, to, P, Q, MF_CAPTURE | MF_PROMO);
|
||||
push_move(out, count, from, to, P, R, MF_CAPTURE | MF_PROMO);
|
||||
push_move(out, count, from, to, P, B, MF_CAPTURE | MF_PROMO);
|
||||
push_move(out, count, from, to, P, N, MF_CAPTURE | MF_PROMO);
|
||||
}
|
||||
// right capture: +9, mask off file H
|
||||
uint64_t right = ((board->pieces[P] & ~FILE_H) << 9) & opp & RANK_8;
|
||||
while (right) {
|
||||
int to = pop_lsb_index(&right);
|
||||
int from = to - 9;
|
||||
push_move(out, count,from, to, P, Q, MF_CAPTURE | MF_PROMO);
|
||||
push_move(out, count, from, to, P, Q, MF_CAPTURE | MF_PROMO);
|
||||
push_move(out, count, from, to, P, R, MF_CAPTURE | MF_PROMO);
|
||||
push_move(out, count, from, to, P, B, MF_CAPTURE | MF_PROMO);
|
||||
push_move(out, count, from, to, P, N, MF_CAPTURE | MF_PROMO);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -263,14 +275,20 @@ void gen_black_pawn_capture_promotions(const struct Board *board, struct Move *o
|
||||
while (left) {
|
||||
int to = pop_lsb_index(&left);
|
||||
int from = to + 7;
|
||||
push_move(out, count,from, to, p, q, MF_CAPTURE | MF_PROMO);
|
||||
push_move(out, count, from, to, p, q, MF_CAPTURE | MF_PROMO);
|
||||
push_move(out, count, from, to, p, r, MF_CAPTURE | MF_PROMO);
|
||||
push_move(out, count, from, to, p, b, MF_CAPTURE | MF_PROMO);
|
||||
push_move(out, count, from, to, p, n, MF_CAPTURE | MF_PROMO);
|
||||
}
|
||||
// “right” is -9 (mask off file A)
|
||||
uint64_t right = ((board->pieces[p] & ~FILE_A) >> 9) & opp & RANK_1;
|
||||
while (right) {
|
||||
int to = pop_lsb_index(&right);
|
||||
int from = to + 9;
|
||||
push_move(out, count,from, to, p, q, MF_CAPTURE | MF_PROMO);
|
||||
push_move(out, count, from, to, p, q, MF_CAPTURE | MF_PROMO);
|
||||
push_move(out, count, from, to, p, r, MF_CAPTURE | MF_PROMO);
|
||||
push_move(out, count, from, to, p, b, MF_CAPTURE | MF_PROMO);
|
||||
push_move(out, count, from, to, p, n, MF_CAPTURE | MF_PROMO);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -575,6 +593,198 @@ bool square_attacked(const struct Board *board, int sq, enum Color by) {
|
||||
return attackers_to(board, sq, by) != 0;
|
||||
}
|
||||
|
||||
int piece_at(const struct Board *board, int sq) {
|
||||
uint64_t mask = 1ULL << sq;
|
||||
for (int piece = 0; piece < 12; ++piece)
|
||||
if (board->pieces[piece] & mask) return piece;
|
||||
return -1;
|
||||
}
|
||||
|
||||
void rebuild_occ(struct Board *board) {
|
||||
uint64_t white=0;
|
||||
uint64_t black=0;
|
||||
|
||||
for (int p = P; p <= K; ++p) white |= board->pieces[p];
|
||||
for (int p = p; p <= k; ++p) black |= board->pieces[p];
|
||||
|
||||
board->occ[WHITE] = white;
|
||||
board->occ[BLACK] = black;
|
||||
board->occ[BOTH] = white | black;
|
||||
}
|
||||
|
||||
uint8_t castlerights_without(uint8_t castlerights, uint8_t mask) {
|
||||
// Update castle rights by removing mask.
|
||||
return castlerights & ~mask;
|
||||
}
|
||||
|
||||
uint8_t update_castlerights_after_move(
|
||||
uint8_t cr, int moved_pid, int from, int to, int cap_pid
|
||||
) {
|
||||
// King moved: lose both
|
||||
if (moved_pid == K) cr &= ~(CASTLE_WK | CASTLE_WQ);
|
||||
if (moved_pid == k) cr &= ~(CASTLE_BK | CASTLE_BQ);
|
||||
|
||||
// Rook moved from its home square
|
||||
if (moved_pid == R) {
|
||||
if (from == H1) cr &= ~CASTLE_WK;
|
||||
if (from == A1) cr &= ~CASTLE_WQ;
|
||||
} else if (moved_pid == r) {
|
||||
if (from == H8) cr &= ~CASTLE_BK;
|
||||
if (from == A8) cr &= ~CASTLE_BQ;
|
||||
}
|
||||
|
||||
// Captured a rook on its home square.
|
||||
// Obviously if a rook has moved and is captured,
|
||||
// the prior check accounts for that.
|
||||
if (cap_pid == R) {
|
||||
if (to == H1) cr &= ~CASTLE_WK;
|
||||
if (to == A1) cr &= ~CASTLE_WQ;
|
||||
} else if (cap_pid == r) {
|
||||
if (to == H8) cr &= ~CASTLE_BK;
|
||||
if (to == A8) cr &= ~CASTLE_BQ;
|
||||
}
|
||||
return cr;
|
||||
}
|
||||
|
||||
void put_piece(struct Board *board, int pid, int sq) {
|
||||
board->pieces[pid] |= 1ULL << sq;
|
||||
}
|
||||
|
||||
void del_piece(struct Board *board, int pid, int sq) {
|
||||
board->pieces[pid] &= ~(1ULL << sq);
|
||||
}
|
||||
|
||||
// Create a copy of the board to simulate move and test state.
|
||||
bool apply_move_on_copy(struct Board *in, struct Board *out, struct Move m) {
|
||||
*out = *in;
|
||||
const enum Color us = in->side_to_move;
|
||||
const enum Color opp = us ^ 1;
|
||||
|
||||
// Reset enpassant.
|
||||
out->ep_square = -1;
|
||||
|
||||
// Handle capture / EP capture
|
||||
int captured_pid = -1;
|
||||
if (m.flags & MF_ENPASSANT) {
|
||||
int cap_sq = (us == WHITE) ? (m.to - 8) : (m.to + 8);
|
||||
captured_pid = piece_at(out, cap_sq);
|
||||
if (captured_pid < 0) return false;
|
||||
del_piece(out, captured_pid, cap_sq);
|
||||
} else if (m.flags & MF_CAPTURE) {
|
||||
captured_pid = piece_at(out, m.to);
|
||||
if (captured_pid < 0) {
|
||||
return false;
|
||||
};
|
||||
del_piece(out, captured_pid, m.to);
|
||||
}
|
||||
|
||||
// Move (with promotion)
|
||||
del_piece(out, m.piece, m.from);
|
||||
// move.promo is the promotion piece.
|
||||
int placed = (m.flags & MF_PROMO) ? m.promo : m.piece;
|
||||
put_piece(out, placed, m.to);
|
||||
|
||||
// Castling: move rook
|
||||
if (m.flags & MF_CASTLE) {
|
||||
if (us == WHITE) {
|
||||
if (m.to == WK_TO) {
|
||||
del_piece(out, R, H1);
|
||||
put_piece(out, R, F1);
|
||||
}
|
||||
else {
|
||||
del_piece(out, R, A1);
|
||||
put_piece(out, R, D1);
|
||||
}
|
||||
} else {
|
||||
if (m.to == BK_TO) {
|
||||
del_piece(out, r, H8);
|
||||
put_piece(out, r, F8);
|
||||
}
|
||||
else {
|
||||
del_piece(out, r, A8);
|
||||
put_piece(out, r, D8);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Double pawn push -> set EP square
|
||||
if (m.flags & MF_DOUBLE_PUSH) out->ep_square = (us == WHITE) ? (m.to - 8) : (m.to + 8);
|
||||
|
||||
// Update castling rights
|
||||
out->castling_rights = update_castlerights_after_move(
|
||||
in->castling_rights, m.piece, m.from, m.to, captured_pid
|
||||
);
|
||||
|
||||
// Side to move flips
|
||||
out->side_to_move = opp;
|
||||
|
||||
rebuild_occ(out);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool castle_path_safe(const struct Board *board, enum Color side, int king_to) {
|
||||
enum Color opp = side ^ 1;
|
||||
if (side == WHITE) {
|
||||
if (king_to == WK_TO) {
|
||||
// E1-F1-G1
|
||||
if (square_attacked(board, E1, opp)) return false;
|
||||
if (square_attacked(board, F1, opp)) return false;
|
||||
} else {
|
||||
// E1-D1-C1
|
||||
if (square_attacked(board, E1, opp)) return false;
|
||||
if (square_attacked(board, D1, opp)) return false;
|
||||
}
|
||||
} else {
|
||||
if (king_to == BK_TO) {
|
||||
// E8-F8-G8
|
||||
if (square_attacked(board, E8, opp)) return false;
|
||||
if (square_attacked(board, F8, opp)) return false;
|
||||
} else {
|
||||
// E8-D8-C8
|
||||
if (square_attacked(board, E8, opp)) return false;
|
||||
if (square_attacked(board, D8, opp)) return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool move_is_legal(struct Board *board, struct Move move) {
|
||||
enum Color us = board->side_to_move;
|
||||
enum Color opp = us ^ 1;
|
||||
|
||||
// Castle-through-check rule
|
||||
if (move.flags & MF_CASTLE) {
|
||||
if (!castle_path_safe(board, us, move.to)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
struct Board after;
|
||||
if (!apply_move_on_copy(board, &after, move)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
// Our king must not be in check after the move
|
||||
uint64_t king_bb = (us == WHITE) ? after.pieces[K] : after.pieces[k];
|
||||
|
||||
if (!king_bb) return false;
|
||||
|
||||
int king_sq = first_set_index(king_bb);
|
||||
return !square_attacked(&after, king_sq, opp);
|
||||
}
|
||||
|
||||
int get_legal_moves(struct Board *board, struct Move *out) {
|
||||
struct Move tmp[256];
|
||||
int n = gen_pseudo_moves(board, tmp, false);
|
||||
|
||||
int count = 0;
|
||||
for (int i = 0; i < n; ++i)
|
||||
if (move_is_legal(board, tmp[i]))
|
||||
out[count++] = tmp[i];
|
||||
return count;
|
||||
}
|
||||
|
||||
void print_board(const struct Board *board) {
|
||||
const char PIECE_CH[12] = {
|
||||
'P','N','B','R','Q','K',
|
||||
|
||||
Reference in New Issue
Block a user