Exemple #1
0
    def load_puzzle(cls, path: Optional[pathlib.Path] = None) -> "Puzzle":
        """Read the puzzle input file and create a Puzzle object from
        its data.

        Args:
            path (Optional[pathlib.Path]): Path to the input file.
                Defaults to None, in which case the default input file
                location is used.

        Returns:
            battleships.puzzle.Puzzle: Newly created Puzzle object.

        """
        if path:
            input_data = Puzzle.parse_input_data_from_file(path)
        else:
            input_data = Puzzle.parse_input_data_from_file(
                params.INPUT_FILE_PATH)
        return Puzzle(
            Board.parse_board(
                input_data.grid,
                input_data.solution_ship_fields_in_rows,
                input_data.solution_ship_fields_in_cols,
            ),
            input_data.fleet,
        )
Exemple #2
0
    def test_parse_board(self):
        input_grid = parse_fieldtypegrid(
            " .   .   .   .   .   .   .   .   .   . \n"
            " x   .   .   .   O   .   .   .   .   . \n"
            " .   .   O   .   O   .   .   x   .   . \n"
            " .   .   O   .   O   .   x   .   .   O \n"
            " x   .   O   .   .   .   x   .   .   . \n"
            " .   .   .   .   O   .   x   .   .   . \n"
            " .   x   .   .   O   .   x   .   .   . \n"
            " x   .   .   x   .   .   .   .   O   O \n"
            " .   .   .   .   .   .   .   .   .   . \n"
            " O   .   O   O   O   O   .   O   O   . "
        )
        input_number_of_ship_fields_to_mark_in_rows = [0, 2, 2, 6, 1, 5, 1, 2, 5, 7]
        input_number_of_ship_fields_to_mark_in_cols = [2, 1, 5, 4, 9, 5, 5, 1, 2, 3]

        input_grid_orig = input_grid
        input_number_of_ship_fields_to_mark_in_rows_orig = (
            input_number_of_ship_fields_to_mark_in_rows
        )
        input_number_of_ship_fields_to_mark_in_cols_orig = (
            input_number_of_ship_fields_to_mark_in_cols
        )

        parsed_board = Board.parse_board(
            input_grid,
            input_number_of_ship_fields_to_mark_in_rows,
            input_number_of_ship_fields_to_mark_in_cols,
        )
        self.assertEqual(self.sample_board, parsed_board)
        self.assertFalse(input_grid is parsed_board.grid)
        self.assertEqual(input_grid, input_grid_orig)
        self.assertFalse(
            input_number_of_ship_fields_to_mark_in_rows
            is parsed_board.number_of_ship_fields_to_mark_in_series[Series.ROW]
        )
        self.assertEqual(
            input_number_of_ship_fields_to_mark_in_rows,
            input_number_of_ship_fields_to_mark_in_rows_orig,
        )
        self.assertFalse(
            input_number_of_ship_fields_to_mark_in_cols
            is parsed_board.number_of_ship_fields_to_mark_in_series[Series.COLUMN]
        )
        self.assertEqual(
            input_number_of_ship_fields_to_mark_in_cols,
            input_number_of_ship_fields_to_mark_in_cols_orig,
        )