예제 #1
0
def test_unique_ship_representations():
    """ Tests battleship.ships.ShipFleet.unique_ship_representations method """
    fleet = ships.ShipFleet(config=pytest.battleship_test_config)
    unique_representation_characters = set()
    for ship_name, ship in fleet.items():
        unique_representation_characters.add(ship.representation)
    assert fleet.unique_ship_representations == unique_representation_characters
예제 #2
0
def test_print():
    """ Tests battleship.ships.ShipFleet._print method """
    fleet = ships.ShipFleet(config=pytest.battleship_test_config)
    repr_print_result = fleet._print(repr)
    str_print_result = fleet._print(str)
    assert isinstance(repr_print_result, str) and isinstance(str_print_result, str)
    assert repr(fleet) == repr_print_result and str(fleet) == str_print_result
예제 #3
0
def test_ship_is_hit():
    """ Tests battleship.ships.ShipFleet.ship_is_hit method """
    fleet = ships.ShipFleet(config=pytest.battleship_test_config)
    for ship_name, ship in fleet.items():
        _place_ship(ship)
        for coordinate in ship.coordinates:
            ship.take_damage(coordinate)
            assert fleet.ship_is_hit(coordinate)
예제 #4
0
def _initialize_resources():
    """ Initializes common Battleship player test resources """
    board_width, board_length = 20, 20
    # Initialize random ships
    game_board = game.BattleshipGame.construct_board(board_length, board_width)
    ship_fleet = ships.ShipFleet(pytest.battleship_test_config)
    # Create players
    players = [player.BattleshipPlayer(game_board) for _ in range(2)]
    players[0].computer = False
    players[1].computer = True
    human_player, computer_player = players[0], players[1]
    computer_player.place_ships(ship_fleet)
    return players, ship_fleet, game_board
예제 #5
0
def test_accept_attack():
    """ Tests battleship.ships.ShipFleet.accept_attack and is_destroyed methods """
    fleet = ships.ShipFleet(config=pytest.battleship_test_config)
    assert not fleet.is_destroyed()
    for ship_name, ship in fleet.items():
        _place_ship(ship)
        for coordinate in ship.coordinates:
            # Freshly placed ships will not have any destroyed coordinates
            assert coordinate not in ship.destroyed_coordinates
            fleet.accept_attack(coordinate)
            # After an attack on the fleet though there will be destroyed coordinates
            assert coordinate in ship.destroyed_coordinates
        assert len(ship.destroyed_coordinates) == len(ship.coordinates)
    # All coordinates were attacked above so the fleet should be destroyed now
    assert fleet.is_destroyed()
예제 #6
0
    def _initialize(self, initialization_object: dict = None) -> None:
        """ Initializes the game of battleship by choosing a human player, receiving input about the size of the
         board, creating each player's board, and placing each player's ships

        :param initialization_object - a dictionary containing starting parameters to run the game with
        """
        # Get input from the user if no initialization_object is provided
        initialization_object = self._get_user_input(initialization_object)
        board_width = initialization_object["board_width"]
        board_length = initialization_object["board_length"]
        human_player_involved = initialization_object.get(
            "human_player_involved", True)
        self.difficulty = initialization_object["difficulty"]
        # Create boards and fleets
        board_list = [
            self.construct_board(board_length, board_width)
            for _ in range(self.number_of_players)
        ]
        fleet_list = [
            ships.ShipFleet(self.config) for _ in range(self.number_of_players)
        ]
        # Create players and associate boards, fleets, and round information objects with each one
        for idx in range(self.number_of_players):
            player_obj = player.BattleshipPlayer(board_list[idx],
                                                 difficulty=self.difficulty)
            self.players.append(player_obj)
            player_obj.place_ships(fleet=fleet_list[idx], )
            self.boards[player_obj.player_id] = board_list[idx]
            self.ship_fleets[player_obj.player_id] = fleet_list[idx]
            self.hits_by_player[player_obj.player_id] = 0
            self.ships_destroyed_by_player[player_obj.player_id] = 0
            self.round_information[player_obj.player_id] = {
                "coordinate": None,
                "successful_hit": False,
                "ship_destroyed": False,
                "already_hit": False
            }
        # Assign a random player to be the human player unless specified otherwise in initialization input
        if human_player_involved:
            self._assign_human_player()