From 5df82d2359e2aa9aa59f1fc5bba573fd045e6936 Mon Sep 17 00:00:00 2001 From: Stefan Harmuth Date: Sat, 10 Dec 2022 14:50:22 +0100 Subject: [PATCH] reversable coordinates; grid.print() can now mark important spots --- tools/coordinate.py | 18 ++++++++++++++---- tools/grid.py | 6 ++++-- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/tools/coordinate.py b/tools/coordinate.py index ebc6fc4..8dea4ef 100644 --- a/tools/coordinate.py +++ b/tools/coordinate.py @@ -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 diff --git a/tools/grid.py b/tools/grid.py index 8e53427..0404366 100644 --- a/tools/grid.py +++ b/tools/grid.py @@ -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="")