allow getActiveCells() to return only one row/column

annotation/import fixed
This commit is contained in:
Stefan Harmuth 2021-12-13 11:10:38 +01:00
parent e16c21b3bb
commit a507b004f9

View File

@ -2,8 +2,7 @@ from __future__ import annotations
from .coordinate import Coordinate, DistanceAlgorithm from .coordinate import Coordinate, DistanceAlgorithm
from dataclasses import dataclass from dataclasses import dataclass
from enum import Enum from enum import Enum
from math import inf from typing import Any, Dict, List, Optional, Union
from typing import Any, List, Union
OFF = False OFF = False
ON = True ON = True
@ -121,8 +120,13 @@ 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 getActiveCells(self) -> List[Coordinate]: def getActiveCells(self, x: int = None, y: int = None) -> List[Coordinate]:
return list(self.__grid.keys()) if x:
return [c for c in self.__grid.keys() if c.x == x]
elif y:
return [c for c in self.__grid.keys() if c.y == y]
else:
return list(self.__grid.keys())
def getSum(self, includeNegative: bool = True) -> Numeric: def getSum(self, includeNegative: bool = True) -> Numeric:
grid_sum = 0 grid_sum = 0
@ -199,18 +203,18 @@ class Grid:
self.transform(GridTransformation.ROTATE_RIGHT) self.transform(GridTransformation.ROTATE_RIGHT)
def getPath(self, pos_from: Coordinate, pos_to: Coordinate, includeDiagonal: bool, walls: List[Any] = None)\ def getPath(self, pos_from: Coordinate, pos_to: Coordinate, includeDiagonal: bool, walls: List[Any] = None)\
-> List[Coordinate]: -> Union[None, List[Coordinate]]:
@dataclass(frozen=True) @dataclass(frozen=True)
class Node: class Node:
f_cost: int f_cost: int
h_cost: int h_cost: int
parent: Coordinate parent: Optional[Coordinate]
if walls in None: if walls in None:
walls = [self.__default] walls = [self.__default]
openNodes: Dict[Coordinate, Node] = {} openNodes: Dict[Coordinate, Node] = {}
closedNodes: Dict[Coordinate, Node] = {} # Dict[Coordinate, Node] closedNodes: Dict[Coordinate, Node] = {}
openNodes[pos_from] = Node( openNodes[pos_from] = Node(
pos_from.getDistanceTo(pos_to, DistanceAlgorithm.MANHATTAN, includeDiagonal), pos_from.getDistanceTo(pos_to, DistanceAlgorithm.MANHATTAN, includeDiagonal),