8-add-legal-move-filter (#16)
All checks were successful
Python tests (make) / test (push) Successful in 11s
All checks were successful
Python tests (make) / test (push) Successful in 11s
works on #8 Reviewed-on: #16 Co-authored-by: Josh <josh@joshuaschuett.com> Co-committed-by: Josh <josh@joshuaschuett.com>
This commit is contained in:
@@ -91,10 +91,12 @@ gen_king_moves = _bind_opt("gen_king_moves", *PIECE_SIG)
|
||||
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)
|
||||
GEN_LEGAL_MOVES = (C.POINTER(Board), C.POINTER(Move))
|
||||
|
||||
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)
|
||||
get_legal_moves = _bind_opt("get_legal_moves", GEN_LEGAL_MOVES, C.c_int)
|
||||
|
||||
|
||||
# Attack cache tables.
|
||||
@@ -126,6 +128,10 @@ def gen_moves(board, captures_only=False, cap=256):
|
||||
return buf, n
|
||||
|
||||
|
||||
def gen_legal_moves(board, out):
|
||||
return int(get_legal_moves(C.byref(board), out))
|
||||
|
||||
|
||||
def is_square_attacked(board, sq, by):
|
||||
return bool(square_attacked(C.byref(board), int(sq), int(by)))
|
||||
|
||||
|
||||
110
test/test_legal_move_gen.py
Normal file
110
test/test_legal_move_gen.py
Normal file
@@ -0,0 +1,110 @@
|
||||
from test.base import ChessLibTestBase
|
||||
from test.chess_ffi import Move
|
||||
from test.chess_ffi import Board
|
||||
from test.chess_ffi import BLACK
|
||||
from test.chess_ffi import is_in_check
|
||||
from test.chess_ffi import gen_legal_moves
|
||||
from test.chess_ffi import sq
|
||||
|
||||
MAX_MOVES = 256
|
||||
|
||||
|
||||
class TestLegalMoveGen(ChessLibTestBase):
|
||||
def _gen_legal(self, board):
|
||||
"""Support either return-count or out-parameter signatures."""
|
||||
moves = (Move * MAX_MOVES)()
|
||||
n = gen_legal_moves(board, moves)
|
||||
return n, moves
|
||||
|
||||
|
||||
def test_start_position(self):
|
||||
# 20 legal moves from the initial chess position
|
||||
# https://www.chessprogramming.org/Perft_Results#Initial_Position
|
||||
fen = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"
|
||||
b = Board()
|
||||
self.load_fen(fen, board=b)
|
||||
|
||||
n, _ = self._gen_legal(b)
|
||||
self.assertEqual(n, 20, "Start position must have 20 legal moves for White")
|
||||
|
||||
|
||||
def test_kiwipete_depth1_count(self):
|
||||
# Kiwipete: perft(1) = 48
|
||||
# https://www.chessprogramming.org/Perft_Results#Position_2
|
||||
fen = "r3k2r/p1ppqpb1/bn2pnp1/3PN3/1p2P3/2N2Q1p/PPPBBPPP/R3K2R w KQkq - 0 1"
|
||||
b = Board(); self.load_fen(fen, board=b)
|
||||
|
||||
n, moves = self._gen_legal(b)
|
||||
self.assertEqual(n, 48, "Kiwipete perft(1) should be 48")
|
||||
|
||||
|
||||
def test_perft_position_3(self):
|
||||
# https://www.chessprogramming.org/Perft_Results#Position_3
|
||||
fen = "8/2p5/3p4/KP5r/1R3p1k/8/4P1P1/8 w - - 0 1"
|
||||
b = Board(); self.load_fen(fen, board=b)
|
||||
|
||||
n, moves = self._gen_legal(b)
|
||||
self.assertEqual(n, 14)
|
||||
|
||||
|
||||
def test_perft_position_4(self):
|
||||
# https://www.chessprogramming.org/Perft_Results#Position_4
|
||||
fen = "r3k2r/Pppp1ppp/1b3nbN/nP6/BBP1P3/q4N2/Pp1P2PP/R2Q1RK1 w kq - 0 1"
|
||||
b = Board(); self.load_fen(fen, board=b)
|
||||
|
||||
n, moves = self._gen_legal(b)
|
||||
self.assertEqual(n, 6)
|
||||
|
||||
|
||||
def test_perft_position_5(self):
|
||||
# https://www.chessprogramming.org/Perft_Results#Position_5
|
||||
fen = "rnbq1k1r/pp1Pbppp/2p5/8/2B5/8/PPP1NnPP/RNBQK2R w KQ - 1 8"
|
||||
b = Board(); self.load_fen(fen, board=b)
|
||||
|
||||
n, moves = self._gen_legal(b)
|
||||
self.assertEqual(n, 44)
|
||||
|
||||
|
||||
def test_perft_position_6(self):
|
||||
# https://www.chessprogramming.org/Perft_Results#Position_6
|
||||
fen = "r4rk1/1pp1qppp/p1np1n2/2b1p1B1/2B1P1b1/P1NP1N2/1PP1QPPP/R4RK1 w - - 0 10"
|
||||
b = Board(); self.load_fen(fen, board=b)
|
||||
|
||||
n, moves = self._gen_legal(b)
|
||||
self.assertEqual(n, 46)
|
||||
|
||||
|
||||
def test_stalemate_black_to_move(self):
|
||||
# Classic stalemate: Black to move, no legal moves, not in check
|
||||
fen = "7k/5Q2/6K1/8/8/8/8/8 b - - 0 1"
|
||||
b = Board()
|
||||
self.load_fen(fen, board=b)
|
||||
|
||||
moves = (Move * MAX_MOVES)()
|
||||
n = gen_legal_moves(b, moves)
|
||||
|
||||
self.assertEqual(n, 0, "Stalemate should have 0 legal moves")
|
||||
self.assertFalse(is_in_check(b, BLACK), "Side to move must not be in check in stalemate")
|
||||
|
||||
|
||||
def test_simple_checkmate(self):
|
||||
# Black to move; mated (Qg7# with Kg6 support)
|
||||
fen = "7k/6Q1/6K1/8/8/8/8/8 b - - 0 1"
|
||||
b = Board(); self.load_fen(fen, board=b)
|
||||
moves = (Move * MAX_MOVES)()
|
||||
n = gen_legal_moves(b, moves)
|
||||
|
||||
self.assertEqual(n, 0, "Checkmated side should have 0 legal moves")
|
||||
self.assertTrue(is_in_check(b, BLACK), "Mated side must be in check")
|
||||
|
||||
|
||||
def test_pinned_piece_cannot_move(self):
|
||||
# White knight e2 pinned by rook e8 vs king e1
|
||||
fen = "4r3/8/8/8/8/8/4N3/4K3 w - - 0 1"
|
||||
b = Board(); self.load_fen(fen, board=b)
|
||||
moves = (Move * MAX_MOVES)()
|
||||
n = gen_legal_moves(b, moves)
|
||||
|
||||
for move in moves:
|
||||
# Pinned knight cannot move from it's square.
|
||||
self.assertFalse(getattr(move, "from") == sq("e2"))
|
||||
@@ -253,7 +253,7 @@ class TestPseudoMoveGeneration(ChessLibTestBase):
|
||||
|
||||
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/PPPPPPPP/8/8/8/8/8/8 w - - 0 1", 32, "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"),
|
||||
@@ -263,8 +263,8 @@ class TestPseudoMoveGeneration(ChessLibTestBase):
|
||||
|
||||
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"),
|
||||
("6n1/7P/8/8/8/8/8/8 w - - 0 1", 4, "one capture"),
|
||||
("5n1n/6P1/8/8/8/8/8/8 w - - 0 1", 8, "two captures"),
|
||||
("8/7P/8/8/8/8/8/8 w - - 0 1", 0, "no capture"),
|
||||
]
|
||||
self.run_subtests(cases, gen_white_pawn_capture_promotions)
|
||||
|
||||
Reference in New Issue
Block a user