-
Notifications
You must be signed in to change notification settings - Fork 603
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
Battle #510
Open
AlyaVKravchenko
wants to merge
2
commits into
mate-academy:master
Choose a base branch
from
AlyaVKravchenko:develop
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Battle #510
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,128 @@ | ||
from typing import List, Tuple | ||
|
||
|
||
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 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 = start | ||
self.end = end | ||
self.decks: List[Deck] = [] | ||
self.is_drowned = False | ||
self._create_decks() | ||
|
||
def get_deck(self, row, column): | ||
# Find the corresponding deck in the list | ||
pass | ||
def _create_decks(self) -> None: | ||
if self.start[0] == self.end[0]: # Horizontal ship | ||
for col in range(self.start[1], self.end[1] + 1): | ||
self.decks.append(Deck(self.start[0], col)) | ||
elif self.start[1] == self.end[1]: # Vertical ship | ||
for row in range(self.start[0], self.end[0] + 1): | ||
self.decks.append(Deck(row, self.start[1])) | ||
|
||
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, col: int) -> str: | ||
# Check if this cell matches a deck | ||
for deck in self.decks: | ||
if deck.row == row and deck.column == col: | ||
deck.hit() | ||
if all(not d.is_alive for d in self.decks): | ||
self.is_drowned = True | ||
return "Sunk!" if self.is_drowned else "Hit!" | ||
return "Miss!" | ||
|
||
|
||
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] = [] | ||
self._validate_field(ships) | ||
self._place_ships(ships) | ||
|
||
def _place_ships( | ||
self, | ||
ships: List[Tuple[Tuple[int, int], Tuple[int, int]]] | ||
) -> None: | ||
# Place the ships on the field and store them | ||
for ship_coords in ships: | ||
start, end = ship_coords | ||
ship = Ship(start, end) | ||
self.ships.append(ship) | ||
for deck in ship.decks: | ||
self.field[(deck.row, deck.column)] = ship | ||
|
||
def _validate_field( | ||
self, | ||
ships: List[Tuple[Tuple[int, int], Tuple[int, int]]] | ||
) -> None: | ||
ship_sizes = {1: 4, 2: 3, 3: 2, 4: 1} | ||
ship_counts = {1: 0, 2: 0, 3: 0, 4: 0} | ||
|
||
for ship_coords in ships: | ||
start, end = ship_coords | ||
if start[0] == end[0]: | ||
size = end[1] - start[1] + 1 | ||
elif start[1] == end[1]: | ||
size = end[0] - start[0] + 1 | ||
else: | ||
raise ValueError("Ships must be either " | ||
"horizontal or vertical.") | ||
|
||
if size not in ship_counts: | ||
raise ValueError("Invalid ship size.") | ||
ship_counts[size] += 1 | ||
|
||
# Check if the ship sizes match the expected counts | ||
for size, count in ship_counts.items(): | ||
if count != ship_sizes[size]: | ||
raise ValueError(f"Invalid number of ships with {size} decks.") | ||
|
||
# Check that ships are not in neighboring cells | ||
for ship_coords in ships: | ||
for (row, col) in self._get_adjacent_cells(ship_coords): | ||
if (row, col) in self.field: | ||
raise ValueError("Ships cannot be in neighboring cells.") | ||
|
||
def _get_adjacent_cells( | ||
self, | ||
ship_coords: Tuple[Tuple[int, int], Tuple[int, int]] | ||
) -> set: | ||
start, end = ship_coords | ||
adjacent_cells = set() | ||
|
||
# Determine all adjacent cells around the ship | ||
for row in range(start[0] - 1, end[0] + 2): | ||
for col in range(start[1] - 1, end[1] + 2): | ||
if ((row, col) != (start[0], start[1]) and (row, col) | ||
!= (end[0], end[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. The condition here is incorrect for determining adjacent cells. It should include the start and end cells as well, otherwise, it will exclude the actual ship cells from being considered adjacent. Consider revising the logic to correctly identify all adjacent cells. |
||
adjacent_cells.add((row, col)) | ||
return adjacent_cells | ||
|
||
def fire(self, location: Tuple[int, int]) -> str: | ||
if location in self.field: | ||
ship = self.field[location] | ||
return ship.fire(location[0], location[1]) | ||
return "Miss!" | ||
|
||
def print_field(self) -> None: | ||
for row in range(10): | ||
for col in range(10): | ||
if (row, col) in self.field: | ||
ship = self.field[(row, col)] | ||
if ship.is_drowned: | ||
print("x", end=" ") | ||
else: | ||
print("*", end=" ") | ||
else: | ||
print("~", end=" ") | ||
print() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
The logic for checking neighboring cells is not correctly implemented. The
self.field
is populated in_place_ships
, so this check will always pass as the field is empty at this point. Consider restructuring the code to ensure that ships are checked for adjacency before they are placed on the field.