Skip to content

Commit

Permalink
solution
Browse files Browse the repository at this point in the history
  • Loading branch information
Bombardier256 committed Jan 10, 2025
1 parent a91a4c5 commit 253fa7c
Showing 1 changed file with 62 additions and 25 deletions.
87 changes: 62 additions & 25 deletions app/main.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,71 @@
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.is_drowned = is_drowned
self.decks = list()
if start[0] < end[0]:
self.decks.extend([
Deck(deck_n, start[1])
for deck_n in range(start[0], end[0] + 1)
])
elif start[1] < end[1]:
self.decks.extend([
Deck(start[0], deck_n)
for deck_n in range(start[1], end[1] + 1)
])
else:
self.decks.append(Deck(start[0], start[1]))

def get_deck(self, row, column):
# Find the corresponding deck in the list
pass
self.health = len(self.decks)

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 get_deck(self, row: int, column: int) -> Deck:
return [
deck
for deck in self.decks
if deck.row == row and deck.column == column
][0]

def fire(self, row: int, column: int) -> None:
if deck := self.get_deck(row, column):
deck.is_alive = False
self.health -= 1
if self.health == 0:
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[tuple, tuple]) -> None:
self.field = dict()
for start_stop in ships:
new_ship = Ship(start_stop[0], start_stop[1])
for deck in new_ship.decks:
self.field[deck.row, deck.column] = new_ship

def fire(self, location: tuple[int, int]) -> str:
if location not in self.field:
return "Miss!"

self.field[location].fire(location[0], location[1])
if self.field[location].health == 0:
return "Sunk!"
else:
return "Hit!"

def field_print(self) -> None:
for row in range(10):
for column in range(10):
if (row, column) in self.field:
print(u"\u25A1", 5 * " ", end="")
else:
print("~", 5 * " ", end="")
print()

0 comments on commit 253fa7c

Please sign in to comment.