def test_king_cannot_move_if_piece_in_front(): # Arrange board = Board.empty() king = King(Player.WHITE) king_square = Square.at(4, 4) board.set_piece(king_square, king) friend = Pawn(Player.WHITE) friend_square = Square.at(5, 5) board.set_piece(friend_square, friend) friend2 = Pawn(Player.WHITE) friendly_square2 = Square.at(3, 4) board.set_piece(friendly_square2, friend2) enemy = Pawn(Player.BLACK) enemy_square = Square.at(5, 4) board.set_piece(enemy_square, enemy) # Act moves = king.get_available_moves(board) # Assert print(moves) assert Square.at(5, 5) not in moves assert Square.at(3, 4) not in moves assert Square.at(5, 4) in moves
def test_king_cannot_move_to_check_by_queen(): # Arrange board = Board.empty() king = King(Player.WHITE) king_square = Square.at(2, 3) board.set_piece(king_square, king) enemy = Queen(Player.BLACK) enemy_square = Square.at(1, 2) board.set_piece(enemy_square, enemy) # Act moves = king.get_available_moves(board) # check if current square is in check? # checked_by = king.get_checked_by(board) # Assert assert Square.at(1, 2) in moves assert Square.at(2, 2) not in moves assert Square.at(3, 2) not 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) not in moves assert Square.at(1, 3) not in moves assert len(moves) == 3
def test_black_king_cannot_move_if_friendly_piece_in_the_way(): # Arrange board = Board.empty() king = King(Player.BLACK) king_square = Square.at(2, 2) board.set_piece(king_square, king) obstructing_square1 = Square.at(1, 1) obstructing_square2 = Square.at(3, 3) obstructing_square3 = Square.at(1, 3) obstructing_square4 = Square.at(3, 1) obstructing_square5 = Square.at(2, 1) obstructing_square6 = Square.at(2, 3) obstructing_square7 = Square.at(1, 2) obstructing_square8 = Square.at(3, 2) 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) board.set_piece(obstructing_square5, obstruction) board.set_piece(obstructing_square6, obstruction) board.set_piece(obstructing_square7, obstruction) board.set_piece(obstructing_square8, obstruction) # Act moves = king.get_available_moves(board) # Assert assert len(moves) == 0
def test_kings_can_move_one_step_up_and_left(): # Arrange board = Board.empty() king = King(Player.WHITE) king_square = Square.at(4, 4) board.set_piece(king_square, king) # Act moves = king.get_available_moves(board) # Assert assert Square.at(5, 3) in moves
def test_white_king_can_move_diagonally(): # Arrange board = Board.empty() king = King(Player.BLACK) square = Square.at(0, 4) board.set_piece(square, king) # Act moves = king.get_available_moves(board) # Assert assert Square.at(1, 3) in moves
def test_black_king_can_move_right_one_square(): # Arrange board = Board.empty() king = King(Player.BLACK) square = Square.at(5, 3) board.set_piece(square, king) # Act moves = king.get_available_moves(board) # Assert assert Square.at(5, 2) in moves
def test_white_king_can_move_right_one_square(): # Arrange board = Board.empty() king = King(Player.WHITE) square = Square.at(3, 4) board.set_piece(square, king) # Act moves = king.get_available_moves(board) # Assert assert Square.at(3, 5) in moves
def test_king_cannot_move_off_board_up(): # Arrange board = Board.empty() king = King(Player.BLACK) king_square = Square.at(7, 7) board.set_piece(king_square, king) # Act moves = king.get_available_moves(board) # Assert assert Square.at(7, 6) in moves assert Square.at(8, 8) not in moves
def test_king_cannot_move_off_board_down(): # Arrange board = Board.empty() king = King(Player.WHITE) king_square = Square.at(0, 0) board.set_piece(king_square, king) # Act moves = king.get_available_moves(board) # Assert assert Square.at(-1, -1) not in moves assert Square.at(0, 1) in moves
def test_black_king_cannot_move_off_top_of_board(): # Arrange board = Board.empty() king = King(Player.BLACK) square = Square.at(1, 7) board.set_piece(square, king) # Act moves = king.get_available_moves(board) # Assert assert Square.at(2, 8) not in moves assert Square.at(0, 8) not in moves assert Square.at(1, 8) not in moves
def test_white_king_can_move_diagonally(): # Arrange board = Board.empty() king = King(Player.WHITE) king_square = Square.at(3, 3) board.set_piece(king_square, king) # Act moves = king.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_king_can_move_1_backslash(): # Arrange board = Board.empty() king = King(Player.BLACK) king_square = Square.at(4, 4) board.set_piece(king_square, king) # Act moves = king.get_available_moves(board) # Assert assert Square.at(3, 5) in moves assert Square.at(2, 6) not in moves assert Square.at(5, 3) in moves assert Square.at(6, 2) not in moves
def test_black_king_can_move_vertically_and_horizontally(): # Arrange board = Board.empty() king = King(Player.BLACK) king_square = Square.at(3, 3) board.set_piece(king_square, king) # Act moves = king.get_available_moves(board) # Assert assert Square.at(3, 4) in moves assert Square.at(4, 3) in moves assert Square.at(3, 2) in moves assert Square.at(2, 3) in moves
def test_kings_cannot_move_to_friendly_square(): # Arrange board = Board.empty() king = King(Player.WHITE) king_square = Square.at(4, 4) board.set_piece(king_square, king) friendly_1 = Pawn(Player.WHITE) friendly_1_square = Square.at(4, 3) board.set_piece(friendly_1_square, friendly_1) # Act moves = king.get_available_moves(board) # Assert assert Square.at(4, 3) not in moves
def test_white_king_checked_by_black_queen_diagonal(): # Arrange board = Board.empty() king = King(Player.WHITE) king_square = Square.at(2, 3) board.set_piece(king_square, king) enemy = Queen(Player.BLACK) enemy_square = Square.at(1, 2) board.set_piece(enemy_square, enemy) # Act check = king.is_in_check(board) # Assert assert check is True
def test_white_kings_can_move_to_all_neighbouring_squares_inside_board(): # Arrange board = Board.empty() king = King(Player.WHITE) square = Square.at(7, 0) board.set_piece(square, king) # Act moves = king.get_available_moves(board) # Assert assert set(moves) == set( [Square.at(6, 0), Square.at(6, 1), Square.at(7, 1)])
def test_king_can_take_enemy_pieces(): # Arrange board = Board.empty() king = King(Player.WHITE) square = Square.at(4,4) pawn1 = Pawn(Player.BLACK) pawn2 = Pawn(Player.BLACK) pawn3 = Pawn(Player.BLACK) pawn4 = Pawn(Player.BLACK) pawn5 = Pawn(Player.BLACK) pawn6 = Pawn(Player.BLACK) pawn7 = Pawn(Player.BLACK) pawn8 = Pawn(Player.BLACK) square1 = Square.at(3, 3) square2 = Square.at(3, 4) square3 = Square.at(3, 5) square4 = Square.at(4, 3) square5 = Square.at(4, 5) square6 = Square.at(5, 3) square7 = Square.at(5, 4) square8 = Square.at(5, 5) board.set_piece(square, king) board.set_piece(square1, pawn1) board.set_piece(square2, pawn2) board.set_piece(square3, pawn3) board.set_piece(square4, pawn4) board.set_piece(square5, pawn5) board.set_piece(square6, pawn6) board.set_piece(square7, pawn7) board.set_piece(square8, pawn8) # Act moves = king.get_available_moves(board) # Assert assert Square.at(3, 3) in moves assert Square.at(3, 4) in moves assert Square.at(3, 5) in moves assert Square.at(4, 3) in moves assert Square.at(4, 5) in moves assert Square.at(5, 3) in moves assert Square.at(5, 4) in moves assert Square.at(5, 5) in moves
def test_white_king_checked_by_black_knight(): # Arrange board = Board.empty() king = King(Player.WHITE) king_square = Square.at(2, 3) board.set_piece(king_square, king) enemy = Knight(Player.BLACK) enemy_square = Square.at(1, 1) 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_black_king_can_castle(): # Arrange board = Board.empty() king = King(Player.BLACK) square = Square.at(7, 4) board.set_piece(square, king) rook1 = Rook(Player.BLACK) board.set_piece(Square.at(7, 0), rook1) rook2 = Rook(Player.BLACK) board.set_piece(Square.at(7, 7), rook2) # Act moves = king.get_available_moves(board) # Assert assert Square.at(7, 2) in moves assert Square.at(7, 6) in moves
def test_white_kings_can_move_to_unoccupied_neighbouring_squares_inside_board( ): # Arrange board = Board.empty() king = King(Player.WHITE) square = Square.at(7, 0) board.set_piece(square, king) enemy1 = Pawn(Player.BLACK) enemy1_square = Square.at(6, 0) board.set_piece(enemy1_square, enemy1) enemy2 = Pawn(Player.WHITE) enemy2_square = Square.at(6, 1) board.set_piece(enemy2_square, enemy2) # Act moves = king.get_available_moves(board) # Assert assert set(moves) == set([Square.at(6, 0), Square.at(7, 1)])
def test_king_cannot_move_off_the_left_of_board(): # Arrange board = Board.empty() king = King(Player.BLACK) square = Square.at(4,0) board.set_piece(square, king) # Act moves = king.get_available_moves(board) # Assert assert Square.at(3, 1) in moves assert Square.at(4, 1) in moves assert Square.at(5, 1) in moves assert Square.at(3, 0) in moves assert Square.at(5, 0) in moves assert Square.at(3, -1) not in moves assert Square.at(4, -1) not in moves assert Square.at(5, -1) not in moves
def test_king_cannot_move_off_the_bottom_of_board(): # Arrange board = Board.empty() king = King(Player.BLACK) square = Square.at(0,4) board.set_piece(square, king) # Act moves = king.get_available_moves(board) # Assert assert Square.at(1, 3) in moves assert Square.at(1, 4) in moves assert Square.at(1, 5) in moves assert Square.at(0, 3) in moves assert Square.at(0, 5) in moves assert Square.at(-1, 3) not in moves assert Square.at(-1, 4) not in moves assert Square.at(-1, 5) not in moves
def test_king_cannot_move_off_the_right_of_board(): # Arrange board = Board.empty() king = King(Player.BLACK) square = Square.at(4,7) board.set_piece(square, king) # Act moves = king.get_available_moves(board) # Assert assert Square.at(3, 6) in moves assert Square.at(4, 6) in moves assert Square.at(5, 6) in moves assert Square.at(3, 7) in moves assert Square.at(5, 7) in moves assert Square.at(3, 8) not in moves assert Square.at(4, 8) not in moves assert Square.at(5, 8) not in moves
def test_king_can_castle(): # Arrange board = Board.empty() king = King(Player.WHITE) king_square = Square.at(0, 4) board.set_piece(king_square, king) friend1 = Rook(Player.WHITE) friend_square1 = Square.at(0, 7) board.set_piece(friend_square1, friend1) friend2 = Rook(Player.WHITE) friend_square2 = Square.at(0, 0) board.set_piece(friend_square2, friend2) # Act moves = king.get_available_moves(board) # Assert assert Square.at(0, 2) in moves assert Square.at(0, 6) in moves
def test_kings_can_capture(): # Arrange board = Board.empty() king = King(Player.WHITE) king_square = Square.at(4, 4) board.set_piece(king_square, king) enemy_1 = Pawn(Player.BLACK) enemy_1_square = Square.at(4, 3) board.set_piece(enemy_1_square, enemy_1) enemy_2 = Pawn(Player.BLACK) enemy_2_square = Square.at(3, 4) board.set_piece(enemy_2_square, enemy_2) # Act moves = king.get_available_moves(board) # Assert assert Square.at(4, 3) in moves assert Square.at(3, 4) in moves
def test_white_kings_can_move_to_all_neighbouring_squares(): # Arrange board = Board.empty() king = King(Player.WHITE) square = Square.at(1, 4) board.set_piece(square, king) # Act moves = king.get_available_moves(board) # Assert assert set(moves) == set([ Square.at(1, 5), Square.at(1, 3), Square.at(2, 5), Square.at(2, 3), Square.at(2, 4), Square.at(0, 5), Square.at(0, 3), Square.at(0, 4) ])
def test_white_kings_moving_to_home_row_do_not_become_queens(): # Arrange board = Board.empty() king = King(Player.WHITE) king_square = Square.at(1, 0) board.set_piece(king_square, king) target_square = Square.at(0, 0) # Act board.move_piece(king_square, target_square) # Assert assert isinstance(board.get_piece(target_square), King) is True
def test_black_kings_moving_to_home_row_do_not_become_queens(): # Arrange board = Board.empty() king = King(Player.BLACK) king_square = Square.at(6, 0) board.set_piece(king_square, king) target_square = Square.at(7, 0) # Act board.current_player = board.current_player.opponent() board.move_piece(king_square, target_square) # Assert assert isinstance(board.get_piece(target_square), King) is True
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
def test_kings_cannot_move_off_the_board(): # Arrange board = Board.empty() king = King(Player.WHITE) king_square = Square.at(0, 0) board.set_piece(king_square, king) friendly_1 = Pawn(Player.WHITE) friendly_1_square = Square.at(1, 0) board.set_piece(friendly_1_square, friendly_1) friendly_2 = Pawn(Player.WHITE) friendly_2_square = Square.at(0, 1) board.set_piece(friendly_2_square, friendly_2) friendly_3 = Pawn(Player.WHITE) friendly_3_square = Square.at(1, 1) board.set_piece(friendly_3_square, friendly_3) # Act moves = king.get_available_moves(board) # Assert assert len(moves) == 0