def test_white_rooks_cannot_move_if_piece_in_front(): # Arrange board = Board.empty() rook = Rook(Player.WHITE) rook_square = Square.at(4, 4) board.set_piece(rook_square, rook) obstructing_square1 = Square.at(3, 4) obstruction = Pawn(Player.WHITE) board.set_piece(obstructing_square1, obstruction) obstructing_square2 = Square.at(5, 4) obstruction = Pawn(Player.WHITE) board.set_piece(obstructing_square2, obstruction) obstructing_square3 = Square.at(4, 3) obstruction = Pawn(Player.WHITE) board.set_piece(obstructing_square3, obstruction) obstructing_square3 = Square.at(4, 5) obstruction = Pawn(Player.WHITE) board.set_piece(obstructing_square3, obstruction) # Act moves = rook.get_available_moves(board) # Assert assert len(moves) == 0
def test_en_passant(): # Arrange board = Board.empty() defending_pawn = Pawn(Player.WHITE) defending_pawn_square_1 = Square.at(1, 4) board.set_piece(defending_pawn_square_1, defending_pawn) attacking_pawn = Pawn(Player.BLACK) attacking_pawn_square_1 = Square.at(3, 5) board.set_piece(attacking_pawn_square_1, attacking_pawn) # Act defending_pawn_moves_1 = defending_pawn.get_available_moves(board) defending_pawn_square_2 = Square.at(3, 4) # Assert assert defending_pawn_square_2 in defending_pawn_moves_1 board.move_piece(defending_pawn_square_1, defending_pawn_square_2) attacking_pawn_moves_1 = attacking_pawn.get_available_moves(board) en_passant_square = Square.at(2, 4) assert en_passant_square in attacking_pawn_moves_1 board.move_piece(attacking_pawn_square_1, en_passant_square) assert board.get_piece(defending_pawn_square_2) is None
def test_black_rooks_can_take_white_pieces_but_not_black(): # Arrange board = Board.empty() rook = Rook(Player.BLACK) rook_square = Square.at(4, 4) board.set_piece(rook_square, rook) obstructing_square1 = Square.at(3, 4) obstruction = Pawn(Player.WHITE) board.set_piece(obstructing_square1, obstruction) obstructing_square2 = Square.at(5, 4) obstruction = Pawn(Player.BLACK) board.set_piece(obstructing_square2, obstruction) obstructing_square3 = Square.at(4, 3) obstruction = Pawn(Player.BLACK) board.set_piece(obstructing_square3, obstruction) obstructing_square3 = Square.at(4, 5) obstruction = Pawn(Player.WHITE) board.set_piece(obstructing_square3, obstruction) # Act moves = rook.get_available_moves(board) # Assert assert len(moves) == 2
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_white_knights_can_move_to_all_unoccupied_squares(): # Arrange board = Board.empty() knight = Knight(Player.WHITE) square = Square.at(1, 4) board.set_piece(square, knight) enemy1 = Pawn(Player.BLACK) enemy1_square = Square.at(0, 6) board.set_piece(enemy1_square, enemy1) enemy2 = Pawn(Player.WHITE) enemy2_square = Square.at(2, 6) board.set_piece(enemy2_square, enemy2) # Act moves = knight.get_available_moves(board) # Assert assert set(moves) == set([ Square.at(0, 6), Square.at(0, 2), Square.at(2, 2), Square.at(3, 3), Square.at(3, 5) ])
def _create_starting_board(): # Create an empty board board = [[None] * BOARD_SIZE for _ in range(BOARD_SIZE)] # Setup the rows of pawns board[1] = [Pawn(Player.WHITE) for _ in range(BOARD_SIZE)] board[6] = [Pawn(Player.BLACK) for _ in range(BOARD_SIZE)] # Setup the rows of pieces piece_row = [Rook, Knight, Bishop, Queen, King, Bishop, Knight, Rook] board[0] = list(map(lambda piece: piece(Player.WHITE), piece_row)) board[7] = list(map(lambda piece: piece(Player.BLACK), piece_row)) return board
def test_black_pawn_cannot_move_two_squares_if_piece_one_in_front(): # Arrange board = Board.empty() pawn = Pawn(Player.BLACK) pawn_square = Square.at(6, 4) board.set_piece(pawn_square, pawn) obstructing_square = Square.at(5, 4) obstruction = Pawn(Player.WHITE) board.set_piece(obstructing_square, obstruction) # Act moves = pawn.get_available_moves(board) # Assert assert Square.at(4, 4) not in moves
def test_white_pawn_cannot_move_if_piece_in_front(): # Arrange board = Board.empty() pawn = Pawn(Player.WHITE) pawn_square = Square.at(4, 4) board.set_piece(pawn_square, pawn) obstructing_square = Square.at(5, 4) obstruction = Pawn(Player.BLACK) board.set_piece(obstructing_square, obstruction) # Act moves = pawn.get_available_moves(board) # Assert assert len(moves) == 0
def test_white_queen_cannot_move_if_friendly_piece_in_the_way(): # Arrange board = Board.empty() queen = Queen(Player.WHITE) queen_square = Square.at(2, 2) board.set_piece(queen_square, queen) 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.WHITE) 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 = queen.get_available_moves(board) # Assert assert len(moves) == 0
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 testKnightJumping(): board = Board.empty() knight = Knight(Player.WHITE) square = Square.at(3, 5) board.set_piece(square, knight) for row_delta in range(-1, 1): for col_delta in range(-1, 1): if row_delta != 0 or col_delta != 0: board.set_piece( Square.at(square.row + row_delta, square.col + col_delta), Pawn(Player.WHITE)) moves = knight.get_available_moves(board) # Assert expected_moves = [ Square.at(4, 7), Square.at(4, 3), Square.at(5, 6), Square.at(5, 4), Square.at(2, 7), Square.at(2, 3), Square.at(1, 6), Square.at(1, 4) ] assert len(moves) == len(expected_moves) assert set(moves) == set(expected_moves)
def test_white_rook_cannot_move_up_if_pawn_is_adjacent(): # Arrange board = Board.empty() rook = Rook(Player.WHITE) square = Square.at(4, 4) board.set_piece(square, rook) obstructing_square = Square.at(5, 4) obstruction = Pawn(Player.WHITE) board.set_piece(obstructing_square, obstruction) # Act moves = rook.get_available_moves(board) # Assert assert Square.at(5, 4) not in moves @staticmethod def test_white_rook_can_capture(): # Arrange board = Board.empty() rook = Rook(Player.WHITE) square = Square.at(4, 4) board.set_piece(square, rook) obstructing_square = Square.at(5, 4) obstruction = Pawn(Player.BLACK) board.set_piece(obstructing_square, obstruction) # Act moves = rook.get_available_moves(board) # Assert assert Square.at(5, 4) in moves assert Square.at(6, 4) not in moves
def test_white_pawns_cannot_move_diagonally_except_to_capture(): # Arrange board = Board.empty() pawn = Pawn(Player.WHITE) pawn_square = Square.at(3, 4) board.set_piece(pawn_square, pawn) friendly = Pawn(Player.WHITE) friendly_square = Square.at(4, 5) board.set_piece(friendly_square, friendly) # Act moves = pawn.get_available_moves(board) # Assert assert Square.at(4, 3) not in moves assert Square.at(4, 5) not in moves
def test_black_pawns_can_capture_diagonally(): # Arrange board = Board.empty() pawn = Pawn(Player.BLACK) pawn_square = Square.at(3, 4) board.set_piece(pawn_square, pawn) friendly = Pawn(Player.BLACK) friendly_square = Square.at(2, 5) board.set_piece(friendly_square, friendly) # Act moves = pawn.get_available_moves(board) # Assert assert Square.at(2, 3) not in moves assert Square.at(2, 5) not in moves
def test_pawn_can_en_passant(): # Arrange board = Board.empty() pawn1 = Pawn(Player.WHITE) pawn1_square = Square.at(1, 4) board.set_piece(pawn1_square, pawn1) pawn1_to_square = Square.at(3, 4) pawn2 = Pawn(Player.BLACK) pawn2_square = Square.at(3, 5) board.set_piece(pawn2_square, pawn2) # Act board.move_piece(pawn1_square, pawn1_to_square) moves = pawn2.get_available_moves(board) # Assert assert Square.at(2, 4) in moves assert Square.at(2, 5) in moves assert Square.at(2, 6) not in moves
def test_knights_cannot_move_off_board(): # Arrange board = Board.empty() knight = Knight(Player.WHITE) knight_square = Square.at(0, 0) board.set_piece(knight_square, knight) friendly_1 = Pawn(Player.WHITE) friendly_1_square = Square.at(2, 1) board.set_piece(friendly_1_square, friendly_1) friendly_2 = Pawn(Player.WHITE) friendly_2_square = Square.at(1, 2) board.set_piece(friendly_2_square, friendly_2) # Act moves = knight.get_available_moves(board) # Assert assert len(moves) == 0
def test_black_pawn_can_be_captured_by_en_pessant(): # Arrange board = Board.empty() pawn1 = Pawn(Player.BLACK) # MAKE BLACK PAWN starting_square1 = Square.at(6, 4) board.set_piece(starting_square1, pawn1) pawn2 = Pawn(Player.WHITE) # MAKE WHITE PAWN starting_square2 = Square.at(4, 3) board.set_piece(starting_square2, pawn2) intermediate_square = Square.at(4, 4) # move black pawn pawn1.move_to(board, intermediate_square) # Act moves = pawn2.get_available_moves(board) # Assert assert Square.at(5, 4) 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_rooks_cannot_move_off_the_board(): # Arrange board = Board.empty() rook = Rook(Player.WHITE) rook_square = Square.at(0, 0) board.set_piece(rook_square, rook) 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) # Act moves = rook.get_available_moves(board) # Assert assert len(moves) == 0
def test_white_pawn_changes_to_queen_when_moved_to_top_of_board(): # Arrange board = Board.empty() pawn = Pawn(Player.WHITE) from_square = Square.at(6, 4) to_square = Square.at(7, 4) board.set_piece(from_square, pawn) board.move_piece(from_square, to_square) new_queen = board.get_piece(to_square) # Assert assert isinstance(new_queen, Queen)
def test_black_pawns_can_capture_diagonally(): # Arrange board = Board.empty() pawn = Pawn(Player.BLACK) pawn_square = Square.at(3, 4) board.set_piece(pawn_square, pawn) enemy1 = Pawn(Player.WHITE) enemy1_square = Square.at(2, 5) board.set_piece(enemy1_square, enemy1) enemy2 = Pawn(Player.WHITE) enemy2_square = Square.at(2, 3) board.set_piece(enemy2_square, enemy2) # Act moves = pawn.get_available_moves(board) # Assert assert enemy1_square in moves assert enemy2_square in moves
def test_black_pawn_can_move_down_two_squares_if_not_moved(): # Arrange board = Board.empty() pawn = Pawn(Player.BLACK) square = Square.at(6, 4) board.set_piece(square, pawn) # Act moves = pawn.get_available_moves(board) # Assert assert Square.at(4, 4) in moves
def test_black_pawn_changes_to_queen_when_moved_to_top_of_board(): # Arrange board = Board.empty() pawn = Pawn(Player.BLACK) square = Square.at(1, 4) board.set_piece(square, pawn) # Act moves = pawn.get_available_moves(board) # Assert assert len(moves) == 1
def test_white_pawn_cannot_move_at_top_of_board(): # Arrange board = Board.empty() pawn = Pawn(Player.WHITE) square = Square.at(7, 4) board.set_piece(square, pawn) # Act moves = pawn.get_available_moves(board) # Assert assert len(moves) == 0
def test_white_pawns_can_move_up_one_square(): # Arrange board = Board.empty() pawn = Pawn(Player.WHITE) square = Square.at(1, 4) board.set_piece(square, pawn) # Act moves = pawn.get_available_moves(board) # Assert assert Square.at(2, 4) in moves
def test_black_pawn_cannot_move_at_bottom_of_board(): # Arrange board = Board.empty() pawn = Pawn(Player.BLACK) square = Square.at(0, 4) board.set_piece(square, pawn) # Act moves = pawn.get_available_moves(board) # Assert assert len(moves) == 0
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_pawns_reaching_end_row_become_queens(): # Arrange board = Board.empty() pawn = Pawn(Player.WHITE) pawn_square = Square.at(6, 0) board.set_piece(pawn_square, pawn) target_square = Square.at(7, 0) # Act board.move_piece(pawn_square, target_square) # Assert assert isinstance(board.get_piece(target_square), Queen) is True
def test_black_pawns_reaching_end_row_become_queens(): # Arrange board = Board.empty() pawn = Pawn(Player.BLACK) pawn_square = Square.at(1, 0) board.set_piece(pawn_square, pawn) target_square = Square.at(0, 0) # Act board.current_player = board.current_player.opponent() board.move_piece(pawn_square, target_square) # Assert assert isinstance(board.get_piece(target_square), Queen) is True
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