Skip to content
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 #336

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 58 additions & 24 deletions app/main.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,68 @@
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 __getitem__(self, item: int) -> int:
if item == 0:
return self.row
elif item == 1:
return self.column


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 | None = False
) -> None:
self.is_drowned = is_drowned
self.decks = []
if start[0] == end[0]:
start_point = start[1]
end_point = end[1] + 1
for column in range(start_point, end_point):
self.decks.append(Deck(start[0], column))
elif start[1] == end[1]:
start_point = start[0]
end_point = end[0] + 1
for row in range(start_point, end_point):
self.decks.append(Deck(row, end[1]))
elif start[0] == end[0] and start[1] == end[1]:
self.decks.append(Deck(start[0], end[0]))

def get_deck(self, row, column):
def get_deck(self, row: int, column: int) -> Deck:
# Find the corresponding deck in the list

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove all comments from your PR.

pass
for deck in self.decks:
if row == deck[0] and column == deck[1]:
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.decks.index(self.get_deck(row, column))
self.decks[deck].is_alive = False
self.is_drowned = not any(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[Ship]) -> None:
self.field = {}
for data in ships:
ship = Ship(data[0], data[1])
for deck in ship.decks:
self.field[(deck[0], deck[1])] = ship

def fire(self, location: tuple) -> str:
if location in self.field:
if isinstance(self.field[location], Ship):
self.field[location].fire(location[0], location[1])
if not self.field[location].is_drowned:
return "Hit!"
return "Sunk!"
return "Miss!"