* 5.6_01 move enums out of Ship class

This commit is contained in:
Dmitry Belyaev 2024-04-29 18:48:39 +03:00
parent fc813b6d5c
commit ee91d98383

View File

@ -55,13 +55,13 @@ True
>>> Ship(5) >>> Ship(5)
Traceback (most recent call last): Traceback (most recent call last):
... ...
ValueError: 5 is not a valid Ship.ShipSize ValueError: 5 is not a valid ShipSize
>>> Ship(1, 1)._tp, Ship(1, 2)._tp >>> Ship(1, 1)._tp, Ship(1, 2)._tp
(1, 2) (1, 2)
>>> Ship(1, 3)._tp >>> Ship(1, 3)._tp
Traceback (most recent call last): Traceback (most recent call last):
... ...
ValueError: 3 is not a valid Ship.ShipOrientation ValueError: 3 is not a valid ShipOrientation
>>> s = Ship(1) >>> s = Ship(1)
>>> {s[0] == 1, len(s) == 1} >>> {s[0] == 1, len(s) == 1}
{True} {True}
@ -71,7 +71,7 @@ ValueError: 3 is not a valid Ship.ShipOrientation
>>> s[0] = 4 >>> s[0] = 4
Traceback (most recent call last): Traceback (most recent call last):
... ...
ValueError: 4 is not a valid Ship.DeckStatus ValueError: 4 is not a valid DeckStatus
>>> s2 = Ship(2) >>> s2 = Ship(2)
>>> s2._cells = [1, 2] >>> s2._cells = [1, 2]
>>> s2._cells >>> s2._cells
@ -350,21 +350,24 @@ class NonNegativeIntField:
setattr(instance, self.name, value) setattr(instance, self.name, value)
class ShipSize(EnumOrdering):
ONE_DECK = 1
TWO_DECKS = 2
THREE_DECKS = 3
FOUR_DECKS = 4
class ShipOrientation(EnumOrdering):
HORIZONTAL = 1
VERTICAL = 2
class DeckStatus(EnumOrdering):
OK = 1
DAMAGED = 2
class Ship: class Ship:
class ShipSize(EnumOrdering):
ONE_DECK = 1
TWO_DECKS = 2
THREE_DECKS = 3
FOUR_DECKS = 4
class ShipOrientation(EnumOrdering):
HORIZONTAL = 1
VERTICAL = 2
class DeckStatus(EnumOrdering):
OK = 1
DAMAGED = 2
pole_size: int = 10 pole_size: int = 10
_length: int = IntEnumField(ShipSize) _length: int = IntEnumField(ShipSize)
@ -384,7 +387,7 @@ class Ship:
cells: Optional[List[DeckStatus]] = None, cells: Optional[List[DeckStatus]] = None,
): ):
self._length, self._tp, self._x, self._y = length, tp, x, y self._length, self._tp, self._x, self._y = length, tp, x, y
self._cells = cells or [self.DeckStatus.OK] * self._length self._cells = cells or [DeckStatus.OK] * self._length
def __repr__(self): def __repr__(self):
return f"{self.__class__.__name__}{(self._length, self._tp, self._x, self._y, self._cells)!r}" return f"{self.__class__.__name__}{(self._length, self._tp, self._x, self._y, self._cells)!r}"
@ -400,11 +403,11 @@ class Ship:
@property @property
def _is_move(self) -> bool: def _is_move(self) -> bool:
return all(cell == self.DeckStatus.OK for cell in self) return all(cell == DeckStatus.OK for cell in self)
@property @property
def is_alive(self) -> bool: def is_alive(self) -> bool:
return any(cell == self.DeckStatus.OK for cell in self) return any(cell == DeckStatus.OK for cell in self)
def set_start_coords(self, x: int, y: int): def set_start_coords(self, x: int, y: int):
self._x, self._y = x, y self._x, self._y = x, y
@ -414,7 +417,7 @@ class Ship:
@property @property
def is_horizontal(self): def is_horizontal(self):
return self._tp == self.ShipOrientation.HORIZONTAL return self._tp == ShipOrientation.HORIZONTAL
def move(self, go: int): def move(self, go: int):
if self._is_move: if self._is_move:
@ -486,7 +489,7 @@ class Ship:
left, top, right, bottom = self.rect left, top, right, bottom = self.rect
if left <= x < right and top <= y < bottom: if left <= x < right and top <= y < bottom:
a, b = ((y, top), (x, left))[self.is_horizontal] a, b = ((y, top), (x, left))[self.is_horizontal]
self[a - b] = self.DeckStatus.DAMAGED self[a - b] = DeckStatus.DAMAGED
return True return True
return False return False
@ -557,7 +560,7 @@ class GamePole:
координаты в виде кортежей в формате (x, y) | (индекс столбца, индекс строки) координаты в виде кортежей в формате (x, y) | (индекс столбца, индекс строки)
""" """
free_places = [] free_places = []
is_horizontal = tp == Ship.ShipOrientation.HORIZONTAL is_horizontal = tp == ShipOrientation.HORIZONTAL
pole_s = pole pole_s = pole
if not is_horizontal: if not is_horizontal:
pole_s = list(zip(*pole)) pole_s = list(zip(*pole))
@ -595,10 +598,10 @@ class GamePole:
def init(self): def init(self):
self._ships.clear() self._ships.clear()
pole = self.get_pole_list() pole = self.get_pole_list()
sizes = Ship.ShipSize.values()[::-1] sizes = ShipSize.values()[::-1]
for ship_size, ship_count in zip(sizes, range(1, len(sizes) + 1)): for ship_size, ship_count in zip(sizes, range(1, len(sizes) + 1)):
for _ in range(ship_count): for _ in range(ship_count):
tp = random.choice(Ship.ShipOrientation.values()) tp = random.choice(ShipOrientation.values())
self.place_new_ship(pole, ship_size, tp) self.place_new_ship(pole, ship_size, tp)
def get_ships(self) -> List[Ship]: def get_ships(self) -> List[Ship]: