Optimizations
This commit is contained in:
parent
dffc6ba4ca
commit
00cb4be9a4
13
maze.py
13
maze.py
|
@ -811,6 +811,7 @@ class Wilson(VectorMaze):
|
||||||
|
|
||||||
def secondary(self, index: tuple[int,int]) -> VectorEnum:
|
def secondary(self, index: tuple[int,int]) -> VectorEnum:
|
||||||
if self.__start is not None:
|
if self.__start is not None:
|
||||||
|
if index in self.__path.keys():
|
||||||
cell: tuple[int,int] = self.__start
|
cell: tuple[int,int] = self.__start
|
||||||
while self.__path.get(cell, VectorEnum.Zero) != VectorEnum.Zero:
|
while self.__path.get(cell, VectorEnum.Zero) != VectorEnum.Zero:
|
||||||
if cell == index: return self.__path[cell]
|
if cell == index: return self.__path[cell]
|
||||||
|
@ -825,6 +826,7 @@ class Wilson(VectorMaze):
|
||||||
self.__path[highlighted] = self.__direction(highlighted, cell)
|
self.__path[highlighted] = self.__direction(highlighted, cell)
|
||||||
if self.__cells[cell[1]][cell[0]] == VectorEnum.Null:
|
if self.__cells[cell[1]][cell[0]] == VectorEnum.Null:
|
||||||
self.__path[cell] = VectorEnum.Zero
|
self.__path[cell] = VectorEnum.Zero
|
||||||
|
self.__optimize_path()
|
||||||
else:
|
else:
|
||||||
cell: tuple[int,int] = self.__start
|
cell: tuple[int,int] = self.__start
|
||||||
while self.__path.get(cell, VectorEnum.Zero) != VectorEnum.Zero:
|
while self.__path.get(cell, VectorEnum.Zero) != VectorEnum.Zero:
|
||||||
|
@ -838,6 +840,17 @@ class Wilson(VectorMaze):
|
||||||
return True
|
return True
|
||||||
else: return False
|
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:
|
def __new_start(self) -> tuple[int, int] | None:
|
||||||
empty = []
|
empty = []
|
||||||
for x in range(self.width):
|
for x in range(self.width):
|
||||||
|
|
Loading…
Reference in New Issue