From 8d7f656d3eecfe859ba0e0c386ff10d71bc049d1 Mon Sep 17 00:00:00 2001 From: Arsen Markotskyi Date: Sun, 21 Jul 2024 19:04:16 +0300 Subject: [PATCH 1/4] "sol" --- app/main.py | 68 +++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 56 insertions(+), 12 deletions(-) diff --git a/app/main.py b/app/main.py index 626f41cf..b014caa6 100644 --- a/app/main.py +++ b/app/main.py @@ -1,34 +1,78 @@ 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 get_coordinates(self) -> tuple: + return self.row, self.column + + def hit(self) -> None: + self.is_alive = False class Ship: - def __init__(self, start, end, is_drowned=False): + def __init__(self, start: tuple, end: tuple, is_drowned: bool = False) -> None: # Create decks and save them to a list `self.decks` - pass + self.start = start + self.end = end + self.is_drowned = is_drowned + self.decks = self.create_decks() + self.is_drowned = is_drowned - def get_deck(self, row, column): + def create_decks(self) -> list: + decks = [] + if self.start[0] == self.end[0]: + for column in range(self.start[1], self.end[1] + 1): + decks.append(Deck(self.start[0], column)) + else: + for row in range(self.start[0], self.end[0] + 1): + decks.append(Deck(row, self.start[1])) + return decks + + def get_deck(self, row: int, column: int) -> Deck: # Find the corresponding deck in the list - pass + for deck in self.decks: + if deck.get_coordinates() == (row, column): + return deck - def fire(self, row, column): + def fire(self, row: int, column: int) -> str: # Change the `is_alive` status of the deck # And update the `is_drowned` value if it's needed - pass + deck = self.get_deck(row, column) + if deck: + deck.hit() + if all(not deck.is_alive for deck in self.decks): + self.is_drowned = True + return "Sunk!" + return "Hit!" + return "Miss!" class Battleship: - def __init__(self, ships): + def __init__(self, ships: list) -> None: # 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 + self.ships = [Ship(start, end) for start, end in ships] + self.field = [["~"] * 10 for _ in range(10)] + + def place_ships(self) -> None: + for ship in self.ships: + for deck in ship.decks: + self.field[deck.row][deck.column] = "□" - def fire(self, location: tuple): + def fire(self, location: tuple) -> str: # 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 + row, column = location + for ship in self.ships: + result = ship.fire(row, column) + if result == "Hit!" or result == "Sunk!": + self.field[row][column] = "x" + return result + self.field[row][column] = "o" + return "Miss!" From 9eff0f0cf425ff97a305cf1319252abf0483b3f6 Mon Sep 17 00:00:00 2001 From: Arsen Markotskyi Date: Sun, 21 Jul 2024 19:09:41 +0300 Subject: [PATCH 2/4] "sol" --- app/main.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/app/main.py b/app/main.py index b014caa6..6fa5ac0e 100644 --- a/app/main.py +++ b/app/main.py @@ -12,7 +12,9 @@ def hit(self) -> None: class Ship: - def __init__(self, start: tuple, end: tuple, is_drowned: bool = False) -> None: + def __init__( + self, start: tuple, end: tuple, is_drowned: bool = False + ) -> None: # Create decks and save them to a list `self.decks` self.start = start self.end = end From ac6d6ef303e29884bdaa7985862ff709e58b34c1 Mon Sep 17 00:00:00 2001 From: Arsen Markotskyi Date: Mon, 22 Jul 2024 19:52:39 +0300 Subject: [PATCH 3/4] "sol" --- app/main.py | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/app/main.py b/app/main.py index 6fa5ac0e..4f78d2f4 100644 --- a/app/main.py +++ b/app/main.py @@ -15,7 +15,6 @@ class Ship: def __init__( self, start: tuple, end: tuple, is_drowned: bool = False ) -> None: - # Create decks and save them to a list `self.decks` self.start = start self.end = end self.is_drowned = is_drowned @@ -39,8 +38,6 @@ def get_deck(self, row: int, column: int) -> Deck: return deck def fire(self, row: int, column: int) -> str: - # Change the `is_alive` status of the deck - # And update the `is_drowned` value if it's needed deck = self.get_deck(row, column) if deck: deck.hit() @@ -53,10 +50,6 @@ def fire(self, row: int, column: int) -> str: class Battleship: def __init__(self, ships: list) -> None: - # 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 self.ships = [Ship(start, end) for start, end in ships] self.field = [["~"] * 10 for _ in range(10)] @@ -66,10 +59,6 @@ def place_ships(self) -> None: self.field[deck.row][deck.column] = "□" def fire(self, location: tuple) -> str: - # 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. row, column = location for ship in self.ships: result = ship.fire(row, column) From 4712f900bb000752f05333a304be69ad81a29371 Mon Sep 17 00:00:00 2001 From: Arsen Markotskyi Date: Mon, 22 Jul 2024 19:53:36 +0300 Subject: [PATCH 4/4] "sol" --- app/main.py | 1 - 1 file changed, 1 deletion(-) diff --git a/app/main.py b/app/main.py index 4f78d2f4..073131fb 100644 --- a/app/main.py +++ b/app/main.py @@ -32,7 +32,6 @@ def create_decks(self) -> list: return decks def get_deck(self, row: int, column: int) -> Deck: - # Find the corresponding deck in the list for deck in self.decks: if deck.get_coordinates() == (row, column): return deck