Add some basic algorithms and eval functions for an AI
All checks were successful
Python tests (make) / test (push) Successful in 11s

This commit is contained in:
2025-08-23 13:10:05 -04:00
parent 60a987a7a1
commit 4fc982eac2
6 changed files with 409 additions and 21 deletions

View File

@@ -28,6 +28,9 @@ 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)
PIECE_CH = ('P','N','B','R','Q','K','p','n','b','r','q','k')
FILES_STR = "a b c d e f g h"
"""
Register C function bindings and interfaces in a dictionary.
@@ -58,6 +61,9 @@ FFI_SPEC = {
"perft": ((C.POINTER(Board), C.c_int), C.c_uint64),
"load_fen": ((C.POINTER(Board), C.c_char_p), C.c_int),
"gen_pseudo_moves": ((C.POINTER(Board), C.POINTER(Move), C.c_bool), C.c_int),
"apply_move_on_copy": ((C.POINTER(Board), C.POINTER(Board), Move), C.c_bool),
# AI Methods
"ai_find_best_move_with_window": ((C.POINTER(Board), C.c_int, C.c_int, C.POINTER(Move)), C.c_int)
}
@@ -79,7 +85,29 @@ def draw_bb(mask, origin=None):
lines.append(f"{r+1} " + " ".join(row))
lines.append(" " + " ".join(FILES))
lines = "\n".join(lines)
print(lines, "\n")
print(lines, "\n")
def print_board(board):
"""
Prints the current position using piece letters from board.pieces.
highlight_sq: optional 0..63 square index to mark with 'O'.
"""
def piece_at(sq: int) -> str:
for i, ch in enumerate(PIECE_CH):
if (int(board.pieces[i]) >> sq) & 1:
return ch
return '.'
lines = []
for r in range(7, -1, -1):
row = []
for f in range(8):
sq = r * 8 + f
row.append(piece_at(sq))
lines.append(f"{r+1} " + " ".join(row))
lines.append(" " + FILES_STR)
print("\n" + "\n".join(lines) + "\n")
def sq_to_coord(sq):
@@ -131,6 +159,10 @@ class ChessFFI:
return buf, n
def apply_move_on_copy(self, board, out_board, move):
return bool(self._c_apply_move_on_copy(C.byref(board), C.byref(out_board), move))
def get_legal_moves(self, board, out):
return self._c_get_legal_moves(board, out)