11-add-attack-detection (#15)
All checks were successful
Python tests (make) / test (push) Successful in 10s

works on #11

Reviewed-on: #15
Co-authored-by: Josh <josh@joshuaschuett.com>
Co-committed-by: Josh <josh@joshuaschuett.com>
This commit is contained in:
2025-08-19 15:56:28 +00:00
committed by Josh
parent 54537e404b
commit e5a75c0737
4 changed files with 209 additions and 5 deletions

View File

@@ -87,6 +87,16 @@ gen_queen_moves = _bind_opt("gen_queen_moves", *PIECE_SIG)
gen_king_moves = _bind_opt("gen_king_moves", *PIECE_SIG)
# Attack checks.
ATTACKED_SIG = (C.POINTER(Board), C.c_int, C.c_int)
INCHECK_ARGS = (C.POINTER(Board), C.c_int)
ATTACKERS_TO = (C.POINTER(Board), C.c_int, C.c_int)
square_attacked = _bind_opt("square_attacked", ATTACKED_SIG, C.c_bool)
in_check = _bind_opt("in_check", INCHECK_ARGS, C.c_bool)
attackers_to = _bind_opt("attackers_to", ATTACKERS_TO, C.c_uint64)
# Attack cache tables.
KnightArr = (C.c_uint64 * 64)
KingArr = (C.c_uint64 * 64)
@@ -102,8 +112,8 @@ except ValueError:
def init_attack_caches():
if create_knight_attack_cache: create_knight_attack_cache()
if create_king_attack_cache: create_king_attack_cache()
if create_pawn_attack_cache: create_pawn_attack_cache()
if create_king_attack_cache: create_king_attack_cache()
if create_pawn_attack_cache: create_pawn_attack_cache()
def load_fen(board, fen):
@@ -116,6 +126,18 @@ def gen_moves(board, captures_only=False, cap=256):
return buf, n
def is_square_attacked(board, sq, by):
return bool(square_attacked(C.byref(board), int(sq), int(by)))
def is_in_check(board, side):
return bool(in_check(C.byref(board), int(side)))
def get_attackers_to(board, sq, by):
return int(attackers_to(C.byref(board), int(sq), int(by)))
def sq_to_coord(sq):
return chr(ord('a') + (sq % 8)) + chr(ord('1') + (sq // 8))
@@ -137,7 +159,7 @@ def popcount(x: int) -> int:
return x.bit_count()
def draw_bb(mask: int, origin: int | None = None) -> str:
def draw_bb(mask, origin=None):
print("\n")
lines = []
for r in range(7, -1, -1):