Skip to content

Commit

Permalink
Solution
Browse files Browse the repository at this point in the history
  • Loading branch information
DanSheremeta committed Nov 17, 2023
1 parent a91a4c5 commit 3bd853b
Showing 1 changed file with 101 additions and 25 deletions.
126 changes: 101 additions & 25 deletions app/main.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,110 @@
class ValidationError(Exception):
pass


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 __repr__(self) -> str:
return f"({self.row}; {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: list,
end: list,
is_drowned: bool = False
) -> None:
if start[0] == end[0]:
self.decks = [
Deck(start[0], i)
for i in range(start[1], end[1] + 1)
]
if start[1] == end[1]:
self.decks = [
Deck(i, start[1])
for i in range(start[0], end[0] + 1)
]
self.is_drowned = is_drowned

def __str__(self) -> str:
return f"{self.decks}"

def __repr__(self) -> str:
return f"{self.decks}"

def get_deck(self, row, column):
# Find the corresponding deck in the list
pass
def get_deck(self, row: int, column: int) -> Deck | None:
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) -> str:
deck = self.get_deck(row, column)
deck.is_alive = False
self.is_drowned = all([
not deck.is_alive
for deck in self.decks
])
return "Sunk!" if self.is_drowned else "Hit!"


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.field = {
ship: Ship(*ship)
for ship in ships
}
self._validate_field()

def fire(self, loc: tuple) -> str:
for coords, ship in self.field.items():
ship_decks = ship.get_deck(*loc)
if ship_decks is not None:
return ship.fire(*loc)
return "Miss!"

def _validate_field(self) -> None:
if len(self.field) != 10:
raise ValidationError("Total amount of the ships should be 10")
ships = [0] * 4
for coords, ship in self.field.items():
if coords[0] == coords[1]:
ships[0] += 1
continue
diff = [coords[1][0] - coords[0][0], coords[1][1] - coords[0][1]]
if diff[0] == 0:
ships[diff[1]] += 1
elif diff[1] == 0:
ships[diff[0]] += 1

if ships[0] != 4 or ships[1] != 3 or ships[2] != 2 or ships[3] != 1:
raise ValidationError("there should be 4 single-deck ships\n"
"there should be 3 double-deck ships\n"
"there should be 2 three-deck ships\n"
"there should be 1 four-deck ship"
)

def print_field(self) -> None:
for i in range(0, 10):
for j in range(0, 10):
coord = (i, j)
for coords, ship in self.field.items():
deck = ship.get_deck(*coord)
if deck is not None:
if ship.is_drowned:
print("x ", end="")
break
if not deck.is_alive:
print("* ", end="")
break
print(u"\u25A1 ", end="")
break
else:
print("~ ", end="")
print()
print()
return

0 comments on commit 3bd853b

Please sign in to comment.