From 6e3f8a07deda9c7c2cb1e34e875df31358814bc3 Mon Sep 17 00:00:00 2001 From: Vitaliy Stepanyuk Date: Sun, 15 Mar 2026 22:47:22 +0200 Subject: [PATCH 1/4] Solution --- app/main.py | 135 ++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 110 insertions(+), 25 deletions(-) diff --git a/app/main.py b/app/main.py index 626f41cf..fe051642 100644 --- a/app/main.py +++ b/app/main.py @@ -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 + 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 + 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") From edb62d4addc5477814cb2ae3ef0362bcaa14e968 Mon Sep 17 00:00:00 2001 From: Vitaliy Stepanyuk Date: Sun, 15 Mar 2026 22:53:48 +0200 Subject: [PATCH 2/4] Fixed solution --- app/main.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/app/main.py b/app/main.py index fe051642..36125997 100644 --- a/app/main.py +++ b/app/main.py @@ -18,8 +18,6 @@ def __init__( is_drowned: bool = False ) -> None: self.decks = [] - self.start = start - self.end = end self.is_drowned = is_drowned for i in range( max( From a7c968255fb130af44d344c3aee0fe01bedbc1ad Mon Sep 17 00:00:00 2001 From: Vitaliy Stepanyuk Date: Sun, 15 Mar 2026 22:56:59 +0200 Subject: [PATCH 3/4] Fixed solution --- app/main.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/main.py b/app/main.py index 36125997..8b46bfd1 100644 --- a/app/main.py +++ b/app/main.py @@ -21,8 +21,8 @@ def __init__( self.is_drowned = is_drowned for i in range( max( - (self.end[0] - self.start[0]), - (self.end[1] - self.start[1]) + (end[0] - start[0]), + (end[1] - start[1]) ) + 1 ): if start[0] != end[0]: From bfdc86869b6f41bd35b10f8613f71154c89e7583 Mon Sep 17 00:00:00 2001 From: Vitaliy Stepanyuk Date: Sun, 15 Mar 2026 23:02:27 +0200 Subject: [PATCH 4/4] Final solution --- app/main.py | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/app/main.py b/app/main.py index 8b46bfd1..92830f95 100644 --- a/app/main.py +++ b/app/main.py @@ -58,9 +58,8 @@ def __init__( self, ships: list, ) -> None: - self.ships = ships self.field = {} - for ship in self.ships: + for ship in ships: current_ship = Ship(ship[0], ship[1]) for deck in current_ship.decks: self.field[deck.row, deck.column] = current_ship @@ -93,16 +92,16 @@ def print_field(self) -> None: def _validate_field(self) -> None: if len(set(self.field.values())) != 10: raise Exception("Invalid number of ships") - ships_lenghts = [] + ships_lengths = [] for ship in set(self.field.values()): - ships_lenghts.append(len(ship.decks)) - if ships_lenghts.count(1) != 4: + ships_lengths.append(len(ship.decks)) + if ships_lengths.count(1) != 4: raise Exception("Invalid number of ships") - if ships_lenghts.count(2) != 3: + if ships_lengths.count(2) != 3: raise Exception("Invalid number of ships") - if ships_lenghts.count(3) != 2: + if ships_lengths.count(3) != 2: raise Exception("Invalid number of ships") - if ships_lenghts.count(4) != 1: + if ships_lengths.count(4) != 1: raise Exception("Invalid number of ships") for (row, column), ship in self.field.items(): for deck_row in range(-1, 2):