Optimizations

This commit is contained in:
Kyler Olsen 2024-11-07 00:34:17 -07:00
parent dffc6ba4ca
commit 00cb4be9a4
1 changed files with 17 additions and 4 deletions

21
maze.py
View File

@ -811,10 +811,11 @@ class Wilson(VectorMaze):
def secondary(self, index: tuple[int,int]) -> VectorEnum:
if self.__start is not None:
cell: tuple[int,int] = self.__start
while self.__path.get(cell, VectorEnum.Zero) != VectorEnum.Zero:
if cell == index: return self.__path[cell]
cell = self.__next(cell, self.__path[cell])
if index in self.__path.keys():
cell: tuple[int,int] = self.__start
while self.__path.get(cell, VectorEnum.Zero) != VectorEnum.Zero:
if cell == index: return self.__path[cell]
cell = self.__next(cell, self.__path[cell])
return VectorEnum.Null
def step(self) -> bool:
@ -825,6 +826,7 @@ class Wilson(VectorMaze):
self.__path[highlighted] = self.__direction(highlighted, cell)
if self.__cells[cell[1]][cell[0]] == VectorEnum.Null:
self.__path[cell] = VectorEnum.Zero
self.__optimize_path()
else:
cell: tuple[int,int] = self.__start
while self.__path.get(cell, VectorEnum.Zero) != VectorEnum.Zero:
@ -838,6 +840,17 @@ class Wilson(VectorMaze):
return True
else: return False
def __optimize_path(self):
if self.__start is not None:
purge = set(self.__path.keys())
cell: tuple[int,int] = self.__start
purge.remove(cell)
while self.__path.get(cell, VectorEnum.Zero) != VectorEnum.Zero:
cell = self.__next(cell, self.__path[cell])
purge.remove(cell)
for cell in purge:
del self.__path[cell]
def __new_start(self) -> tuple[int, int] | None:
empty = []
for x in range(self.width):