SeAIPalette/Palette/algos/utils.py

46 lines
1.5 KiB
Python

from Palette.constants import UP, DOWN, LEFT, RIGHT, FINISHED, BROKEN,\
FILLING_UP, FILLING_DOWN, FILLING_LEFT, FILLING_RIGHT
class Node:
def __init__(self, x, y, father, energy: bool = False):
self.x, self.y = x, y
self._energy = energy
self.direction = None
self.father = father
if self.father is not None:
father_x, father_y = self.father.x, self.father.y
delta_x, delta_y = x-father_x, y-father_y
if delta_x == 0:
if delta_y == 1:
if not self.energy:
self.direction = DOWN
else:
self.direction = FILLING_DOWN
elif delta_y == -1:
if not self.energy:
self.direction = UP
else:
self.direction = FILLING_UP
elif delta_y == 0:
if delta_x == 1:
if not self.energy:
self.direction = RIGHT
else:
self.direction = FILLING_RIGHT
elif delta_x == -1:
if not self.energy:
self.direction = LEFT
else:
self.direction = FILLING_LEFT
if self.direction is None:
raise ValueError(f'invalid delta: ({delta_x}, {delta_y})')
def is_root(self) -> bool:
return self.father is None
@property
def energy(self):
return self._energy