Add king move gen and a test.c file for quick debug
All checks were successful
Python tests (make) / test (push) Successful in 10s
Python tests (make) / test (pull_request) Successful in 10s

This commit is contained in:
2025-08-17 20:27:03 -04:00
parent fd9af926e0
commit b811e33b81
3 changed files with 207 additions and 26 deletions

View File

@@ -7,8 +7,12 @@ 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 gen_rook_moves
from test.chess_ffi import gen_queen_moves
from test.chess_ffi import gen_king_moves
from test.chess_ffi import Move
from test.chess_ffi import Board
from test.chess_ffi import BLACK
from test.chess_ffi import WHITE
MAX_MOVES = 256
@@ -61,7 +65,6 @@ class TestPseudoMoveGeneration(ChessLibTestBase):
self.run_subtests(captures_only, gen_knight_moves, captures_only=True)
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"),
@@ -135,11 +138,105 @@ class TestPseudoMoveGeneration(ChessLibTestBase):
def test_queen_move_gen(self):
pass
cases_queen_all = [
("8/8/8/8/3Q4/8/8/8 w - - 0 1", 27, "center d4: rook(14)+bishop(13)"),
("8/8/8/8/Q7/8/8/8 w - - 0 1", 21, "edge a4: 14+7"),
("8/8/8/8/8/8/8/Q7 w - - 0 1", 21, "corner a1: 14+7"),
("8/8/8/8/8/8/8/1Q6 w - - 0 1", 21, "b1: 14+7"),
# all 8 adjacent squares are friendly ⇒ blocked immediately on all rays
("8/8/8/2PPP3/2PQP3/2PPP3/8/8 w - - 0 1", 0, "d4 with white pawns on c3,d3,e3,c4,e4,c5,d5,e5"),
# all 8 adjacent squares are enemies ⇒ exactly 8 captures (one per ray), no further squares
("8/8/8/2p1p3/2pQp3/2p1p3/8/8 w - - 0 1", 13, "d4 with black pawns adjacent on all rays"),
# only horizontal blocked by own pieces at c4/e4; vertical+diagonals open
("8/8/8/8/2PQP3/8/8/8 w - - 0 1", 20, "d4, own at c4,e4: rook loses 7→7 (up+down only), bishop 13 ⇒ 20"),
# two queens on a1 & h1 block each other along rank 1
("8/8/8/8/8/8/8/Q6Q w - - 0 1", 40, "a1 & h1: each 21→20 due to mutual block ⇒ 40 total"),
]
cases_queen_caps = [
("8/8/8/8/3Q4/8/8/8 w - - 0 1", 0, "no enemies to capture"),
# adjacent enemies only → 6 captures
("8/8/8/2p1p3/2pQp3/2p1p3/8/8 w - - 0 1", 6, "adjacent caps on all rays"),
# adjacent enemies only → 8 captures
("8/8/8/2ppp3/2pQp3/2ppp3/8/8 w - - 0 1", 8, "adjacent caps on all rays"),
# far targets on two rook rays (d8 and h4) + friendly blocking b1
("3r4/8/8/8/1P1Q3r/8/8/8 w - - 0 1", 2, "captures on d8 and h4; b4 friend blocks left; no other enemies"),
# mixed: captures on two diagonals only
("7p/p7/8/8/3Q4/8/8/p5p1 w - - 0 1", 4, "diagonal targets a1,a7,g1,h8 (4 captures)"),
]
self.run_subtests(cases_queen_all, gen_queen_moves, captures_only=False)
self.run_subtests(cases_queen_caps, gen_queen_moves, captures_only=True)
def test_king_move_gen(self):
pass
cases_king_all = [
("8/8/8/8/3K4/8/8/8 w - - 0 1", 8, "center d4: 8 moves"),
("8/8/8/8/K7/8/8/8 w - - 0 1", 5, "edge a4: 5 moves"),
("8/8/8/8/8/8/8/K7 w - - 0 1", 3, "corner a1: 3 moves"),
# all eight adjacent squares are WHITE pieces → 0
("8/8/8/2PPP3/2PKP3/2PPP3/8/8 w - - 0 1", 0, "surrounded by own"),
# all eight adjacent squares are BLACK pieces → 8 (all captures)
("8/8/8/2ppp3/2pKp3/2ppp3/8/8 w - - 0 1", 8, "surrounded by enemies"),
# mixed: own block east/west; captures NE & SE; rest quiet
("8/8/8/2p5/2PKP3/4p3/8/8 w - - 0 1", 6, "d4: caps c5,e3; quiet d5,d3,c3,e5"),
]
cases_king_caps = [
("8/8/8/8/3K4/8/8/8 w - - 0 1", 0, "no enemies"),
# eight adjacent enemies → 8 captures
("8/8/8/2ppp3/2pKp3/2ppp3/8/8 w - - 0 1", 8, "all adjacent caps"),
# edge a1 with only b2 enemy → 1 capture
("8/8/8/8/8/8/1p6/K7 w - - 0 1", 1, "a1: only Kxb2"),
# mixed: same as above mixed case → 2 captures
("8/8/8/2p5/2PKP3/4p3/8/8 w - - 0 1", 2, "captures c5 & e3"),
]
cases_king_castle_white = [
("8/8/8/8/8/8/8/R3K2R w KQ - 0 1", 7, "e1 with KQ rights, empty lanes: 5 king steps + O-O + O-O-O"),
("8/8/8/8/8/8/8/4K2R w K - 0 1", 6, "e1 with K only: 5 steps + O-O"),
("8/8/8/8/8/8/8/R3K3 w Q - 0 1", 6, "e1 with Q only: 5 steps + O-O-O"),
# blocked kingside (f1 occupied) → only O-O-O available
("8/8/8/8/8/8/8/R3KB1R w KQ - 0 1", 5, "f1 blocked by own piece; queen-side castle only"),
# blocked queenside (d1 occupied) → only O-O available
("8/8/8/8/8/8/8/R2BK2R w KQ - 0 1", 5, "d1 blocked by own piece; king-side castle only"),
# both sides blocked (d1 and f1 occupied) → no castles, just 5 normal moves
("8/8/8/8/8/8/8/R2BKB1R w KQ - 0 1", 3, "both lanes blocked; no castles"),
]
cases_king_castle_black = [
("r3k2r/8/8/8/8/8/8/8 b kq - 0 1", 7, "e8 with kq rights, empty lanes: 5 steps + O-O + O-O-O"),
("4k2r/8/8/8/8/8/8/8 b k - 0 1", 6, "e8 with k only: 5 steps + O-O"),
("r3k3/8/8/8/8/8/8/8 b q - 0 1", 6, "e8 with q only: 5 steps + O-O-O"),
# blocked kingside (f8 occupied) → only O-O-O available
("r3kb1r/8/8/8/8/8/8/8 b kq - 0 1", 5, "f8 blocked; only queen-side castle"),
# blocked queenside (d8 occupied) → only O-O available
("r2bk2r/8/8/8/8/8/8/8 b kq - 0 1", 5, "d8 blocked; only king-side castle"),
]
self.run_subtests(cases_king_all, gen_king_moves, captures_only=False)
self.run_subtests(cases_king_castle_white, gen_king_moves, captures_only=False)
self.run_subtests(cases_king_caps, gen_king_moves, captures_only=True)
self.run_subtests(cases_king_castle_black, gen_king_moves, captures_only=False)
def test_quiet_pawn_pushes_white(self):