reversable coordinates; grid.print() can now mark important spots
This commit is contained in:
parent
35972bffe2
commit
5df82d2359
@ -13,6 +13,7 @@ class DistanceAlgorithm(Enum):
|
||||
CHEBYSHEV = 2
|
||||
CHESSBOARD = 2
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class Coordinate:
|
||||
x: int
|
||||
@ -161,6 +162,12 @@ class Coordinate:
|
||||
step_z = diff.z // steps
|
||||
return [self.__class__(self.x + step_x * i, self.y + step_y * i, self.z + step_z * i) for i in range(steps + 1)]
|
||||
|
||||
def reverse(self) -> Coordinate:
|
||||
if self.z is None:
|
||||
return self.__class__(-self.x, -self.y)
|
||||
else:
|
||||
return self.__class__(-self.x, -self.y, -self.z)
|
||||
|
||||
def __add__(self, other: Coordinate) -> Coordinate:
|
||||
if self.z is None:
|
||||
return self.__class__(self.x + other.x, self.y + other.y)
|
||||
@ -212,14 +219,14 @@ class Coordinate:
|
||||
else:
|
||||
return "%s(x=%d, y=%d, z=%d)" % (self.__class__.__name__, self.x, self.y, self.z)
|
||||
|
||||
@staticmethod
|
||||
def generate(from_x: int, to_x: int, from_y: int, to_y: int,
|
||||
@classmethod
|
||||
def generate(cls, from_x: int, to_x: int, from_y: int, to_y: int,
|
||||
from_z: int = None, to_z: int = None) -> List[Coordinate]:
|
||||
if from_z is None or to_z is None:
|
||||
return [self.__class__(x, y) for x in range(from_x, to_x + 1) for y in range(from_y, to_y + 1)]
|
||||
return [cls(x, y) for x in range(from_x, to_x + 1) for y in range(from_y, to_y + 1)]
|
||||
else:
|
||||
return [
|
||||
self.__class__(x, y, z)
|
||||
cls(x, y, z)
|
||||
for x in range(from_x, to_x + 1)
|
||||
for y in range(from_y, to_y + 1)
|
||||
for z in range(from_z, to_z + 1)
|
||||
@ -266,7 +273,10 @@ class HexCoordinate(Coordinate):
|
||||
if minX <= (self + x).x <= maxX and minY <= (self + x).y <= maxY and minZ <= (self + x).z <= maxZ
|
||||
]
|
||||
|
||||
|
||||
HexCoordinateR = HexCoordinate
|
||||
|
||||
|
||||
class HexCoordinateF(HexCoordinate):
|
||||
"""
|
||||
https://www.redblobgames.com/grids/hexagons/#coordinates-cube
|
||||
|
||||
@ -357,10 +357,12 @@ class Grid:
|
||||
put_y = y
|
||||
put_x += 1
|
||||
|
||||
def print(self, spacer: str = "", true_char: str = None, false_char: str = " "):
|
||||
def print(self, spacer: str = "", true_char: str = None, false_char: str = " ", mark: list = None):
|
||||
for y in range(self.minY, self.maxY + 1):
|
||||
for x in range(self.minX, self.maxX + 1):
|
||||
if true_char:
|
||||
if mark and Coordinate(x, y) in mark:
|
||||
print("X", end="")
|
||||
elif true_char:
|
||||
print(true_char if self.get(Coordinate(x, y)) else false_char, end="")
|
||||
else:
|
||||
print(self.get(Coordinate(x, y)), end="")
|
||||
|
||||
Loading…
Reference in New Issue
Block a user