Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
BastovOleksandr committed Aug 29, 2024
1 parent f38a83f commit f4ce2fc
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def __init__(
self.is_drowned = is_drowned
self.decks = []
if start == end:
self.decks.append(Deck(start[0], start[1]))
self.decks.append(Deck(*start))
elif start[0] == end[0] and start[1] != end[1]:
for num in range(end[1] - start[1] + 1):
self.decks.append(Deck(start[0], start[1] + num))
Expand All @@ -30,7 +30,7 @@ def get_deck(self, row: int, column: int) -> Deck:

def fire(self, row: int, column: int) -> bool:
deck = self.get_deck(row, column)
if not self.is_drowned and deck.is_alive:
if deck.is_alive and not self.is_drowned:
deck.is_alive = False
for deck in self.decks:
if deck.is_alive:
Expand All @@ -44,11 +44,13 @@ class Battleship:
def __init__(self, ships: list[tuple[tuple, tuple]]) -> None:
self.field = {}
for ship in ships:
current_ship = Ship(ship[0], ship[1])
current_ship = Ship(*ship)
for deck in current_ship.decks:
self.field[(deck.row, deck.column)] = current_ship
self._validate_field()
self.print_field()

def fire(self, location: tuple) -> str:
def fire(self, location: tuple[int, int]) -> str:
if location in self.field:
sunk = self.field[location].fire(*location)
return "Sunk!" if sunk else "Hit!"
Expand All @@ -57,11 +59,11 @@ def fire(self, location: tuple) -> str:

def _validate_field(self) -> None:
ships = set(self.field.values())
if not (
len(ships) == 10
or self._validate_ships_count
or self._validate_ship_location
):
if not all([
len(ships) == 10,
self._validate_ships_count,
self._validate_ship_location
]):
raise InvalidBattleshipField(
"Incorrect ship coordinates in the passed list"
)
Expand All @@ -77,7 +79,7 @@ def print_field(self) -> None:
empty[deck.row][deck.column] = u"\u25A1"
else:
empty[deck.row][deck.column] = marker
filled_field = ""
filled_field = "\n"

for row in empty:
filled_field += "\t".join(row) + "\n"
Expand All @@ -102,7 +104,7 @@ def _validate_ship_location(self) -> bool:
if adjacent in self.field and self.field[adjacent] != ship:
return False

return False
return True


class InvalidBattleshipField(Exception):
Expand Down

0 comments on commit f4ce2fc

Please sign in to comment.