Skip to content

Commit

Permalink
solution
Browse files Browse the repository at this point in the history
  • Loading branch information
spryima committed Nov 4, 2023
1 parent a91a4c5 commit 7ee1252
Showing 1 changed file with 87 additions and 25 deletions.
112 changes: 87 additions & 25 deletions app/main.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,96 @@
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[int, int], end: tuple[int, int]) -> None:
self.decks = []
for x_coord in range(start[0], end[0] + 1):
for y_coord in range(start[1], end[1] + 1):
self.decks.append(Deck(x_coord, y_coord))
self.alive_decks = len(self.decks)

def get_deck(self, row, column):
# Find the corresponding deck in the list
pass
def get_deck(self, row: int, column: int) -> bool:
for deck in self.decks:
if (row, column) == (deck.row, deck.column):
return deck.is_alive

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:
for deck in self.decks:
if (row, column) == (deck.row, deck.column):
deck.is_alive = False


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[int, int], tuple[int, int]]]) -> None:

ships = [Ship(ship_coord[0], ship_coord[1]) for ship_coord in ships]
self.field = {(deck.row, deck.column): ship
for ship in ships for deck in ship.decks}
if not self._validate_field(ships):
print("List of ships is wrong!")

def fire(self, location: tuple[int, int]) -> str:
if location in self.field:
if self.field[location].get_deck(location[0], location[1]):
self.field[location].fire(location[0], location[1])
self.field[location].alive_decks -= 1
if self.field[location].alive_decks == 0:
return "Sunk!"
return "Hit!"
return "Miss!"

def print_field(self) -> None:
print(end=" ")
for column_num in range(10):
print(f" {column_num} ", end="")
print()
for row in range(10):
print(row, end=" ")
for column in range(10):
if (row, column) in self.field:
if self.field[(row, column)].get_deck(row, column):
print(u" \u25A1 ", end="")
else:
print(" x ", end="")
else:
print(" . ", end="")
print()

def _validate_field(self, ships: list[Ship]) -> bool:
single_deck = 0
double_deck = 0
three_deck = 0
four_deck = 0
for ship in ships:
if ship.alive_decks == 4:
four_deck += 1
if ship.alive_decks == 3:
three_deck += 1
if ship.alive_decks == 2:
double_deck += 1
if ship.alive_decks == 1:
single_deck += 1
if (four_deck != 1 or three_deck != 2
or double_deck != 3 or single_deck != 4):
return False
check_pool = []
for ship in ships:
for deck in ship.decks:
if (deck.row, deck.column) in check_pool:
return False
for deck in ship.decks:
check_pool.extend([(deck.row, deck.column),
(deck.row, deck.column + 1),
(deck.row, deck.column - 1),
(deck.row + 1, deck.column + 1),
(deck.row + 1, deck.column - 1),
(deck.row + 1, deck.column),
(deck.row - 1, deck.column + 1),
(deck.row - 1, deck.column - 1),
(deck.row - 1, deck.column)])
return True

0 comments on commit 7ee1252

Please sign in to comment.