Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solution #458

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
126 changes: 101 additions & 25 deletions app/main.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,110 @@
from typing import List, Tuple, Dict


class Deck:
def __init__(self, row, column, is_alive=True):
pass
def __init__(self, row: int, column: int) -> None:
self.row: int = row
self.column: int = column
self.is_alive: bool = True

def hit(self) -> None:
self.is_alive = False


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.start: Tuple[int, int] = start
self.end: Tuple[int, int] = end
self.decks: List[Deck] = self.create_decks(start, end)
self.is_drowned: bool = False

def create_decks(self,
start: Tuple[int, int],
end: Tuple[int, int]) -> List[Deck]:
decks: List[Deck] = []
if start[0] == end[0]:
for col in range(start[1], end[1] + 1):
decks.append(Deck(start[0], col))
else:
for row in range(start[0], end[0] + 1):
decks.append(Deck(row, start[1]))
return decks

def get_deck(self, row, column):
# Find the corresponding deck in the list
pass
def fire(self, row: int, column: int) -> None:
for deck in self.decks:
if deck.row == row and deck.column == column:
deck.hit()
break
self.update_is_drowned()

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 update_is_drowned(self) -> None:
self.is_drowned = all(not deck.is_alive for deck in self.decks)


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:
self.field: Dict[Tuple[int, int], Ship] = {}
self.ships: List[Ship] = []
for start, end in ships:
ship = Ship(start, end)
self.ships.append(ship)
for deck in ship.decks:
self.field[(deck.row, deck.column)] = ship

def fire(self, location: Tuple[int, int]) -> str:
if location not in self.field:
return "Miss!"
ship = self.field[location]
ship.fire(location[0], location[1])
if ship.is_drowned:
return "Sunk!"
return "Hit!"

def print_field(self) -> None:
field_representation: List[List[str]] = [["~"] * 10 for _ in range(10)]
for ship in self.ships:
for deck in ship.decks:
if not deck.is_alive:
symbol = "x" if ship.is_drowned else "*"
else:
symbol = "□"
field_representation[deck.row][deck.column] = symbol
for row in field_representation:
print(" ".join(row))

def _validate_field(self) -> None:
if len(self.ships) != 10:
raise ValueError("The total number of ships should be 10")
counts: Dict[int, int] = {1: 0, 2: 0, 3: 0, 4: 0}
for ship in self.ships:
length: int = len(ship.decks)
if length in counts:
counts[length] += 1
if (
counts
[1] != 4 or counts[2] != 3 or counts[3] != 2 or counts[4] != 1
):
raise ValueError("Invalid number of ships of each type")
self._check_no_neighbors()

def _check_no_neighbors(self) -> None:
directions: List[Tuple[int, int]] = [
(-1, -1), (-1, 0), (-1, 1),
(0, -1), (0, 1),
(1, -1), (1, 0), (1, 1)
]
for ship in self.ships:
for deck in ship.decks:
for direction in directions:
neighbor: Tuple[int, int] = (
deck.row + direction[0],
deck.column + direction[1]
)
if (
neighbor
in self.field and self.field[neighbor] != ship
):
raise ValueError(
"Ships are located in neighboring cells"
)
Loading