Skip to content

Commit

Permalink
Manually simulate a stack to replace recursion
Browse files Browse the repository at this point in the history
  • Loading branch information
weilycoder committed Nov 20, 2024
1 parent 82c9f5f commit a3041d8
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions cyaron/maze.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,17 @@ def generate_maze(
):
maze = [[wall for _ in range(width)] for _ in range(height)]

def carve_passages_from(cx, cy):
def carve_passages_from(x, y):
stack = [(x, y)]
d = [(0, -1), (0, 1), (-1, 0), (1, 0)]
random.shuffle(d)
for dx, dy in d:
nx, ny = cx + dx * 2, cy + dy * 2
if 0 <= nx < width and 0 <= ny < height and maze[ny][nx] == wall:
maze[ny][nx] = maze[cy + dy][cx + dx] = way
carve_passages_from(nx, ny)
while len(stack) >= 1:
cx, cy = stack.pop()
random.shuffle(d)
for dx, dy in d:
nx, ny = cx + dx * 2, cy + dy * 2
if 0 <= nx < width and 0 <= ny < height and maze[ny][nx] == wall:
maze[ny][nx] = maze[cy + dy][cx + dx] = way
stack.append((nx, ny))

start_x = random.randrange(0, width)
start_y = random.randrange(0, height)
Expand Down

0 comments on commit a3041d8

Please sign in to comment.