attack.c: sq_is_attacked() and is_in_check()

This commit is contained in:
2024-03-26 17:34:11 +01:00
parent 70d6c23c00
commit 7637bdad10
5 changed files with 83 additions and 21 deletions

View File

@@ -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

View File

@@ -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);

View File

@@ -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;
}

View File

@@ -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 */

View File

@@ -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 */