220 lines
8.3 KiB
Python
220 lines
8.3 KiB
Python
import ctypes
|
|
from test.base import ChessLibTestBase
|
|
from test.chess_ffi import gen_white_pawn_quiet_pushes
|
|
from test.chess_ffi import gen_white_pawn_push_promotions
|
|
from test.chess_ffi import gen_white_pawn_capture_promotions
|
|
from test.chess_ffi import gen_white_pawn_captures
|
|
from test.chess_ffi import gen_knight_moves
|
|
from test.chess_ffi import gen_bishop_moves
|
|
from test.chess_ffi import Move
|
|
from test.chess_ffi import Board
|
|
|
|
|
|
MAX_MOVES = 256
|
|
|
|
|
|
class TestPseudoMoveGeneration(ChessLibTestBase):
|
|
def test_knight_move_gen(self):
|
|
all_move_types = [
|
|
("8/8/8/8/3N4/8/8/8 w - - 0 1", 8, "center d4: 8 moves"),
|
|
("8/8/8/8/N7/8/8/8 w - - 0 1", 4, "edge a4: 4 moves"),
|
|
("8/8/8/8/8/8/8/N7 w - - 0 1", 2, "corner a1: 2 moves"),
|
|
("8/8/8/8/8/8/8/1N6 w - - 0 1", 3, "b1: 3 moves"),
|
|
|
|
# own pieces on destinations reduce count
|
|
("8/8/8/5P2/3N4/1P6/2P5/8 w - - 0 1", 5, "d4 with own pawns on f5,b3,c2 -> 8-3=5"),
|
|
|
|
# two knights
|
|
("8/8/8/8/8/8/8/1N4N1 w - - 0 1", 6, "b1 & g1, empty board: 3+3"),
|
|
|
|
# all 8 destinations occupied by black (still 8 total moves, all as captures)
|
|
("8/8/2p1p3/1p3p2/3N4/1p3p2/2p1p3/8 w - - 0 1", 8, "enemy on all 8 targets"),
|
|
|
|
# corner with one capture and one friendly block
|
|
("8/8/8/8/8/1p6/2P5/N7 w - - 0 1", 1, "a1, b3 black (capture), c2 white (blocked)"),
|
|
]
|
|
|
|
captures_only = False
|
|
for fen, expected, msg in all_move_types:
|
|
with self.subTest(msg=msg, fen=fen):
|
|
cnt = ctypes.c_int(0)
|
|
moves = (Move * MAX_MOVES)()
|
|
b = Board()
|
|
|
|
self.load_fen(fen, board=b)
|
|
gen_knight_moves(b, moves, ctypes.byref(cnt), captures_only)
|
|
|
|
self.assertEqual(expected, cnt.value)
|
|
|
|
cases_knight_captures = [
|
|
("8/8/8/8/3N4/8/8/8 w - - 0 1", 0, "center d4 but no enemies"),
|
|
("8/8/2p5/1p6/3N4/5p2/8/8 w - - 0 1", 3, "d4 capturing c6,b5,f3"),
|
|
("8/8/8/8/3N4/1P3p2/4P3/8 w - - 0 1", 1, "d4 with f3 black (1 cap), b3/e2 white (not capturable)"),
|
|
("8/8/2p1p3/1p3p2/3N4/1p3p2/2p1p3/8 w - - 0 1", 8, "d4 with all 8 targets black: 8 captures"),
|
|
]
|
|
|
|
captures_only = True
|
|
for fen, expected, msg in cases_knight_captures:
|
|
with self.subTest(msg=msg, fen=fen):
|
|
cnt = ctypes.c_int(0)
|
|
moves = (Move * MAX_MOVES)()
|
|
b = Board()
|
|
|
|
self.load_fen(fen, board=b)
|
|
gen_knight_moves(b, moves, ctypes.byref(cnt), captures_only)
|
|
|
|
self.assertEqual(expected, cnt.value)
|
|
|
|
|
|
def test_bishop_move_gen(self):
|
|
cases_bishop_all = [
|
|
("8/8/8/8/3B4/8/8/8 w - - 0 1", 13, "center d4: 13 moves"),
|
|
("8/8/8/8/B7/8/8/8 w - - 0 1", 7, "edge a4: 7 moves"),
|
|
("8/8/8/8/8/8/8/B7 w - - 0 1", 7, "corner a1: 7 moves"),
|
|
("8/8/8/8/8/8/8/1B6 w - - 0 1", 7, "b1: 7 moves"),
|
|
|
|
# all four diagonals blocked immediately by own pieces -> 0
|
|
("8/8/8/2P1P3/3B4/2P1P3/8/8 w - - 0 1", 0, "d4 with white pawns on c3,e3,c5,e5"),
|
|
|
|
# all four diagonals have a black piece on the first square -> 4 (all captures)
|
|
("8/8/8/2p1p3/3B4/2p1p3/8/8 w - - 0 1", 4, "d4 with black pawns on c3,e3,c5,e5"),
|
|
|
|
# mixed blockers: NE own on f6, SW capture on b2, SE capture on e3, NW open
|
|
("8/8/1p3P2/8/3B4/4p3/1p6/8 w - - 0 1", 6, ""),
|
|
]
|
|
|
|
captures_only = False
|
|
for fen, expected, msg in cases_bishop_all:
|
|
with self.subTest(msg=msg, fen=fen):
|
|
cnt = ctypes.c_int(0)
|
|
moves = (Move * MAX_MOVES)()
|
|
b = Board()
|
|
|
|
self.load_fen(fen, board=b)
|
|
gen_bishop_moves(b, moves, ctypes.byref(cnt), captures_only)
|
|
|
|
self.assertEqual(expected, cnt.value)
|
|
|
|
cases_bishop_caps = [
|
|
("8/8/8/8/3B4/8/8/8 w - - 0 1", 0, "no enemies to capture"),
|
|
|
|
# first squares on all rays are enemies → 4 captures
|
|
("8/8/8/2p1p3/3B4/2p1p3/8/8 w - - 0 1", 4, "adjacent caps on c3,e3,c5,e5"),
|
|
|
|
# enemies at far ends on each ray → still 4 captures (one per ray)
|
|
("7p/p7/8/8/3B4/8/8/p5p1 w - - 0 1", 4, "targets h8,a7,g1,a1"),
|
|
|
|
# mixed: only two rays have a capture
|
|
("8/8/1p3P2/8/3B4/2P5/5p2/8 w - - 0 1", 2, "d4: NW capture b6, SE capture f2; NE/SW blocked by own"),
|
|
]
|
|
|
|
captures_only = True
|
|
for fen, expected, msg in cases_bishop_caps:
|
|
with self.subTest(msg=msg, fen=fen):
|
|
cnt = ctypes.c_int(0)
|
|
moves = (Move * MAX_MOVES)()
|
|
b = Board()
|
|
|
|
self.load_fen(fen, board=b)
|
|
gen_bishop_moves(b, moves, ctypes.byref(cnt), captures_only)
|
|
|
|
self.assertEqual(expected, cnt.value)
|
|
|
|
|
|
def test_rook_move_gen(self):
|
|
pass
|
|
|
|
|
|
def test_queen_move_gen(self):
|
|
pass
|
|
|
|
|
|
def test_king_move_gen(self):
|
|
pass
|
|
|
|
|
|
def test_quiet_pawn_pushes_white(self):
|
|
cases = [
|
|
("8/pppppppp/8/8/8/8/PPPPPPPP/8 w - - 0 1", 16, "open ranks"),
|
|
("8/8/8/8/8/8/P7/8 w - - 0 1", 2, "single pawn open"),
|
|
("8/8/8/8/8/p7/P7/8 w - - 0 1", 0, "blocked single enemy"),
|
|
("8/8/8/8/8/n7/P7/8 w - - 0 1", 0, "blocked single friendly"),
|
|
# Although legal move, we have a separate function that calculates this move type.
|
|
("8/9P/8/8/8/8/8/8 w - - 0 1", 0, "no promotion push"),
|
|
]
|
|
|
|
for fen, expected, msg in cases:
|
|
with self.subTest(msg=msg, fen=fen):
|
|
cnt = ctypes.c_int(0)
|
|
moves = (Move * MAX_MOVES)()
|
|
b = Board()
|
|
|
|
self.load_fen(fen, board=b)
|
|
gen_white_pawn_quiet_pushes(b, moves, ctypes.byref(cnt))
|
|
|
|
self.assertEqual(expected, cnt.value)
|
|
|
|
|
|
def test_quiet_pawn_promotions_white(self):
|
|
cases = [
|
|
("8/PPPPPPPP/8/8/8/8/8/8 w - - 0 1", 8, "all open on 7th rank"),
|
|
("8/8/7P/8/8/8/8/8 w - - 0 1", 0, "no promotions"),
|
|
("7n/7P/8/8/8/8/8/8 w - - 0 1", 0, "blocked by enemy"),
|
|
("7N/7P/8/8/8/8/8/8 w - - 0 1", 0, "blocked by friendly"),
|
|
]
|
|
|
|
for fen, expected, msg in cases:
|
|
with self.subTest(msg=msg, fen=fen):
|
|
cnt = ctypes.c_int(0)
|
|
moves = (Move * MAX_MOVES)()
|
|
b = Board()
|
|
|
|
self.load_fen(fen, board=b)
|
|
gen_white_pawn_push_promotions(b, moves, ctypes.byref(cnt))
|
|
|
|
self.assertEqual(expected, cnt.value)
|
|
|
|
|
|
def test_capture_pawn_promotions_white(self):
|
|
cases = [
|
|
("6n1/7P/8/8/8/8/8/8 w - - 0 1", 1, "one capture"),
|
|
("5n1n/6P1/8/8/8/8/8/8 w - - 0 1", 2, "two captures"),
|
|
("8/7P/8/8/8/8/8/8 w - - 0 1", 0, "no capture"),
|
|
]
|
|
|
|
for fen, expected, msg in cases:
|
|
with self.subTest(msg=msg, fen=fen):
|
|
cnt = ctypes.c_int(0)
|
|
moves = (Move * MAX_MOVES)()
|
|
b = Board()
|
|
|
|
self.load_fen(fen, board=b)
|
|
gen_white_pawn_capture_promotions(b, moves, ctypes.byref(cnt))
|
|
|
|
self.assertEqual(expected, cnt.value)
|
|
|
|
|
|
def test_capture_pawn_white(self):
|
|
cases = [
|
|
# normal diagonal captures
|
|
("8/8/8/3n4/4P3/8/8/8 w - - 0 1", 1, "e4xd5"),
|
|
("8/8/8/3n1n2/4P3/8/8/8 w - - 0 1", 2, "e4xd5 and e4xf5"),
|
|
("8/8/8/1p6/P7/8/8/8 w - - 0 1", 1, "edge file: a4xb5"),
|
|
("8/7P/8/8/8/8/8/8 w - - 0 1", 0, "no capture"),
|
|
|
|
# en passant
|
|
("8/8/8/3pP3/8/8/8/8 w - d6 0 1", 1, "EP available: e5xd6 e.p. (black pawn on d5)"),
|
|
("8/8/5n2/3pP3/8/8/8/8 w - d6 0 1", 2, "EP e5xd6 and normal e5xf6"),
|
|
("8/8/8/3p4/8/4P3/8/8 w - d6 0 1", 0, "EP square present but no white pawn can take"),
|
|
]
|
|
|
|
for fen, expected, msg in cases:
|
|
with self.subTest(msg=msg, fen=fen):
|
|
cnt = ctypes.c_int(0)
|
|
moves = (Move * MAX_MOVES)()
|
|
b = Board()
|
|
|
|
self.load_fen(fen, board=b)
|
|
gen_white_pawn_captures(b, moves, ctypes.byref(cnt))
|
|
|
|
self.assertEqual(expected, cnt.value) |