sq_attackers() + others (see dedails). Ready for move do/undo ?
- add many "const" in func parameters - attack.c: sq_attackers() - move print_board_raw from position.c to to board.c - move some fen_check() tests to pos_check() - add REL_RANK() macro. TODO: add one more for bitboards - fen.c: more tests for FEN validity - position.c: add pos_checkers() and pos_check() - tests: add common-test.h (for shared FEN positions access)
This commit is contained in:
56
src/attack.c
56
src/attack.c
@@ -18,9 +18,10 @@
|
||||
#include "bitboard.h"
|
||||
#include "position.h"
|
||||
#include "hyperbola-quintessence.h"
|
||||
#include "attack.h"
|
||||
|
||||
/**
|
||||
* square_attackers() - find attackers on a square
|
||||
* sq_attackers() - find attackers on a square
|
||||
* @pos: position
|
||||
* @sq: square to test
|
||||
* @c: attacker color
|
||||
@@ -36,32 +37,69 @@
|
||||
* @Return: a bitboard of attackers.
|
||||
*
|
||||
*/
|
||||
bitboard_t sq_attackers(pos_t *pos, square_t sq, color_t c)
|
||||
bitboard_t sq_attackers(const pos_t *pos, const square_t sq, const color_t c)
|
||||
{
|
||||
bitboard_t attackers = 0;
|
||||
bitboard_t from = mask(sq);
|
||||
bitboard_t sqbb = mask(sq);
|
||||
bitboard_t c_pieces = pos->bb[c][ALL_PIECES];
|
||||
bitboard_t occ = c_pieces | pos->bb[OPPONENT(c)][ALL_PIECES];
|
||||
bitboard_t to;
|
||||
color_t opp = OPPONENT(c);
|
||||
|
||||
/* pawn */
|
||||
to = pos->bb[c][PAWN];
|
||||
attackers |= pawn_push_upleft(from, c) & to;
|
||||
attackers |= pawn_push_upright(from, c) & to;
|
||||
attackers |= pawn_shift_upleft(sqbb, opp) & to;
|
||||
# ifdef DEBUG_ATTACK
|
||||
bitboard_print("sq_attackers after pawn upleft", attackers);
|
||||
# endif
|
||||
attackers |= pawn_shift_upright(sqbb, opp) & to;
|
||||
# ifdef DEBUG_ATTACK
|
||||
bitboard_print("sq_attackers pawn upright", attackers);
|
||||
# endif
|
||||
|
||||
/* knight & king */
|
||||
to = pos->bb[c][KNIGHT];
|
||||
attackers |= bb_knight_moves(c_pieces, from);
|
||||
attackers |= bb_knight_moves(to, sq);
|
||||
# ifdef DEBUG_ATTACK
|
||||
bitboard_print("sq_attackers after knight", attackers);
|
||||
# endif
|
||||
to = pos->bb[c][KING];
|
||||
attackers |= bb_king_moves(c_pieces, from);
|
||||
attackers |= bb_king_moves(to, sq);
|
||||
# ifdef DEBUG_ATTACK
|
||||
bitboard_print("sq_attackers after king", attackers);
|
||||
# endif
|
||||
|
||||
/* bishop / queen */
|
||||
to = pos->bb[c][BISHOP] | pos->bb[c][QUEEN];
|
||||
attackers |= hyperbola_bishop_moves(occ, from) & to;
|
||||
attackers |= hyperbola_bishop_moves(occ, sq) & to;
|
||||
# ifdef DEBUG_ATTACK
|
||||
bitboard_print("sq_attackers after bishop/queen", attackers);
|
||||
# endif
|
||||
|
||||
/* rook / queen */
|
||||
to = pos->bb[c][ROOK] | pos->bb[c][QUEEN];
|
||||
attackers |= hyperbola_rook_moves(occ, from) & to;
|
||||
# ifdef DEBUG_ATTACK
|
||||
bitboard_print("sq_attackers after queen", attackers);
|
||||
# endif
|
||||
attackers |= hyperbola_rook_moves(occ, sq) & to;
|
||||
# ifdef DEBUG_ATTACK
|
||||
bitboard_print("sq_attackers after rook/queen", attackers);
|
||||
# endif
|
||||
|
||||
return attackers;
|
||||
}
|
||||
|
||||
/**
|
||||
* sq_attackers() - find all attackers on a square
|
||||
* @pos: position
|
||||
* @sq: square to test
|
||||
*
|
||||
* Find all attacks on @sq. En-passant is not considered.
|
||||
* Just a wrapper over @sq_attackers().
|
||||
*
|
||||
* @Return: a bitboard of attackers.
|
||||
*/
|
||||
bitboard_t sq_attackers_all(const pos_t *pos, const square_t sq)
|
||||
{
|
||||
return sq_attackers(pos, sq, WHITE) | sq_attackers(pos, sq, BLACK);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user