-
Notifications
You must be signed in to change notification settings - Fork 755
'Solution' #657
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' #657
Changes from 1 commit
0c34767
30bacb3
643fc6f
e0af18d
0ad826a
20c9f14
b3ae965
f8a85c0
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,62 @@ | ||
| from typing import List, Tuple, Optional, Dict | ||
|
|
||
|
|
||
| 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[int, int], | ||
| end: Tuple[int, int], | ||
| is_drowned: bool = False | ||
| ) -> None: | ||
| self.start = start | ||
| self.end = end | ||
|
||
| self.is_drowned = is_drowned | ||
| self.decks: List[Deck] = [] | ||
|
|
||
| row_start, row_end = min(start[0], end[0]), max(start[0], end[0]) | ||
| col_start, col_end = min(start[1], end[1]), max(start[1], end[1]) | ||
|
|
||
| def get_deck(self, row, column): | ||
| # Find the corresponding deck in the list | ||
| pass | ||
| for r in range(row_start, row_end + 1): | ||
| for c in range(col_start, col_end + 1): | ||
| self.decks.append(Deck(r, c)) | ||
|
|
||
| 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 get_deck(self, row: int, column: int) -> Optional[Deck]: | ||
| for deck in self.decks: | ||
| if deck.row == row and deck.column == column: | ||
| return deck | ||
| return None | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This violates checklist item #2: "Don't use a redundant |
||
|
|
||
| def fire(self, row: int, column: int) -> None: | ||
| target_deck = self.get_deck(row, column) | ||
| if target_deck is not None: | ||
| target_deck.is_alive = False | ||
| self.is_drowned = all(not deck.is_alive 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[Tuple[Tuple[int, int], Tuple[int, int]]]) -> None: | ||
|
||
| self.field: Dict[Tuple[int, int], Ship] = {} | ||
|
|
||
| for start, end in ships: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Before inserting ship decks into |
||
| new_ship = Ship(start, end) | ||
| for deck in new_ship.decks: | ||
| self.field[(deck.row, deck.column)] = new_ship | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When assigning |
||
|
|
||
| def fire(self, location: Tuple[int, int]) -> str: | ||
| if location not in self.field: | ||
| return "Miss!" | ||
|
|
||
| ship = self.field[location] | ||
| ship.fire(location[0], location[1]) | ||
|
|
||
| if ship.is_drowned: | ||
| return "Sunk!" | ||
|
|
||
| return "Hit!" | ||
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 violates CheckList item #4: "Remove unused attributes. Check that all the attributes you define in the
__init__method are used." You assignself.starthere but that attribute is never used elsewhere; either use it (e.g., for validation/representation) or remove it.