示例#1
0
    def mark_subfleet_of_biggest_remaining_ships(self) -> None:
        """Determine the size of the largest ship remaining in the
        Puzzle fleet and mark the entire subfleet of those ships onto
        the Puzzle board.

        If the board offers more available "slots" in which all subfleet
        ships can be marked, then branch the puzzle solving by marking
        the subfleet in each possible slot combination.

        If no ships are remaining in the fleet, that means a new puzzle
        solution has been found.

        """
        if self.fleet.has_ships_remaining():
            ship_size = self.fleet.longest_ship_size
            max_possible_subfleet = self.board.get_possible_ships_of_size(
                ship_size)
            if len(max_possible_subfleet) == self.fleet.size_of_subfleet(
                    ship_size):
                with contextlib.suppress(InvalidShipPlacementException):
                    self.mark_ship_group(max_possible_subfleet)
            else:
                for possible_subfleet in itertools.combinations(
                        max_possible_subfleet,
                        self.fleet.size_of_subfleet(ship_size)):
                    puzzle_branch = Puzzle(Board.get_copy_of(self.board),
                                           Fleet.get_copy_of(self.fleet))
                    with contextlib.suppress(InvalidShipPlacementException):
                        puzzle_branch.mark_ship_group(set(possible_subfleet))
        else:
            self.__class__.solutions.append(self.board.repr(False))
示例#2
0
    def test___eq__(self):
        other_board = parse_board(self.sample_board_repr)
        other_board_orig = Board.get_copy_of(other_board)
        self.assertTrue(self.sample_board.__eq__(other_board))
        self.assertEqual(other_board, other_board_orig)

        other_board.grid[1][1] = FieldType.UNKNOWN
        other_board_orig = Board.get_copy_of(other_board)
        self.assertFalse(self.sample_board.__eq__(other_board))
        self.assertEqual(other_board, other_board_orig)

        other_board.grid[1][1] = FieldType.SEA
        other_board_orig = Board.get_copy_of(other_board)
        self.assertTrue(self.sample_board.__eq__(other_board))
        self.assertEqual(other_board, other_board_orig)

        other_board.number_of_ship_fields_to_mark_in_series[Series.ROW][6] = 5
        other_board_orig = Board.get_copy_of(other_board)
        self.assertFalse(self.sample_board.__eq__(other_board))
        self.assertEqual(other_board, other_board_orig)

        self.assertFalse(self.sample_board.__eq__(self.sample_board.grid))
示例#3
0
 def test_get_copy_of(self):
     sample_board_orig = copy.deepcopy(self.sample_board)
     actual_copy = Board.get_copy_of(self.sample_board)
     self.assertFalse(actual_copy is self.sample_board)
     self.assertEqual(actual_copy, self.sample_board)
     self.assertEqual(self.sample_board, sample_board_orig)
示例#4
0
        def find_puzzles(  # pylint: disable=W9015
            ship_group: Set[Ship],
            covered_positions: Set[Position],
            positions_to_cover: List[Position],
            available_coverings: Dict[Ship, Set[Position]],
            puzzle: Puzzle,
        ) -> None:
            """Build a list of possible Puzzle objects created by
            branching a given Puzzle object and covering all given
            positions with a single ship from a given ship set.

            Depth-First Search (DFS) algorithm is used.

            Args:
                ship_group (Set[battleships.ship.Ship]): Group of
                    previously selected ships from available_coverings.
                covered_positions (Set[battleships.grid.Position]): Set
                    of positions covered by ships in ship_group.
                positions_to_cover (List[battleships.grid.Position]):
                    Positions remaining to be covered by ships.
                available_coverings(Dict[
                    battleships.ship.Ship, Set[battleships.grid.Position
                    ]]): Mapping of remaining ships and sets of
                    positions that each ship covers. Does not contain
                    any of the ships in ship_group. The sets of ships do
                    not contain any of the positions in
                    covered_positions.
                puzzle (battleships.puzzle.Puzzle): Current Puzzle
                    object.

            """
            if not positions_to_cover:
                puzzles.append(puzzle)
                return
            remaining_position = positions_to_cover[0]
            ship_candidates = [
                ship for ship, positions in available_coverings.items()
                if remaining_position in positions
            ]
            if not ship_candidates:
                return
            for ship_candidate in ship_candidates:
                if puzzle.board.can_fit_ship(
                        ship_candidate
                ) and not puzzle.ship_group_exceeds_fleet([ship_candidate]):
                    ship_candidate_positions = available_coverings[
                        ship_candidate]
                    new_positions_to_cover = [
                        position for position in positions_to_cover
                        if position not in ship_candidate_positions
                    ]
                    new_coverings = {
                        ship: {*ship_positions}
                        for ship, ship_positions in
                        available_coverings.items() if ship != ship_candidate
                    }
                    new_puzzle = Puzzle(Board.get_copy_of(puzzle.board),
                                        Fleet.get_copy_of(puzzle.fleet))
                    new_puzzle.board.mark_ship_and_surrounding_sea(
                        ship_candidate)
                    new_puzzle.board.mark_sea_in_series_with_no_rem_ship_fields(
                    )
                    new_puzzle.fleet.remove_ship_of_size(ship_candidate.size)
                    find_puzzles(
                        ship_group.union({ship_candidate}),
                        covered_positions.union(ship_candidate_positions),
                        new_positions_to_cover,
                        new_coverings,
                        new_puzzle,
                    )