def print_2d_board( board: Board, width: int = 1, empty: str = ".", focus: str = "x", representation=None, focus_position: _Position = None ): assert board.dimension == 2 representation = LetterRepresentation if representation is None else representation if focus_position is not None: assert board.contains(focus_position) focus_color, focus_piece = board.get(focus_position) focus_next = focus_piece.movements(board, focus_position, focus_color, board.is_first_movement(focus_position)) else: focus_next = [] for i in range(board.size): for j in range(board.size): print( "{0:{width}}".format( focus if (i, j) in focus_next else representation[board.get((i, j))] if board.contains((i, j)) else empty, width=width ), end=" " ) print()
def test_contains(self): board = Board(4, 2) for i in range(4): for j in range(4): board.add(Piece, (i, j), Color.BLACK if i % 2 else Color.WHITE) for i in range(4): for j in range(4): self.assertTrue(board.contains((i, j)))
def test_get(self): board = Board(4, 2) for i in range(4): for j in range(4): board.add(Piece, (i, j), Color.BLACK if i % 2 else Color.WHITE) for i in range(4): for j in range(4): self.assertEqual(Color.BLACK if i % 2 else Color.WHITE, board.get((i, j))[0])
def test_promotion(self): board = Board(4, 2) board.add(Pawn, (0, 0), Color.WHITE) for i in range(3): board.move((0, i), (0, i + 1)) board.promote((0, 3), Queen) self.assertEqual(Queen, board.get((0, 3))[1])
def test_add(self): board = Board(4, 2) for i in range(4): for j in range(4): board.add(Piece, (i, j), Color.BLACK if i % 2 else Color.WHITE) self.assertEqual(len(board.board), 16) for i in range(4): for j in range(4): self.assertTrue((i, j) in board.board) self.assertEqual(Color.BLACK if i % 2 else Color.WHITE, board.board[(i, j)][0]) self.assertEqual(Piece, board.board[(i, j)][1])
def test_remove(self): board = Board(4, 2) board.add(Queen, (0, 0), Color.WHITE) board.add(Queen, (3, 3), Color.BLACK) self.assertEqual(2, len(board.board)) board.remove((3, 3)) self.assertEqual(1, len(board.board))
from Chess.Piece.King import King from Chess.Piece.Queen import Queen from Chess.Board import Board, Color from Chess.Piece.Pawn import Pawn from Chess.Utility import print_2d_board board = Board(4, 2) board.add(Pawn, (0, 0), Color.WHITE) board.add(Pawn, (1, 1), Color.BLACK) print_2d_board(board) print(f"White Pawn movements: {Pawn.movements(board, (0, 0))}") print(f"Black Pawn movements: {Pawn.movements(board, (1, 1))}")
def test_in_bounds(self): board = Board(4, 2) for i in range(4): for j in range(4): self.assertTrue(board.in_bounds((i, j)))