Added options to Recursive Division

This commit is contained in:
Kyler Olsen 2024-11-05 00:05:03 -07:00
parent 514d4577c9
commit 2c6c32c34d
3 changed files with 40 additions and 21 deletions

View File

@ -80,7 +80,10 @@ VEC_MAZE_SIZE = (MAZE_SIZE + 1) // 2
# my_maze = maze.Sidewinder(MAZE_SIZE, run_param=0.2)
# my_maze = maze.Sidewinder(MAZE_SIZE, run_param=0.5)
# my_maze = maze.Sidewinder(MAZE_SIZE, run_param=0.8)
my_maze = maze.RecursiveDivision(MAZE_SIZE)
# my_maze = maze.RecursiveDivision(MAZE_SIZE)
# my_maze = maze.RecursiveDivision(MAZE_SIZE, uniform=False)
# my_maze = maze.RecursiveDivision(MAZE_SIZE, depth_first=False)
my_maze = maze.RecursiveDivision(MAZE_SIZE, binary=True)
# for _ in range(512): my_maze.step()
# for _ in range(2048): my_maze.step()

52
maze.py
View File

@ -523,12 +523,6 @@ class _Window:
def x(self, x: int) -> int: return x + self.__x_start
def y(self, y: int) -> int: return y + self.__y_start
# def rand_x(self) -> int:
# return (random.randint(0, (self.width - 3) // 2) * 2) + 1
# def rand_y(self) -> int:
# return (random.randint(0, (self.height - 3) // 2) * 2) + 1
def vertical_bisect(self, x: int) -> "tuple[_Window, _Window]":
return _Window(
self.__maze,
@ -563,20 +557,36 @@ class _Window:
class RecursiveDivision(Maze):
__cells: list[list[bool]]
# __stack: list[tuple[bool | None, _Window]]
__stack: list[tuple[bool, _Window]]
__stack: list[tuple[bool | None, _Window]]
__split: tuple[bool, int, _Window] | None
__binary: bool
__uniform: bool
__depth_first: bool
__width: int
__height: int
def __init__(self, width: int, height: int | None = None):
def __init__(
self,
width: int,
height: int | None = None,
*,
binary: bool = False,
uniform: bool = True,
depth_first: bool = True,
):
self.__width = width
self.__height = height or width
self.__binary = binary
self.__uniform = uniform
self.__depth_first = depth_first
self.__cells = [
list([(0 < x < self.width-1 and 0 < y < self.height-1)
for x in range(self.width)]) for y in range(self.height)]
self.__stack = [(True, _Window(self,1,self.width,1,self.height))]
self.__stack = [
((True if self.__uniform else None),
_Window(self,1,self.width,1,self.height))]
self.__split = None
@property
@ -594,28 +604,34 @@ class RecursiveDivision(Maze):
if self.__split is not None:
if self.__split[0]:
axis, y, window = self.__split
x = (random.randint(0, (window.width - 3) // 2) * 2)
x = (random.randint(0, (window.width - 1) // 2) * 2)
self.__cells[window.y(y)][window.x(x)] = True
a, b = [(not axis, w) for w in window.horizontal_bisect(y)]
a, b = [
((not axis if self.__uniform else None), w)
for w in window.horizontal_bisect(y)]
else:
axis, x, window = self.__split
y = (random.randint(0, (window.height - 3) // 2) * 2)
y = (random.randint(0, (window.height - 1) // 2) * 2)
self.__cells[window.y(y)][window.x(x)] = True
a, b = [(not axis, w) for w in window.vertical_bisect(x)]
a, b = [
((not axis if self.__uniform else None), w)
for w in window.vertical_bisect(x)]
if a[1].width > 2 and a[1].height > 2: self.__stack.append(a)
if b[1].width > 2 and b[1].height > 2: self.__stack.append(b)
self.__split = None
return True
elif self.__stack:
axis, window = self.__stack.pop()
# if axis is None: axis = bool(random.randint(0,1))
axis, window = self.__stack.pop(-1 if self.__depth_first else 0)
if axis is None: axis = bool(random.randint(0,1))
if axis:
y = (random.randint(0, (window.height - 3) // 2) * 2) + 1
if self.__binary: y = (((window.height - 3) // 4) * 2) + 1
else: y = (random.randint(0, (window.height - 3) // 2) * 2) + 1
for x in range(window.width):
self.__cells[window.y(y)][window.x(x)] = False
self.__split = axis, y, window
else:
x = (random.randint(0, (window.width - 3) // 2) * 2) + 1
if self.__binary: x = (((window.width - 3) // 4) * 2) + 1
else: x = (random.randint(0, (window.width - 3) // 2) * 2) + 1
for y in range(window.height):
self.__cells[window.y(y)][window.x(x)] = False
self.__split = axis, x, window

View File

@ -2,8 +2,8 @@
+ Origin Shift
+ Binary Tree
+ Sidewinder
- Recursive Division
- Binary Division
+ Recursive Division
+ Binary Division
- Prim's Algorithm
- Aldous-Broder Algorithm
- Wilson's Algorithm