Fix attack to tests

This commit is contained in:
2025-08-22 14:28:48 -04:00
parent f3b6b7448e
commit 5ddb359675
3 changed files with 52 additions and 54 deletions

View File

@@ -28,12 +28,12 @@ FILES = {c:i for i,c in enumerate("abcdefgh")}
WHITE, BLACK, BOTH = 0, 1, 2
P, N, B, R, Q, K, p, n, b, r, q, k = range(12)
"""
Register C function bindings and interfaces in a dictionary.
"""
PAWN_MOVE_SIG = ((C.POINTER(Board), C.POINTER(Move), C.POINTER(C.c_int)), None)
PIECE_MOVE_SIG = ((C.POINTER(Board), C.POINTER(Move), C.POINTER(C.c_int), C.c_bool), None)
FFI_SPEC = {
"create_knight_attack_cache": None,
"create_king_attack_cache": None,
@@ -61,9 +61,40 @@ FFI_SPEC = {
}
def draw_bb(mask, origin=None):
print("\n")
lines = []
for r in range(7, -1, -1):
row = []
for f in range(8):
sqi = r * 8 + f
bit = (mask >> sqi) & 1
if origin is not None and sqi == origin:
ch = 'O'
elif bit:
ch = 'x'
else:
ch = '.'
row.append(ch)
lines.append(f"{r+1} " + " ".join(row))
lines.append(" " + " ".join(FILES))
lines = "\n".join(lines)
print(lines, "\n")
def sq_to_coord(sq):
return chr(ord('a') + (sq % 8)) + chr(ord('1') + (sq // 8))
def sq(name):
f = FILES[name[0].lower()]
r = int(name[1]) - 1
return f + 8 * r
class ChessFFI:
def __init__(self):
self.lib_path = "./build/lichess.so"
self.lib_path = "./build/libchess.so"
self._load_lib()
self._load_constants()
self._bind_functions()
@@ -76,7 +107,7 @@ class ChessFFI:
def load_fen(self, board, fen_string):
return self._c_load_fen(C.byref(board), fen_string.encode("ascii"))
return self._c_load_fen(board, fen_string.encode("ascii"))
def gen_pseudo_moves(self, board, captures_only=False, cap=256):
@@ -98,44 +129,13 @@ class ChessFFI:
def attackers_to(self, board, sq, by):
return int(self._c_attackers_to(C.byref(board), int(sq), int(by)))
def sq_to_coord(self, sq):
return chr(ord('a') + (sq % 8)) + chr(ord('1') + (sq // 8))
def sq(self, name):
f = FILES[name[0].lower()]
r = int(name[1]) - 1
return f + 8 * r
return self._c_attackers_to(board, sq, by)
def popcount(self, x):
return x.bit_count()
def draw_bb(self, mask, origin=None):
print("\n")
lines = []
for r in range(7, -1, -1):
row = []
for f in range(8):
sqi = r * 8 + f
bit = (mask >> sqi) & 1
if origin is not None and sqi == origin:
ch = 'O'
elif bit:
ch = 'x'
else:
ch = '.'
row.append(ch)
lines.append(f"{r+1} " + " ".join(row))
lines.append(" " + " ".join(FILES))
lines = "\n".join(lines)
print(lines, "\n")
def _load_lib(self):
self._lib = C.CDLL(self.lib_path)
@@ -164,7 +164,7 @@ class ChessFFI:
if fn is not None:
fn.argtypes = argtypes
fn.restype = restype
fn.restype = restype
# Prepend each function name with _c to allow us to reuse the
# function names as methods on this class. This means that we