-
Notifications
You must be signed in to change notification settings - Fork 602
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
Solution #470
Open
OleksiukStepan
wants to merge
3
commits into
mate-academy:master
Choose a base branch
from
OleksiukStepan:solution
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Solution #470
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,127 @@ | ||
class Deck: | ||
def __init__(self, row, column, is_alive=True): | ||
pass | ||
from __future__ import annotations | ||
|
||
|
||
class Cell: | ||
def __init__(self, row: int, column: int, symbol: str = "~") -> None: | ||
self.row = row | ||
self.column = column | ||
self.symbol = symbol | ||
|
||
|
||
class Deck(Cell): | ||
def __init__(self, row: int, column: int, is_alive: bool = True) -> None: | ||
super().__init__(row, 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.start = start | ||
self.end = end | ||
self.decks = self.create_decs(start, end) | ||
self.is_drowned = is_drowned | ||
|
||
def get_deck(self, row: int, column: int) -> Deck | None: | ||
deck = [ | ||
deck for deck in self.decks | ||
if deck.row == row and deck.column == column | ||
] | ||
return deck[0] if deck else None | ||
|
||
def fire(self, row: int, column: int) -> str: | ||
if self.is_drowned: | ||
return "The ship in this location already drowned" | ||
|
||
deck = self.get_deck(row, column) | ||
deck.is_alive = False | ||
deck.symbol = "*" | ||
return "Sunk!" if self.if_all_deck_is_damaged() else "Hit!" | ||
|
||
def if_all_deck_is_damaged(self) -> bool: | ||
if not any([deck.is_alive for deck in self.decks]): | ||
self.is_drowned = True | ||
for deck in self.decks: | ||
deck.symbol = "x" | ||
return True | ||
return False | ||
|
||
def get_deck(self, row, column): | ||
# Find the corresponding deck in the list | ||
pass | ||
@staticmethod | ||
def create_decs(start: tuple, end: tuple) -> list[Deck]: | ||
list_of_decks = [] | ||
if start[0] == end[0]: | ||
# ship located horizontally | ||
ship_length = end[1] - start[1] + 1 | ||
for i in range(ship_length): | ||
list_of_decks.append(Deck(start[0], start[1] + i)) | ||
else: | ||
# ship located vertically | ||
ship_length = end[0] - start[0] + 1 | ||
for i in range(ship_length): | ||
list_of_decks.append(Deck(start[0] + i, start[1])) | ||
|
||
def fire(self, row, column): | ||
# Change the `is_alive` status of the deck | ||
# And update the `is_drowned` value if it's needed | ||
pass | ||
return list_of_decks | ||
|
||
@classmethod | ||
def create_ship(cls, ship_cords: tuple) -> Ship: | ||
return Ship(*ship_cords) | ||
|
||
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. use cls instead of Ship, it's the purpose of classmethod |
||
|
||
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]) -> None: | ||
self.field = self.create_empty_field() | ||
self.ships = ships | ||
|
||
@property | ||
def ships(self) -> list[Ship]: | ||
return self._ships | ||
|
||
@ships.setter | ||
def ships(self, ships: list[tuple]) -> None: | ||
self._ships = [Ship.create_ship(cords) for cords in ships] | ||
self.add_ships_to_the_field() | ||
|
||
def fire(self, location: tuple) -> str: | ||
try: | ||
if not isinstance(self.field[location], Ship): | ||
return "Miss!" | ||
result = self.field[location].fire(*location) | ||
# If you want to print field -> uncomment func() below | ||
# self.print_field() | ||
return result | ||
except (TypeError, KeyError): | ||
print( | ||
f"Location {location} isn't correct. " | ||
f"Must be in range (0-9, 0-9)" | ||
) | ||
|
||
def add_ships_to_the_field(self) -> None: | ||
for ship in self.ships: | ||
for deck in ship.decks: | ||
self.field[(deck.row, deck.column)] = ship | ||
|
||
def print_field(self) -> None: | ||
for i, (cords, cell) in enumerate(self.field.items()): | ||
if i % 10 == 0: | ||
print() | ||
print(self.get_symbol_from_cell(cords, cell), end=" ") | ||
print("") | ||
|
||
@staticmethod | ||
def get_symbol_from_cell(cords: tuple[int, int], cell: Cell | Ship) -> str: | ||
if isinstance(cell, Ship): | ||
return cell.get_deck(cords[0], cords[1]).symbol | ||
else: | ||
return cell.symbol | ||
|
||
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. no need for else statement because if statement has return |
||
@staticmethod | ||
def create_empty_field() -> dict[tuple[int, int], Cell]: | ||
return ({ | ||
(i, j): Cell(i, j) | ||
for i in range(10) | ||
for j in range(10) | ||
}) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
a bit don't get it. In case deck is not found, get_deck returns None, but you set deck.is_alive = False anyway. Shouldn't it be missed?
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.
You're correct. The method get_deck cannot return None, as each ship consists solely of decks; thus, it will always return a deck. I've made some adjustments to ensure it functions more accurately.