Skip to content
Open
Changes from 1 commit
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
135 changes: 110 additions & 25 deletions app/main.py
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

Choose a reason for hiding this comment

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

These attributes self.start and self.end are assigned but never used after the __init__ method completes. This violates checklist item #4: 'Remove unused attributes.' The start and end parameters are sufficient for creating the decks list within this method.

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

Choose a reason for hiding this comment

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

Great job addressing the previous feedback about unused attributes in the Ship class! However, a similar issue exists here. The self.ships attribute is assigned but only used within the __init__ method. According to checklist item #4 ('Remove unused attributes'), it should not be stored on the instance if it's not used elsewhere. You can use the ships parameter directly in the for loop on line 63.

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")
Loading