-
Notifications
You must be signed in to change notification settings - Fork 599
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' #309
base: master
Are you sure you want to change the base?
'Solution' #309
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -1,34 +1,106 @@ | ||||||||
from typing import Tuple, List | ||||||||
|
||||||||
|
||||||||
class Deck: | ||||||||
def __init__(self, row, column, is_alive=True): | ||||||||
pass | ||||||||
def __init__(self, row: int, column: int, is_alive: bool = True) -> None: | ||||||||
self.is_alive = is_alive | ||||||||
self.coordinate = (row, column) | ||||||||
|
||||||||
|
||||||||
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], | ||||||||
is_drowned: bool = False) -> None: | ||||||||
self.is_drowned = is_drowned | ||||||||
self.decks = Ship.create_decks(start, end) | ||||||||
|
||||||||
def get_deck(self, row: int, column: int) -> Deck: | ||||||||
for deck in self.decks: | ||||||||
if deck.coordinate == (row, column): | ||||||||
return deck | ||||||||
|
||||||||
def fire(self, row: int, column: int) -> str: | ||||||||
self.get_deck(row, column).is_alive = False | ||||||||
self.is_it_drowned() | ||||||||
|
||||||||
if self.is_drowned: | ||||||||
return "Sunk!" | ||||||||
else: | ||||||||
return "Hit!" | ||||||||
|
||||||||
def is_it_drowned(self) -> None: | ||||||||
self.is_drowned = True | ||||||||
for deck in self.decks: | ||||||||
if deck.is_alive: | ||||||||
self.is_drowned = False | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just use any/all |
||||||||
|
||||||||
def get_length(self) -> int: | ||||||||
return len(self.decks) | ||||||||
|
||||||||
def get_deck(self, row, column): | ||||||||
# Find the corresponding deck in the list | ||||||||
pass | ||||||||
@staticmethod | ||||||||
def create_decks(ship_begin: Tuple[int, int], | ||||||||
ship_end: Tuple[int, int]) -> List: | ||||||||
if ship_begin == ship_end: | ||||||||
return [Deck(*ship_begin)] | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This check is redundant |
||||||||
|
||||||||
def fire(self, row, column): | ||||||||
# Change the `is_alive` status of the deck | ||||||||
# And update the `is_drowned` value if it's needed | ||||||||
pass | ||||||||
border = 0 | ||||||||
if ship_begin[1] != ship_end[1]: | ||||||||
border = 1 | ||||||||
|
||||||||
ship_decks = [Deck(ship_begin[0], ship_begin[1])] | ||||||||
blank_deck = [ship_begin[0], ship_begin[1]] | ||||||||
|
||||||||
while blank_deck[border] != ship_end[border]: | ||||||||
blank_deck[border] += 1 | ||||||||
ship_decks.append(Deck(blank_deck[0], blank_deck[1])) | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of if+ while just use forloop into forloop |
||||||||
return ship_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]) -> None: | ||||||||
self._init_field(ships) | ||||||||
self.print_field() | ||||||||
|
||||||||
def _init_field(self, ships: List[Tuple]) -> None: | ||||||||
self.field = {} | ||||||||
for ship in ships: | ||||||||
temp_ship = Ship(*ship) | ||||||||
for deck in temp_ship.decks: | ||||||||
self.field[deck.coordinate] = temp_ship | ||||||||
|
||||||||
def fire(self, location: Tuple[int, int]) -> str: | ||||||||
ship = self.field.get(location) | ||||||||
if ship is not None: | ||||||||
return ship.fire(*location) | ||||||||
else: | ||||||||
return "Miss!" | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
|
||||||||
def print_field(self) -> None: | ||||||||
row = -1 | ||||||||
column = -1 | ||||||||
empty = " ~ " | ||||||||
alive_deck = " □ " | ||||||||
hit_deck = " * " | ||||||||
drowned_ship = " x " | ||||||||
|
||||||||
def print_cell(coord: tuple) -> str: | ||||||||
ship = coord | ||||||||
if ship in self.field: | ||||||||
if self.field[ship].is_drowned: | ||||||||
return drowned_ship | ||||||||
elif not self.field[ship].get_deck(ship[0], ship[1]).is_alive: | ||||||||
return hit_deck | ||||||||
elif self.field[ship].get_deck(ship[0], ship[1]).is_alive: | ||||||||
return alive_deck | ||||||||
else: | ||||||||
return empty | ||||||||
|
||||||||
for cell in range(100): | ||||||||
if column == 9: | ||||||||
column = -1 | ||||||||
if cell % 10 == 0: | ||||||||
row += 1 | ||||||||
print() | ||||||||
column += 1 | ||||||||
print(f" {print_cell((row, column))} ", end=" ") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It can also return None
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not fixed