From 7637bdad10b4ff7fc69ce0313fe848054c7b82c6 Mon Sep 17 00:00:00 2001 From: Bruno Raoult Date: Tue, 26 Mar 2024 17:34:11 +0100 Subject: [PATCH] attack.c: sq_is_attacked() and is_in_check() --- src/attack.c | 63 +++++++++++++++++++++++++++++++++++- src/attack.h | 3 ++ src/bitboard.c | 2 -- src/bitboard.h | 16 ++++----- src/hyperbola-quintessence.h | 20 ++++++------ 5 files changed, 83 insertions(+), 21 deletions(-) diff --git a/src/attack.c b/src/attack.c index bc09669..150d14d 100644 --- a/src/attack.c +++ b/src/attack.c @@ -21,7 +21,68 @@ #include "attack.h" -// #define DEBUG_ATTACK_ATTACKERS1 +/** + * sq_is_attacked() - find if a square is attacked + * @pos: position + * @occ: occupation mask used + * @sq: square to test + * @c: attacker color + * + * Find if a @c piece attacks @sq. + * + * Algorithm: We perform a reverse attack, and check if any given + * piece type on @sq can attack a @c piece of same type. + * + * For example, if @c is white, we test for all T in P,N,B,R,Q,K if + * T on @sq could attack a white T piece. + * + * @Return: true if @sq is attacked by a @c piece, false otherwise. + */ +bool sq_is_attacked(const pos_t *pos, const bitboard_t occ, const square_t sq, const color_t c) +{ + bitboard_t sqbb = mask(sq); + color_t opp = OPPONENT(c); + + + //pos_print_raw(pos, 1); + + /* bishop / queen */ + if (hyperbola_bishop_moves(occ, sq) & (pos->bb[c][BISHOP] | pos->bb[c][QUEEN])) + return true; + + /* rook / queen */ + if (hyperbola_rook_moves(occ, sq) & (pos->bb[c][ROOK] | pos->bb[c][QUEEN])) + return true; + + /* pawn */ + if ((pawn_shift_upleft(sqbb, opp) | pawn_shift_upright(sqbb, opp)) & pos->bb[c][PAWN]) + return true; + + /* knight */ + if (bb_knight_moves(pos->bb[c][KNIGHT], sq)) + return true; + + /* king */ + if (bb_king_moves(pos->bb[c][KING], sq)) + return true; + + return false; +} + +/** + * is_in_check() - find if a king is in check. + * @pos: position + * @color: king color + * + * Find if a @c king is in check. This function is a wrapper over @sq_is_attacked(). + * + * @Return: true if @sq is attacked by a @c piece, false otherwise. + */ +bool is_in_check(const pos_t *pos, const color_t color) +{ + bitboard_t occ = pos_occ(pos); + return sq_is_attacked(pos, occ, pos->king[color], OPPONENT(color)); +} /** * sq_attackers() - find attackers on a square diff --git a/src/attack.h b/src/attack.h index 603a965..6c999bc 100644 --- a/src/attack.h +++ b/src/attack.h @@ -17,6 +17,9 @@ #include "chessdefs.h" #include "bitboard.h" +bool sq_is_attacked(const pos_t *pos, const bitboard_t occ, const square_t sq, const color_t c); +bool is_in_check(const pos_t *pos, const color_t color); + bitboard_t sq_attackers(const pos_t *pos, const bitboard_t occ, const square_t sq, const color_t c); bitboard_t sq_attackers_all(const pos_t *pos, const square_t sq); bitboard_t sq_pinners(const pos_t *pos, const square_t sq, const color_t c); diff --git a/src/bitboard.c b/src/bitboard.c index e78e3d6..984ccbb 100644 --- a/src/bitboard.c +++ b/src/bitboard.c @@ -190,8 +190,6 @@ void bitboard_init(void) */ bitboard_t bb_knight_moves(bitboard_t notmine, square_t sq) { - //bitboard_print("bb_knight_move mask", bb_knight[sq]); - //bitboard_print("bb_knight_move res", bb_knight[sq] & notmine); return bb_knight[sq] & notmine; } diff --git a/src/bitboard.h b/src/bitboard.h index c537946..dccf58a 100644 --- a/src/bitboard.h +++ b/src/bitboard.h @@ -272,15 +272,15 @@ static __always_inline bitboard_t shift_nw(const bitboard_t bb) #define pawn_push_upleft(sq, c) ((sq) + ((c) == WHITE ? NORTH_WEST: SOUTH_EAST)) #define pawn_push_upright(sq, c) ((sq) + ((c) == WHITE ? NORTH_EAST: SOUTH_WEST)) -extern bitboard_t bitboard_between_excl(square_t sq1, square_t sq2); -extern void bitboard_init(void); +bitboard_t bitboard_between_excl(square_t sq1, square_t sq2); +void bitboard_init(void); -extern bitboard_t bb_knight_moves(bitboard_t occ, square_t sq); -extern bitboard_t bb_king_moves(bitboard_t occ, square_t sq); +bitboard_t bb_knight_moves(bitboard_t notmine, square_t sq); +bitboard_t bb_king_moves(bitboard_t notmine, square_t sq); -extern void bb_print(const char *title, const bitboard_t bitboard); -extern void bb_print_multi(const char *title, const int n, ...); -extern char *bb_rank_sprint(char *str, const uchar bb8); -extern char *bb_sq2str(const bitboard_t bb, char *str, int len); +void bb_print(const char *title, const bitboard_t bitboard); +void bb_print_multi(const char *title, const int n, ...); +char *bb_rank_sprint(char *str, const uchar bb8); +char *bb_sq2str(const bitboard_t bb, char *str, int len); #endif /* _BITBOARD_H */ diff --git a/src/hyperbola-quintessence.h b/src/hyperbola-quintessence.h index c7cc8b7..82c867c 100644 --- a/src/hyperbola-quintessence.h +++ b/src/hyperbola-quintessence.h @@ -17,17 +17,17 @@ #include "board.h" #include "bitboard.h" -extern void hyperbola_init(void); +void hyperbola_init(void); -extern bitboard_t hyperbola_rank_moves(const bitboard_t occ, const square_t sq); -extern bitboard_t hyperbola_moves(const bitboard_t pieces, const square_t sq, - const bitboard_t mask); -extern bitboard_t hyperbola_file_moves(const bitboard_t occ, const square_t sq); -extern bitboard_t hyperbola_diag_moves(const bitboard_t occ, const square_t sq); -extern bitboard_t hyperbola_anti_moves(const bitboard_t occ, const square_t sq); +bitboard_t hyperbola_rank_moves(const bitboard_t occ, const square_t sq); +bitboard_t hyperbola_moves(const bitboard_t pieces, const square_t sq, + const bitboard_t mask); +bitboard_t hyperbola_file_moves(const bitboard_t occ, const square_t sq); +bitboard_t hyperbola_diag_moves(const bitboard_t occ, const square_t sq); +bitboard_t hyperbola_anti_moves(const bitboard_t occ, const square_t sq); -extern bitboard_t hyperbola_bishop_moves(const bitboard_t occ, const square_t sq); -extern bitboard_t hyperbola_rook_moves(const bitboard_t occ, const square_t sq); -extern bitboard_t hyperbola_queen_moves(const bitboard_t occ, const square_t sq); +bitboard_t hyperbola_bishop_moves(const bitboard_t occ, const square_t sq); +bitboard_t hyperbola_rook_moves(const bitboard_t occ, const square_t sq); +bitboard_t hyperbola_queen_moves(const bitboard_t occ, const square_t sq); #endif /* _HYPERBOLA_QUINTESSENCE_H */