def __init__(self): board = [] for row_index in range(1, 9): board_row = [] for column_index in range(1, 9): cell = Cell(row_index, column_index) board_row.append(cell) if row_index == 2 or row_index == 7: is_white = row_index == 2 pawn = Pawn(is_white=is_white) cell.piece = pawn if row_index == 1 or row_index == 8: is_white = row_index == 1 board_row[0].piece = Rook(is_white=is_white) board_row[1].piece = Knight(is_white=is_white) board_row[2].piece = Bishop(is_white=is_white) board_row[3].piece = King(is_white=is_white) board_row[4].piece = Queen(is_white=is_white) board_row[5].piece = Bishop(is_white=is_white) board_row[6].piece = Knight(is_white=is_white) board_row[7].piece = Rook(is_white=is_white) board.append(board_row) self.board = board
def test_get_castle_params(self, check_mock, move_mock, undo_mock): """tests the get_castle_params method.""" check_mock.return_value = [] king = self.king board = self.board left_rook = Rook(ChessColor.WHITE) right_rook = Rook(ChessColor.WHITE) left_square = board.squares[0][0] right_square = board.squares[0][7] king_square = board.squares[0][4] king_square.piece = self.king left_target = board.squares[0][2] right_target = board.squares[0][6] # TODO: queenside castle tests, black king tests # should raise if king has moved king.move_count = 1 with self.assertRaises(InvalidMoveException): king.get_castle_params(king_square, right_target, board) king.move_count = 0 # should raise if no rook on end with self.assertRaises(InvalidMoveException): king.get_castle_params(king_square, right_target, board) right_square.piece = right_rook # should raise if targeted rook is opponent's right_rook.color = ChessColor.BLACK with self.assertRaises(InvalidMoveException): king.get_castle_params(king_square, right_target, board) right_rook.color = ChessColor.WHITE # should raise if piece in between rook and king other_piece = Piece(ChessColor.WHITE) board.squares[0][5].piece = other_piece with self.assertRaises(InvalidMoveException): king.get_castle_params(king_square, right_target, board) board.squares[0][5].piece = None # should raise if king is in check when move starts check_mock.return_value = ['something'] with self.assertRaises(InvalidMoveException): king.get_castle_params(king_square, right_target, board) check_mock.return_value = [] # should raise if king attempts to move through check move_mock.side_effect = InvalidMoveException('move exception') with self.assertRaises(InvalidMoveException): king.get_castle_params(king_square, right_target, board) move_mock.side_effect = None # should return the value otherwise expected_res = (king_square, right_target, None, None, MoveSideEffect.CASTLE) res = king.get_castle_params(king_square, right_target, board) self.assertEqual(res, expected_res)
def castle_test(): b = StandardBoard() k = King(ChessColor.WHITE) lr = Rook(ChessColor.WHITE) rr = Rook(ChessColor.WHITE) b.squares[0][0].piece = lr b.squares[0][7].piece = rr b.squares[0][4].piece = k print(b) print('Castling...') b.move_piece((0, 4), (0, 6), ChessColor.WHITE) print(b) print('Undoing castling...') b.undo_move() print(b)
def test_piece_init(): pawn = Pawn() rook = Rook(is_white=False) bishop = Bishop() knight = Knight() queen = Queen() king = King() assert pawn.color == 'white' assert rook.color == 'black' assert bishop.color == 'white' assert knight.color == 'white' assert queen.color == 'white' assert king.color == 'white'
def board(): board = Board() board.board[0][7].piece = None board.board[4][4].piece = Rook() board.board[7][1].piece = None board.board[7][2].piece = None board.board[7][4].piece = None board.board[7][5].piece = None board.board[7][6].piece = None """ board wr0 wh0 wb0 wk0 wq0 wb0 wh0 ### wp0 wp0 wp0 wp0 wp0 wp0 wp0 wp0 ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### wr0 ### ### ### ### ### ### ### ### ### ### ### bp0 bp0 bp0 bp0 bp0 bp0 bp0 bp0 br0 ### ### bk0 ### ### ### br0 """ return board
def _cell_factory(x: int, y: int, piece_str: str): is_white = True if piece_str[0] == 'w' else False piece_name = piece_str[1] has_moved = piece_str[2] == "1" piece = None if piece_name == 'p': piece = Pawn(is_white=is_white) elif piece_name == 'r': piece = Rook(is_white=is_white) elif piece_name == 'h': piece = Knight(is_white=is_white) elif piece_name == 'b': piece = Bishop(is_white=is_white) elif piece_name == 'k': piece = King(is_white=is_white) elif piece_name == 'q': piece = Queen(is_white=is_white) if piece: piece.has_moved = has_moved return Cell(x + 1, y + 1, piece, is_white=is_white)
def setUpClass(cls): cls.rook = Rook(ChessColor.BLACK)