grid.Grid: finally add that stupid print() method I'm always writing for debugging
grid.Grid: add(), sub() and set() return what they've actually wrote to the cell
This commit is contained in:
parent
55a12f7dc8
commit
abbf1c85e1
@ -1,7 +1,7 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
from .coordinate import Coordinate
|
from .coordinate import Coordinate
|
||||||
from enum import Enum
|
from enum import Enum
|
||||||
from typing import Union, Any
|
from typing import Any, List, Union
|
||||||
|
|
||||||
OFF = False
|
OFF = False
|
||||||
ON = True
|
ON = True
|
||||||
@ -46,7 +46,7 @@ class Grid:
|
|||||||
self.__trackBoundaries(pos)
|
self.__trackBoundaries(pos)
|
||||||
self.__grid[pos] = not self.__default
|
self.__grid[pos] = not self.__default
|
||||||
|
|
||||||
def set(self, pos: Coordinate, value: Any = True):
|
def set(self, pos: Coordinate, value: Any = True) -> Any:
|
||||||
if pos.z is not None:
|
if pos.z is not None:
|
||||||
self.mode3D = True
|
self.mode3D = True
|
||||||
|
|
||||||
@ -56,6 +56,8 @@ class Grid:
|
|||||||
self.__trackBoundaries(pos)
|
self.__trackBoundaries(pos)
|
||||||
self.__grid[pos] = value
|
self.__grid[pos] = value
|
||||||
|
|
||||||
|
return value
|
||||||
|
|
||||||
def get(self, pos: Coordinate) -> Any:
|
def get(self, pos: Coordinate) -> Any:
|
||||||
if pos in self.__grid:
|
if pos in self.__grid:
|
||||||
return self.__grid[pos]
|
return self.__grid[pos]
|
||||||
@ -98,11 +100,11 @@ class Grid:
|
|||||||
else:
|
else:
|
||||||
return self.minX <= pos.x <= self.maxX and self.minY <= pos.y <= self.maxY
|
return self.minX <= pos.x <= self.maxX and self.minY <= pos.y <= self.maxY
|
||||||
|
|
||||||
def add(self, pos: Coordinate, value: Union[float, int] = 1):
|
def add(self, pos: Coordinate, value: Union[float, int] = 1) -> Union[float, int]:
|
||||||
self.set(pos, self.get(pos) + value)
|
return self.set(pos, self.get(pos) + value)
|
||||||
|
|
||||||
def sub(self, pos: Coordinate, value: Union[float, int] = 1):
|
def sub(self, pos: Coordinate, value: Union[float, int] = 1) -> Union[float, int]:
|
||||||
self.set(pos, self.get(pos) - value)
|
return self.set(pos, self.get(pos) - value)
|
||||||
|
|
||||||
def getActiveCells(self):
|
def getActiveCells(self):
|
||||||
return list(self.__grid.keys())
|
return list(self.__grid.keys())
|
||||||
@ -115,6 +117,18 @@ class Grid:
|
|||||||
|
|
||||||
return grid_sum
|
return grid_sum
|
||||||
|
|
||||||
|
def getNeighboursOf(self, pos: Coordinate, includeDefault: bool = False, includeDiagonal: bool = True) \
|
||||||
|
-> List[Coordinate]:
|
||||||
|
neighbours = pos.getNeighbours(
|
||||||
|
includeDiagonal=includeDiagonal,
|
||||||
|
minX=self.minX, minY=self.minY, minZ=self.minZ,
|
||||||
|
maxX=self.maxX, maxY=self.maxY, maxZ=self.maxZ
|
||||||
|
)
|
||||||
|
if includeDefault:
|
||||||
|
return neighbours
|
||||||
|
else:
|
||||||
|
return [x for x in neighbours if self.get(x) != self.__default]
|
||||||
|
|
||||||
def getNeighbourSum(self, pos: Coordinate, includeNegative: bool = True, includeDiagonal: bool = True) \
|
def getNeighbourSum(self, pos: Coordinate, includeNegative: bool = True, includeDiagonal: bool = True) \
|
||||||
-> Union[float, int]:
|
-> Union[float, int]:
|
||||||
neighbour_sum = 0
|
neighbour_sum = 0
|
||||||
@ -169,3 +183,11 @@ class Grid:
|
|||||||
elif mode == GridTransformation.ROTATE_TWICE:
|
elif mode == GridTransformation.ROTATE_TWICE:
|
||||||
self.transform(GridTransformation.ROTATE_RIGHT)
|
self.transform(GridTransformation.ROTATE_RIGHT)
|
||||||
self.transform(GridTransformation.ROTATE_RIGHT)
|
self.transform(GridTransformation.ROTATE_RIGHT)
|
||||||
|
|
||||||
|
def print(self, spacer: str = ""):
|
||||||
|
for y in range(self.minY, self.maxY + 1):
|
||||||
|
for x in range(self.minX, self.maxX + 1):
|
||||||
|
print(self.get(Coordinate(x, y)), end="")
|
||||||
|
print(spacer, end="")
|
||||||
|
|
||||||
|
print()
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user