Files
2024-01-15 18:28:14 +01:00

39 lines
1.4 KiB
Python

class Cell:
def __init__(self, pose: tuple = (0, 0), parent = None) -> None:
self.move = [(1, 2), (1, -2), (-1, 2), (-1, -2), (2, 1), (2, -1), (-2, 1), (-2, -1)]
self.pose = pose
self.parent = parent
self.childs = []
self.find = False
def get_path(self) -> list:
path = []
current = self
while current is not None:
path.append(current.pose)
current = current.parent
return path[::-1]
def __eq__(self, other) -> bool:
return isinstance(other, Cell) and self.pose == other.pose
def make_moves(self, destination, paths) -> None:
if isinstance(destination, Cell):
if len(self.childs) < 1:
for move in self.move:
x = self.pose[0] + move[0]
y = self.pose[1] + move[1]
if 0 <= x < 8 and 0 <= y < 8:
new_cell = Cell(pose=(x, y), parent=self)
if destination == new_cell:
self.find = True
paths.append(new_cell.get_path())
else:
self.childs.append(new_cell)
if self.find:
self.childs.clear()
self.parent.childs.remove(self)
else:
for child in self.childs:
child.make_moves(destination, paths)