-
Notifications
You must be signed in to change notification settings - Fork 755
solution py battleship #652
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 3 commits
a921455
087d332
9e057e6
75923ca
5124dd2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,34 +1,114 @@ | ||
| from typing import List | ||
|
|
||
|
|
||
| class Deck: | ||
| def __init__(self, row, column, is_alive=True): | ||
| pass | ||
| def __init__(self, row: int, column: int, is_alive: bool = True) -> None: | ||
| self.row = row | ||
| self.column = column | ||
| self.is_alive = is_alive | ||
|
|
||
| def fire(self) -> bool: | ||
| self.is_alive = False | ||
| return self.is_alive | ||
|
|
||
| def __repr__(self) -> str: | ||
| return "□" if self.is_alive else "x" | ||
|
|
||
|
|
||
| class Ship: | ||
| def __init__(self, start, end, is_drowned=False): | ||
| # Create decks and save them to a list `self.decks` | ||
| pass | ||
| def __init__(self, | ||
| start: tuple, | ||
| end: tuple, | ||
| is_drowned: bool = False | ||
| ) -> None: | ||
| self.is_drowned = is_drowned | ||
| self.decks = self.create_decks(start, end) | ||
|
|
||
| @staticmethod | ||
| def create_decks(start: tuple, end: tuple) -> List[Deck]: | ||
| decks = [] | ||
| if start[0] == end[0]: # Horizontal ship | ||
| for col in range(start[1], end[1] + 1): | ||
| decks.append(Deck(start[0], col)) | ||
| elif start[1] == end[1]: # Vertical ship | ||
| for row in range(start[0], end[0] + 1): | ||
| decks.append(Deck(row, start[1])) | ||
| return decks | ||
|
|
||
| # @staticmethod | ||
| # def get_deck(row: int, column: int) -> Deck: | ||
| # return Deck(row, column) | ||
|
|
||
| def get_deck(self, row, column): | ||
| # Find the corresponding deck in the list | ||
| pass | ||
| def fire(self, row: int, column: int) -> str: | ||
| for deck in self.decks: | ||
| if deck.row == row and deck.column == column: | ||
| deck.fire() | ||
| if all(not deck.is_alive for deck in self.decks): | ||
| self.is_drowned = True | ||
| return "Sunk!" | ||
| return "Hit!" | ||
| return "Miss!" | ||
|
|
||
| def fire(self, row, column): | ||
| # Change the `is_alive` status of the deck | ||
| # And update the `is_drowned` value if it's needed | ||
| pass | ||
| def __repr__(self) -> str: | ||
| return "".join(str(deck) for deck in self.decks) | ||
|
|
||
|
|
||
| class Battleship: | ||
| def __init__(self, ships): | ||
| # Create a dict `self.field`. | ||
| # Its keys are tuples - the coordinates of the non-empty cells, | ||
| # A value for each cell is a reference to the ship | ||
| # which is located in it | ||
| pass | ||
|
|
||
| def fire(self, location: tuple): | ||
| # This function should check whether the location | ||
| # is a key in the `self.field` | ||
| # If it is, then it should check if this cell is the last alive | ||
| # in the ship or not. | ||
| pass | ||
| def __init__(self, ships: list[Ship]) -> None: | ||
|
||
| self.field = [["~" for _ in range(10)] for _ in range(10)] | ||
| self.ships = [Ship(start, end) for start, end in ships] | ||
| self.place_ships() | ||
| self._validate_field() | ||
|
|
||
| def place_ships(self) -> None: | ||
| for ship in self.ships: | ||
| for deck in ship.decks: | ||
| row, col = deck.row, deck.column | ||
| self.field[row][col] = "□" | ||
|
|
||
| def fire(self, location: tuple) -> str: | ||
| row, col = location | ||
| for ship in self.ships: | ||
| result = ship.fire(*location) | ||
| if result != "Miss!": | ||
| self.field[row][col] = "*" if result == "Hit!" else "x" | ||
| return result | ||
| # self.field[row][col] = "o" | ||
|
||
| return "Miss!" | ||
|
|
||
| def print_field(self) -> None: | ||
| for row in self.field: | ||
| print(" ".join(row)) | ||
|
|
||
| def _validate_field(self) -> None: | ||
| ship_lengths = [len(ship.decks) for ship in self.ships] | ||
| if len(self.ships) != 10: | ||
| raise ValueError("There should be exactly 10 ships.") | ||
| if ship_lengths.count(1) != 4: | ||
| raise ValueError("There should be exactly 4 single-deck ships.") | ||
| if ship_lengths.count(2) != 3: | ||
| raise ValueError("There should be exactly 3 double-deck ships.") | ||
| if ship_lengths.count(3) != 2: | ||
| raise ValueError("There should be exactly 2 three-deck ships.") | ||
| if ship_lengths.count(4) != 1: | ||
| raise ValueError("There should be exactly 1 four-deck ship.") | ||
| if not self._check_no_neighbors(): | ||
| raise ValueError("Ships should not be located " | ||
| "in neighboring cells.") | ||
|
|
||
| def _check_no_neighbors(self) -> bool: | ||
| directions = [(-1, -1), (-1, 0), (-1, 1), (0, -1), | ||
| (0, 1), (1, -1), (1, 0), (1, 1) | ||
| ] | ||
| for ship in self.ships: | ||
| for deck in ship.decks: | ||
| row, col = deck.row, deck.column | ||
| for dr, dc in directions: | ||
| nr, nc = row + dr, col + dc | ||
| if (0 <= nr < 10 | ||
| and 0 <= nc < 10 | ||
| and self.field[nr][nc] == "□" | ||
| and (nr, nc) | ||
| not in [(d.row, d.column) for d in ship.decks]): | ||
| return False | ||
| return True | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This attribute
self.is_drownedis assigned a value here and on line 45, but it's never read or used anywhere else in the program. This violates checklist item #4: 'Remove unused attributes.' The state of the ship (whether it's sunk or not) is determined on-the-fly by checking its decks, so this attribute is redundant. Consider removing it, along with theis_drownedparameter from the__init__method.