Add python c binding class interface #29
@@ -132,7 +132,7 @@ class ChessFFI:
|
|||||||
|
|
||||||
|
|
||||||
def get_legal_moves(self, board, out):
|
def get_legal_moves(self, board, out):
|
||||||
return int(self._c_get_legal_moves(C.byref(board), out))
|
return int(self._c_get_legal_moves(board, out))
|
||||||
|
|
||||||
|
|
||||||
def square_attacked(self, board, sq, by):
|
def square_attacked(self, board, sq, by):
|
||||||
|
|||||||
@@ -1,19 +1,17 @@
|
|||||||
from test.base import ChessLibTestBase
|
from test.base import ChessLibTestBase
|
||||||
from test.chess_ffi import Move
|
from binding.python_c_ffi import Board
|
||||||
from test.chess_ffi import Board
|
from binding.python_c_ffi import Move
|
||||||
from test.chess_ffi import BLACK
|
from binding.python_c_ffi import BLACK
|
||||||
from test.chess_ffi import is_in_check
|
from binding.python_c_ffi import sq
|
||||||
from test.chess_ffi import gen_legal_moves
|
|
||||||
from test.chess_ffi import sq
|
|
||||||
|
|
||||||
MAX_MOVES = 256
|
MAX_MOVES = 256
|
||||||
|
|
||||||
|
|
||||||
class TestLegalMoveGen(ChessLibTestBase):
|
class TestLegalMoveGen(ChessLibTestBase):
|
||||||
def _gen_legal(self, board):
|
def _gen_legal(self, board):
|
||||||
"""Support either return-count or out-parameter signatures."""
|
|
||||||
moves = (Move * MAX_MOVES)()
|
moves = (Move * MAX_MOVES)()
|
||||||
n = gen_legal_moves(board, moves)
|
n = self.chess_ffi.get_legal_moves(board, moves)
|
||||||
return n, moves
|
return n, moves
|
||||||
|
|
||||||
|
|
||||||
@@ -22,7 +20,7 @@ class TestLegalMoveGen(ChessLibTestBase):
|
|||||||
# https://www.chessprogramming.org/Perft_Results#Initial_Position
|
# https://www.chessprogramming.org/Perft_Results#Initial_Position
|
||||||
fen = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"
|
fen = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"
|
||||||
b = Board()
|
b = Board()
|
||||||
self.load_fen(fen, board=b)
|
self.chess_ffi.load_fen(b, fen)
|
||||||
|
|
||||||
n, _ = self._gen_legal(b)
|
n, _ = self._gen_legal(b)
|
||||||
self.assertEqual(n, 20, "Start position must have 20 legal moves for White")
|
self.assertEqual(n, 20, "Start position must have 20 legal moves for White")
|
||||||
@@ -32,7 +30,8 @@ class TestLegalMoveGen(ChessLibTestBase):
|
|||||||
# Kiwipete: perft(1) = 48
|
# Kiwipete: perft(1) = 48
|
||||||
# https://www.chessprogramming.org/Perft_Results#Position_2
|
# https://www.chessprogramming.org/Perft_Results#Position_2
|
||||||
fen = "r3k2r/p1ppqpb1/bn2pnp1/3PN3/1p2P3/2N2Q1p/PPPBBPPP/R3K2R w KQkq - 0 1"
|
fen = "r3k2r/p1ppqpb1/bn2pnp1/3PN3/1p2P3/2N2Q1p/PPPBBPPP/R3K2R w KQkq - 0 1"
|
||||||
b = Board(); self.load_fen(fen, board=b)
|
b = Board()
|
||||||
|
self.chess_ffi.load_fen(b, fen)
|
||||||
|
|
||||||
n, moves = self._gen_legal(b)
|
n, moves = self._gen_legal(b)
|
||||||
self.assertEqual(n, 48, "Kiwipete perft(1) should be 48")
|
self.assertEqual(n, 48, "Kiwipete perft(1) should be 48")
|
||||||
@@ -41,7 +40,8 @@ class TestLegalMoveGen(ChessLibTestBase):
|
|||||||
def test_perft_position_3(self):
|
def test_perft_position_3(self):
|
||||||
# https://www.chessprogramming.org/Perft_Results#Position_3
|
# https://www.chessprogramming.org/Perft_Results#Position_3
|
||||||
fen = "8/2p5/3p4/KP5r/1R3p1k/8/4P1P1/8 w - - 0 1"
|
fen = "8/2p5/3p4/KP5r/1R3p1k/8/4P1P1/8 w - - 0 1"
|
||||||
b = Board(); self.load_fen(fen, board=b)
|
b = Board()
|
||||||
|
self.chess_ffi.load_fen(b, fen)
|
||||||
|
|
||||||
n, moves = self._gen_legal(b)
|
n, moves = self._gen_legal(b)
|
||||||
self.assertEqual(n, 14)
|
self.assertEqual(n, 14)
|
||||||
@@ -50,7 +50,8 @@ class TestLegalMoveGen(ChessLibTestBase):
|
|||||||
def test_perft_position_4(self):
|
def test_perft_position_4(self):
|
||||||
# https://www.chessprogramming.org/Perft_Results#Position_4
|
# https://www.chessprogramming.org/Perft_Results#Position_4
|
||||||
fen = "r3k2r/Pppp1ppp/1b3nbN/nP6/BBP1P3/q4N2/Pp1P2PP/R2Q1RK1 w kq - 0 1"
|
fen = "r3k2r/Pppp1ppp/1b3nbN/nP6/BBP1P3/q4N2/Pp1P2PP/R2Q1RK1 w kq - 0 1"
|
||||||
b = Board(); self.load_fen(fen, board=b)
|
b = Board()
|
||||||
|
self.chess_ffi.load_fen(b, fen)
|
||||||
|
|
||||||
n, moves = self._gen_legal(b)
|
n, moves = self._gen_legal(b)
|
||||||
self.assertEqual(n, 6)
|
self.assertEqual(n, 6)
|
||||||
@@ -59,7 +60,8 @@ class TestLegalMoveGen(ChessLibTestBase):
|
|||||||
def test_perft_position_5(self):
|
def test_perft_position_5(self):
|
||||||
# https://www.chessprogramming.org/Perft_Results#Position_5
|
# https://www.chessprogramming.org/Perft_Results#Position_5
|
||||||
fen = "rnbq1k1r/pp1Pbppp/2p5/8/2B5/8/PPP1NnPP/RNBQK2R w KQ - 1 8"
|
fen = "rnbq1k1r/pp1Pbppp/2p5/8/2B5/8/PPP1NnPP/RNBQK2R w KQ - 1 8"
|
||||||
b = Board(); self.load_fen(fen, board=b)
|
b = Board()
|
||||||
|
self.chess_ffi.load_fen(b, fen)
|
||||||
|
|
||||||
n, moves = self._gen_legal(b)
|
n, moves = self._gen_legal(b)
|
||||||
self.assertEqual(n, 44)
|
self.assertEqual(n, 44)
|
||||||
@@ -68,7 +70,8 @@ class TestLegalMoveGen(ChessLibTestBase):
|
|||||||
def test_perft_position_6(self):
|
def test_perft_position_6(self):
|
||||||
# https://www.chessprogramming.org/Perft_Results#Position_6
|
# https://www.chessprogramming.org/Perft_Results#Position_6
|
||||||
fen = "r4rk1/1pp1qppp/p1np1n2/2b1p1B1/2B1P1b1/P1NP1N2/1PP1QPPP/R4RK1 w - - 0 10"
|
fen = "r4rk1/1pp1qppp/p1np1n2/2b1p1B1/2B1P1b1/P1NP1N2/1PP1QPPP/R4RK1 w - - 0 10"
|
||||||
b = Board(); self.load_fen(fen, board=b)
|
b = Board()
|
||||||
|
self.chess_ffi.load_fen(b, fen)
|
||||||
|
|
||||||
n, moves = self._gen_legal(b)
|
n, moves = self._gen_legal(b)
|
||||||
self.assertEqual(n, 46)
|
self.assertEqual(n, 46)
|
||||||
@@ -78,32 +81,31 @@ class TestLegalMoveGen(ChessLibTestBase):
|
|||||||
# Classic stalemate: Black to move, no legal moves, not in check
|
# Classic stalemate: Black to move, no legal moves, not in check
|
||||||
fen = "7k/5Q2/6K1/8/8/8/8/8 b - - 0 1"
|
fen = "7k/5Q2/6K1/8/8/8/8/8 b - - 0 1"
|
||||||
b = Board()
|
b = Board()
|
||||||
self.load_fen(fen, board=b)
|
self.chess_ffi.load_fen(b, fen)
|
||||||
|
|
||||||
moves = (Move * MAX_MOVES)()
|
n, moves = self._gen_legal(b)
|
||||||
n = gen_legal_moves(b, moves)
|
|
||||||
|
|
||||||
self.assertEqual(n, 0, "Stalemate should have 0 legal 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")
|
self.assertFalse(self.chess_ffi.in_check(b, BLACK), "Side to move must not be in check in stalemate")
|
||||||
|
|
||||||
|
|
||||||
def test_simple_checkmate(self):
|
def test_simple_checkmate(self):
|
||||||
# Black to move; mated (Qg7# with Kg6 support)
|
# Black to move; mated (Qg7# with Kg6 support)
|
||||||
fen = "7k/6Q1/6K1/8/8/8/8/8 b - - 0 1"
|
fen = "7k/6Q1/6K1/8/8/8/8/8 b - - 0 1"
|
||||||
b = Board(); self.load_fen(fen, board=b)
|
b = Board()
|
||||||
moves = (Move * MAX_MOVES)()
|
self.chess_ffi.load_fen(b, fen)
|
||||||
n = gen_legal_moves(b, moves)
|
n, moves = self._gen_legal(b)
|
||||||
|
|
||||||
self.assertEqual(n, 0, "Checkmated side should have 0 legal 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")
|
self.assertTrue(self.chess_ffi.in_check(b, BLACK), "Mated side must be in check")
|
||||||
|
|
||||||
|
|
||||||
def test_pinned_piece_cannot_move(self):
|
def test_pinned_piece_cannot_move(self):
|
||||||
# White knight e2 pinned by rook e8 vs king e1
|
# White knight e2 pinned by rook e8 vs king e1
|
||||||
fen = "4r3/8/8/8/8/8/4N3/4K3 w - - 0 1"
|
fen = "4r3/8/8/8/8/8/4N3/4K3 w - - 0 1"
|
||||||
b = Board(); self.load_fen(fen, board=b)
|
b = Board()
|
||||||
moves = (Move * MAX_MOVES)()
|
self.chess_ffi.load_fen(b, fen)
|
||||||
n = gen_legal_moves(b, moves)
|
n, moves = self._gen_legal(b)
|
||||||
|
|
||||||
for move in moves:
|
for move in moves:
|
||||||
# Pinned knight cannot move from it's square.
|
# Pinned knight cannot move from it's square.
|
||||||
|
|||||||
Reference in New Issue
Block a user