From 82c9f5fd4a5de802ce752cdfc3360f86f2485917 Mon Sep 17 00:00:00 2001 From: weilycoder Date: Wed, 20 Nov 2024 18:49:59 +0800 Subject: [PATCH 1/3] Support generating maze maps --- cyaron/__init__.py | 1 + cyaron/maze.py | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 cyaron/maze.py diff --git a/cyaron/__init__.py b/cyaron/__init__.py index ec5f271..3b2e2d8 100644 --- a/cyaron/__init__.py +++ b/cyaron/__init__.py @@ -15,6 +15,7 @@ from .graph import Edge, Graph from .io import IO from .math import * +from .maze import * from .merger import Merger from .polygon import Polygon from .sequence import Sequence diff --git a/cyaron/maze.py b/cyaron/maze.py new file mode 100644 index 0000000..80b57ea --- /dev/null +++ b/cyaron/maze.py @@ -0,0 +1,35 @@ +import random + +__all__ = ["generate_maze"] + + +def generate_maze( + width: int, + height: int, + *, + wall: str = "#", + way: str = ".", + start: str = "S", + end: str = "T", +): + maze = [[wall for _ in range(width)] for _ in range(height)] + + def carve_passages_from(cx, cy): + 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) + + start_x = random.randrange(0, width) + start_y = random.randrange(0, height) + maze[start_y][start_x] = start + carve_passages_from(start_x, start_y) + + end_x, end_y = random.choice([(x, y) for x in range(width) + for y in range(height) if maze[y][x] == way]) + maze[end_y][end_x] = end + + return maze From a3041d8b6e0d5a25c7b928f0f7ab66792d38f2e3 Mon Sep 17 00:00:00 2001 From: weilycoder Date: Wed, 20 Nov 2024 19:21:42 +0800 Subject: [PATCH 2/3] Manually simulate a stack to replace recursion --- cyaron/maze.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/cyaron/maze.py b/cyaron/maze.py index 80b57ea..8212027 100644 --- a/cyaron/maze.py +++ b/cyaron/maze.py @@ -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) From 7ae894e9537fa49909cf220d1d80db8b4405f18f Mon Sep 17 00:00:00 2001 From: weilycoder Date: Fri, 6 Dec 2024 22:27:44 +0800 Subject: [PATCH 3/3] Rename file --- cyaron/__init__.py | 2 +- cyaron/{maze.py => misc.py} | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename cyaron/{maze.py => misc.py} (100%) diff --git a/cyaron/__init__.py b/cyaron/__init__.py index 3b2e2d8..c6d3fc5 100644 --- a/cyaron/__init__.py +++ b/cyaron/__init__.py @@ -15,7 +15,7 @@ from .graph import Edge, Graph from .io import IO from .math import * -from .maze import * +from .misc import * from .merger import Merger from .polygon import Polygon from .sequence import Sequence diff --git a/cyaron/maze.py b/cyaron/misc.py similarity index 100% rename from cyaron/maze.py rename to cyaron/misc.py