def test_bishop_can_move_diagonally(): # Arrange board = Board.empty() bishop = Bishop(Player.WHITE) square = Square.at(3, 5) board.set_piece(square, bishop) # Act moves = bishop.get_available_moves(board) # Assert expected_moves = [ Square.at(0, 2), Square.at(1, 3), Square.at(2, 4), Square.at(4, 6), Square.at(5, 7), Square.at(1, 7), Square.at(2, 6), Square.at(4, 4), Square.at(5, 3), Square.at(6, 2), Square.at(7, 1) ] assert len(moves) == len(expected_moves) assert set(moves) == set(expected_moves)
def test_black_bishop_cannot_move_off_top_of_board(): # Arrange board = Board.empty() bishop = Bishop(Player.WHITE) square = Square.at(1, 7) board.set_piece(square, bishop) # Act moves = bishop.get_available_moves(board) # Assert assert Square.at(2, 8) not in moves assert Square.at(0, 8) not in moves
def test_bishop_can_take_opponent(): # Arrange board = Board.empty() bishop = Bishop(Player.WHITE) square = Square.at(4, 4) board.set_piece(square, bishop) board.set_piece(Square.at(6, 6), Bishop(Player.BLACK)) # Act moves = bishop.get_available_moves(board) # Assert assert Square.at(6, 6) in moves
def test_bishop_can_move_diagonally(): # Arrange board = Board.empty() bishop = Bishop(Player.WHITE) square = Square.at(4, 4) board.set_piece(square, bishop) # Act moves = bishop.get_available_moves(board) # Assert assert Square.at(6, 6) in moves
def test_bishop_cannot_move_off_board(): # Arrange board = Board.empty() bishop = Bishop(Player.BLACK) bishop_square = Square.at(4, 4) board.set_piece(bishop_square, bishop) # Act moves = bishop.get_available_moves(board) # Assert assert Square.at(0, 0) in moves assert Square.at(7, 7) in moves assert Square.at(8, 8) not in moves assert Square.at(-1, -1) not in moves
def test_bishop_can_move_down_and_right(): # Arrange board = Board.empty() bishop = Bishop(Player.WHITE) bishop_square = Square.at(4, 4) board.set_piece(bishop_square, bishop) # Act moves = bishop.get_available_moves(board) # Assert assert Square.at(3, 5) in moves assert Square.at(2, 6) in moves assert Square.at(1, 7) in moves
def test_bishop_can_move_up_and_left(): # Arrange board = Board.empty() bishop = Bishop(Player.WHITE) bishop_square = Square.at(4, 4) board.set_piece(bishop_square, bishop) # Act moves = bishop.get_available_moves(board) # Assert assert Square.at(5, 3) in moves assert Square.at(6, 2) in moves assert Square.at(7, 1) in moves
def test_bishop_can_move_until_blocked_by_teammate(): # Arrange board = Board.empty() bishop = Bishop(Player.WHITE) square = Square.at(4, 4) board.set_piece(square, bishop) board.set_piece(Square.at(6, 6), Bishop(Player.WHITE)) # Act moves = bishop.get_available_moves(board) # Assert assert Square.at(6, 6) not in moves
def test_bishop_cannot_move_through_pieces_down_and_left(): # Arrange board = Board.empty() bishop = Bishop(Player.WHITE) bishop_square = Square.at(4, 4) board.set_piece(bishop_square, bishop) friendly = Pawn(Player.WHITE) friendly_square = Square.at(3, 3) board.set_piece(friendly_square, friendly) # Act moves = bishop.get_available_moves(board) # Assert assert Square.at(2, 2) not in moves
def test_bishop_can_move_on_backslash(): # Arrange board = Board.empty() bishop = Bishop(Player.BLACK) bishop_square = Square.at(4, 4) board.set_piece(bishop_square, bishop) # Act moves = bishop.get_available_moves(board) # Assert assert Square.at(3, 5) in moves assert Square.at(2, 6) in moves assert Square.at(5, 3) in moves assert Square.at(6, 2) in moves
def test_black_bishop_can_move_diagonally(): # Arrange board = Board.empty() bishop = Bishop(Player.BLACK) bishop_square = Square.at(3, 3) board.set_piece(bishop_square, bishop) # Act moves = bishop.get_available_moves(board) # Assert assert Square.at(4, 4) in moves assert Square.at(2, 2) in moves assert Square.at(4, 2) in moves assert Square.at(2, 4) in moves
def test_bishop_can_capture_enemy_pieces(): # Arrange board = Board.empty() bishop = Bishop(Player.WHITE) square = Square.at(3, 5) board.set_piece(square, bishop) enemy = Pawn(Player.BLACK) enemy_square = Square.at(5, 7) board.set_piece(enemy_square, enemy) # Act moves = bishop.get_available_moves(board) # Assert assert enemy_square in moves
def test_bishop_cannot_move_off_board(): # Arrange board = Board.empty() bishop = Bishop(Player.WHITE) bishop_square = Square.at(0, 0) board.set_piece(bishop_square, bishop) friendly = Pawn(Player.WHITE) friendly_square = Square.at(1, 1) board.set_piece(friendly_square, friendly) # Act moves = bishop.get_available_moves(board) # Assert assert len(moves) == 0
def test_bishop_cannot_capture_friendly_pieces(): # Arrange board = Board.empty() bishop = Bishop(Player.WHITE) square = Square.at(3, 5) board.set_piece(square, bishop) friendly = Pawn(Player.WHITE) friendly_square = Square.at(5, 3) board.set_piece(friendly_square, friendly) # Act moves = bishop.get_available_moves(board) # Assert assert friendly_square not in moves
def test_bishop_is_blocked_by_enemy_pieces(): # Arrange board = Board.empty() bishop = Bishop(Player.WHITE) square = Square.at(3, 5) board.set_piece(square, bishop) enemy = Pawn(Player.BLACK) enemy_square = Square.at(5, 3) board.set_piece(enemy_square, enemy) # Act moves = bishop.get_available_moves(board) # Assert assert Square.at(6, 2) not in moves
def test_bishop_can_capture_down_and_left(): # Arrange board = Board.empty() bishop = Bishop(Player.WHITE) bishop_square = Square.at(4, 4) board.set_piece(bishop_square, bishop) enemy = Pawn(Player.BLACK) enemy_square = Square.at(2, 2) board.set_piece(enemy_square, enemy) # Act moves = bishop.get_available_moves(board) # Assert assert Square.at(2, 2) in moves
def test_bishop_is_blocked_by_friendly_pieces(): # Arrange board = Board.empty() bishop = Bishop(Player.WHITE) square = Square.at(3, 5) board.set_piece(square, bishop) friendly = Pawn(Player.WHITE) friendly_square = Square.at(5, 3) board.set_piece(friendly_square, friendly) # Act moves = bishop.get_available_moves(board) # Assert assert Square.at(6, 2) not in moves assert Square.at(7, 7) not in moves
def test_bishop_cannot_move_if_piece_in_front(): # Arrange board = Board.empty() bishop = Bishop(Player.WHITE) bishop_square = Square.at(4, 4) board.set_piece(bishop_square, bishop) enemy = Pawn(Player.BLACK) enemy_square = Square.at(5, 5) board.set_piece(enemy_square, enemy) # Act moves = bishop.get_available_moves(board) # Assert print(moves) assert Square.at(5, 5) in moves assert Square.at(6, 6) not in moves assert Square.at(7, 7) not in moves assert Square.at(2, 2) in moves
def test_white_bishops_moving_to_home_row_do_not_become_queens(): # Arrange board = Board.empty() bishop = Bishop(Player.WHITE) bishop_square = Square.at(1, 0) board.set_piece(bishop_square, bishop) target_square = Square.at(0, 1) # Act board.move_piece(bishop_square, target_square) # Assert assert isinstance(board.get_piece(target_square), Bishop) is True
def test_black_bishop_cannot_move_if_friendly_piece_in_the_way(): # Arrange board = Board.empty() bishop = Bishop(Player.BLACK) bishop_square = Square.at(2, 2) board.set_piece(bishop_square, bishop) obstructing_square1 = Square.at(1, 1) obstructing_square2 = Square.at(3, 3) obstructing_square3 = Square.at(1, 3) obstructing_square4 = Square.at(3, 1) obstruction = Pawn(Player.BLACK) board.set_piece(obstructing_square1, obstruction) board.set_piece(obstructing_square2, obstruction) board.set_piece(obstructing_square3, obstruction) board.set_piece(obstructing_square4, obstruction) # Act moves = bishop.get_available_moves(board) # Assert assert len(moves) == 0
def test_black_bishops_moving_to_home_row_do_not_become_queens(): # Arrange board = Board.empty() bishop = Bishop(Player.BLACK) bishop_square = Square.at(6, 0) board.set_piece(bishop_square, bishop) target_square = Square.at(7, 1) # Act board.current_player = board.current_player.opponent() board.move_piece(bishop_square, target_square) # Assert assert isinstance(board.get_piece(target_square), Bishop) is True
def handle_promotion(self, moving_piece, to_square): if isinstance(moving_piece, Pawn): if to_square.row == 0 or to_square.row == 7: piece = input( "Select piece to promote pawn to: 'Q', 'K', 'R' or 'B'.") if piece == 'Q': self.set_piece(to_square, Queen(self.current_player)) elif piece == 'K': self.set_piece(to_square, Knight(self.current_player)) elif piece == 'R': self.set_piece(to_square, Rook(self.current_player)) elif piece == 'B': self.set_piece(to_square, Bishop(self.current_player)) else: self.set_piece(to_square, Queen(self.current_player)) return
def test_white_king_checked_by_black_bishop(): # Arrange board = Board.empty() king = King(Player.WHITE) king_square = Square.at(2, 3) board.set_piece(king_square, king) enemy = Bishop(Player.BLACK) enemy_square = Square.at(1, 2) board.set_piece(enemy_square, enemy) # Act # check if current square is in check? check = king.is_in_check(board) # Assert assert check is True
def test_king_cannot_move_to_check_by_bishop(): # Arrange board = Board.empty() king = King(Player.WHITE) king_square = Square.at(2, 3) board.set_piece(king_square, king) enemy = Bishop(Player.BLACK) enemy_square = Square.at(1, 2) board.set_piece(enemy_square, enemy) # Act moves = king.get_available_moves(board) # Assert assert len(moves) == 7 assert Square.at(1, 2) in moves assert Square.at(2, 2) in moves assert Square.at(3, 2) in moves assert Square.at(3, 3) in moves assert Square.at(3, 4) not in moves assert Square.at(2, 4) in moves assert Square.at(1, 4) in moves assert Square.at(1, 3) in moves
def test_white_king_not_in_check(): # Arrange board = Board.empty() king = King(Player.WHITE) king_square = Square.at(2, 3) board.set_piece(king_square, king) enemy_pawn = Pawn(Player.BLACK) enemy_pawn_square = Square.at(3, 3) board.set_piece(enemy_pawn_square, enemy_pawn) enemy_rook = Rook(Player.BLACK) enemy_rook_square = Square.at(3, 4) board.set_piece(enemy_rook_square, enemy_rook) enemy_bishop = Bishop(Player.BLACK) enemy_bishop_square = Square.at(1, 3) board.set_piece(enemy_bishop_square, enemy_bishop) enemy_knight = Knight(Player.BLACK) enemy_knight_square = Square.at(7, 7) board.set_piece(enemy_knight_square, enemy_knight) enemy_queen = Queen(Player.BLACK) enemy_queen_square = Square.at(3, 0) board.set_piece(enemy_queen_square, enemy_queen) enemy_king = King(Player.BLACK) enemy_king_square = Square.at(6, 6) board.set_piece(enemy_king_square, enemy_king) # Act check = king.is_in_check(board) # Assert assert check is False