Added BinaryTree Alg

This commit is contained in:
Kyler Olsen 2024-11-04 20:21:05 -07:00
parent bb74cc58e5
commit ea0ec289a7
3 changed files with 104 additions and 6 deletions

View File

@ -58,10 +58,13 @@ class MazeVisualizer:
pygame.quit()
sys.exit()
my_maze = maze.RecursiveBacktracker(63)
# my_maze = maze.RecursiveBacktracker(63)
# my_maze = maze.VectorWrapper(maze.OriginShift(32))
my_maze = maze.VectorWrapper(maze.BinaryTree(32, bias=maze.VectorEnum.Up | maze.VectorEnum.Left))
# for _ in range(512): my_maze.step()
# for _ in range(2048): my_maze.step()
# for _ in range(16384): my_maze.step()
visualizer = MazeVisualizer(my_maze)
visualizer.run()

103
maze.py
View File

@ -1,4 +1,5 @@
import abc, enum, random
from types import UnionType
class Maze(abc.ABC):
@ -54,10 +55,26 @@ class Maze(abc.ABC):
class VectorEnum(enum.Enum):
Zero = 0
Up = 1
Left = 2
Down = 3
Right = 4
Up = 2
Down = 4
Left = 8
Right = 16
def __or__(self, value: "VectorEnum | int") -> int:
if not isinstance(value, int): value = value.value
return self.value | value
def __ror__(self, value: "VectorEnum | int") -> int:
if not isinstance(value, int): value = value.value
return self.value | value
def __and__(self, value: "VectorEnum | int") -> "VectorEnum":
if not isinstance(value, int): value = value.value
return VectorEnum(self.value & value)
def __rand__(self, value: "VectorEnum | int") -> "VectorEnum":
if not isinstance(value, int): value = value.value
return VectorEnum(self.value & value)
class VectorMaze(Maze):
@ -240,3 +257,81 @@ class OriginShift(VectorMaze):
if x == self.width - 1 and y == self.height - 1: return VectorEnum.Zero
elif x == self.width - 1: return VectorEnum.Down
else: return VectorEnum.Right
class BinaryTree(VectorMaze):
__cells: list[list[VectorEnum]]
__width: int
__height: int
__bias: int
__x: int
__y: int
def __init__(self, width: int, height: int | None = None, *, bias: int | None = None ):
self.__width = width
self.__height = height or width
self.__bias = bias or (VectorEnum.Up | VectorEnum.Left)
self.__x = 0
self.__y = 0
self.__cells = [list([VectorEnum.Zero for x in range(self.width)]) for y in range(self.height)]
@property
def width(self) -> int:
return self.__width
@property
def height(self) -> int:
return self.__height
@property
def highlighted(self) -> tuple[int,int] | None:
if self.__x < self.width and self.__y < self.height:
return self.__x, self.__y
def __getitem__(self, index: tuple[int,int]) -> VectorEnum:
x, y = index
return self.__cells[y][x]
def step(self) -> bool:
if self.highlighted is not None:
neighbors = self._neighbors(self.highlighted)
if neighbors:
cell = neighbors[random.randint(0,len(neighbors)-1)]
self.__cells[self.__y][self.__x] = self.__direction(self.highlighted, cell)
else:
self.__cells[self.__y][self.__x] = VectorEnum.Zero
self.__x += 1
if self.__x >= self.width:
self.__x = 0
self.__y += 1
return True
else: return False
def __direction(self, start: tuple[int,int], end: tuple[int,int]) -> VectorEnum:
if start[0] - end[0] > 0: return VectorEnum.Left
if start[0] - end[0] < 0: return VectorEnum.Right
if start[1] - end[1] > 0: return VectorEnum.Up
if start[1] - end[1] < 0: return VectorEnum.Down
return VectorEnum.Zero
def _neighbors(self, index: tuple[int,int]) -> list[tuple[int,int]]:
neighbors: list[tuple[int,int]] = []
x, y = index
if x - 1 >= 0 and (self.__bias & VectorEnum.Left) == VectorEnum.Left:
neighbors.append((x - 1,y))
if x + 1 < self.width and (self.__bias & VectorEnum.Right) == VectorEnum.Right:
neighbors.append((x + 1,y))
if y - 1 >= 0 and (self.__bias & VectorEnum.Up) == VectorEnum.Up:
neighbors.append((x,y - 1))
if y + 1 < self.height and (self.__bias & VectorEnum.Down) == VectorEnum.Down:
neighbors.append((x,y + 1))
return neighbors

View File

@ -1,6 +1,6 @@
+ Recursive Backtracking
+ Origin Shift
- Binary Tree
+ Binary Tree
- Sidewinder
- Recursive Division
- Binary Division