-
Notifications
You must be signed in to change notification settings - Fork 755
Solution #672
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?
Solution #672
Changes from 1 commit
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,119 @@ | ||
| 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 | ||
|
|
||
|
|
||
| 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.decks = [] | ||
| self.start = start | ||
| self.end = end | ||
| self.is_drowned = is_drowned | ||
| for i in range( | ||
| max( | ||
| (self.end[0] - self.start[0]), | ||
| (self.end[1] - self.start[1]) | ||
| ) + 1 | ||
| ): | ||
| if start[0] != end[0]: | ||
| self.decks.append(Deck(start[0] + i, start[1])) | ||
| elif start[1] != end[1]: | ||
| self.decks.append(Deck(start[0], start[1] + i)) | ||
| else: | ||
| self.decks.append(Deck(start[0], start[1])) | ||
|
|
||
| def get_deck(self, row, column): | ||
| # Find the corresponding deck in the list | ||
| pass | ||
| def get_deck( | ||
| self, | ||
| row: int, | ||
| column: int | ||
| ) -> Deck: | ||
| for deck in self.decks: | ||
| if deck.row == row and deck.column == column: | ||
| return deck | ||
|
|
||
| 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 fire( | ||
| self, | ||
| row: int, | ||
| column: int | ||
| ) -> None: | ||
| deck = self.get_deck(row, column) | ||
| if deck.is_alive: | ||
| deck.is_alive = False | ||
| if all(deck.is_alive is False for deck in self.decks): | ||
| self.is_drowned = True | ||
|
|
||
|
|
||
| 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, | ||
| ) -> None: | ||
| self.ships = ships | ||
|
||
| self.field = {} | ||
| for ship in self.ships: | ||
| current_ship = Ship(ship[0], ship[1]) | ||
| for deck in current_ship.decks: | ||
| self.field[deck.row, deck.column] = current_ship | ||
| self._validate_field() | ||
|
|
||
| def fire( | ||
| self, | ||
| location: tuple | ||
| ) -> str: | ||
| if location in self.field: | ||
| self.field[location].fire(location[0], location[1]) | ||
| if self.field[location].is_drowned: | ||
| return "Sunk!" | ||
| return "Hit!" | ||
| return "Miss!" | ||
|
|
||
| def print_field(self) -> None: | ||
| field = [["~"] * 10 for _ in range(10)] | ||
| for (row, column), ship in self.field.items(): | ||
| deck = ship.get_deck(row, column) | ||
| if ship.is_drowned: | ||
| field[row][column] = "x" | ||
| elif not deck.is_alive: | ||
| field[row][column] = "*" | ||
| else: | ||
| field[row][column] = u"\u25A1" | ||
| for row in field: | ||
| print(" ".join(row)) | ||
|
|
||
| def _validate_field(self) -> None: | ||
| if len(set(self.field.values())) != 10: | ||
| raise Exception("Invalid number of ships") | ||
| ships_lenghts = [] | ||
| for ship in set(self.field.values()): | ||
| ships_lenghts.append(len(ship.decks)) | ||
| if ships_lenghts.count(1) != 4: | ||
| raise Exception("Invalid number of ships") | ||
| if ships_lenghts.count(2) != 3: | ||
| raise Exception("Invalid number of ships") | ||
| if ships_lenghts.count(3) != 2: | ||
| raise Exception("Invalid number of ships") | ||
| if ships_lenghts.count(4) != 1: | ||
| raise Exception("Invalid number of ships") | ||
| for (row, column), ship in self.field.items(): | ||
| for deck_row in range(-1, 2): | ||
| for deck_column in range(-1, 2): | ||
| if deck_row == 0 and deck_column == 0: | ||
| pass | ||
| elif (row + deck_row, column + deck_column) in self.field: | ||
| near_ship = self.field[ | ||
| row + deck_row, column + deck_column | ||
| ] | ||
| if near_ship != ship: | ||
| raise Exception("Invalid location of ships") | ||
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.
These attributes
self.startandself.endare assigned but never used after the__init__method completes. This violates checklist item #4: 'Remove unused attributes.' Thestartandendparameters are sufficient for creating thedeckslist within this method.