Certainly! Below is a Python function that generates an ( n )-dimensional maze using Prim's algorithm. This function starts from a given coordinate (start_coord) and generates a maze by carving passages between cells. The maze is represented by a NumPy array of walls, where each cell has walls in ( 2n ) directions corresponding to the positive and negative directions along each dimension.
import numpy as np
import random
def generate_maze_prim_nD(sizes, start_coord, end_coord):
n = len(sizes) # Number of dimensions
walls_shape = list(sizes) + [2 * n]
walls = np.ones(walls_shape, dtype=bool) # All walls intact
visited = np.zeros(sizes, dtype=bool) # Track visited cells
# Start from start_coord
current_coord = tuple(start_coord)
visited[current_coord] = True
# Initialize walls_list with walls of start_coord
walls_list = []
for d in range(2 * n):
walls_list.append((current_coord, d))
while walls_list:
# Choose a random wall from the list
idx = random.randint(0, len(walls_list) - 1)
cell_coord, wall_direction = walls_list.pop(idx)
cell_coord = list(cell_coord)
neighbor_coord = cell_coord.copy()
dim = wall_direction // 2 # Dimension index
direction = wall_direction % 2 # 0 for negative, 1 for positive direction
# Move to the neighbor in the chosen direction
offset = -1 if direction == 0 else 1
neighbor_coord[dim] += offset
# Check if the neighbor is within bounds
if 0 <= neighbor_coord[dim] < sizes[dim]:
neighbor_coord = tuple(neighbor_coord)
if not visited[neighbor_coord]:
# Remove the wall between cell and neighbor
walls[tuple(cell_coord)][wall_direction] = False
# Also remove the wall on the opposite side of neighbor
opposite_wall_direction = wall_direction ^ 1 # Flip the last bit to get opposite wall
walls[neighbor_coord][opposite_wall_direction] = False
# Mark neighbor as visited
visited[neighbor_coord] = True
# Add neighbor's walls to the walls_list
for d in range(2 * n):
walls_list.append((neighbor_coord, d))
return walls
Explanation:
Imports:
numpy for handling multidimensional arrays.
random for random selection of walls.
Function Definition:
sizes: A list of integers specifying the size of the maze in each dimension.
start_coord: The starting coordinate of the maze (list or tuple).
end_coord: The ending coordinate of the maze (not used in maze generation but can be used for pathfinding later).
Initialization:
n: Number of dimensions.
walls_shape: Shape of the walls array, which is the maze sizes plus an extra dimension for walls.
walls: A NumPy array initialized to True, indicating all walls are intact initially.
visited: A NumPy array to keep track of visited cells, initialized to False.
Starting Point:
Mark the starting cell as visited.
Initialize the walls_list with all walls of the starting cell.
Prim's Algorithm Loop:
While there are walls in the walls_list:
Randomly select a wall and remove it from the list.
Determine the neighbor cell corresponding to the selected wall.
Check if the neighbor is within bounds and not visited.
If so, remove the wall between the current cell and the neighbor.
Mark the neighbor as visited.
Add all walls of the neighbor to the walls_list.
Maze Representation:
Each cell has 2n walls corresponding to the negative and positive direction along each dimension.
The walls array has an additional dimension of size 2n to represent these walls.
Usage Example:
Example usage for a 3D maze of size 4x4x4
sizes = [4, 4, 4]
start_coord = [0, 0, 0]
end_coord = [3, 3, 3] # Can be used for pathfinding later
maze_walls = generate_maze_prim_nD(sizes, start_coord, end_coord)
The maze_walls array contains the maze structure.
You can use this array to visualize the maze or to find a path from start to end.
Notes:
Walls Indexing: Each wall is represented by an integer from 0 to 2n - 1.
For each dimension dim, there are two walls:
2 * dim for the negative direction.
2 * dim + 1 for the positive direction.
Neighbor Calculation:
The neighbor coordinate is calculated by adding -1 (negative direction) or 1 (positive direction) to the current cell's coordinate in the relevant dimension.
Opposite Wall:
The opposite wall in the neighbor cell is found using bitwise XOR with 1 (wall_direction ^ 1).
Visualization:
Visualizing higher-dimensional mazes can be challenging. For 2D mazes, you can create a visual representation using characters or graphical libraries. For 3D or higher-dimensional mazes, consider using projection techniques or simply verify the maze structure through tests.
Testing the Maze:
You can test the maze generation by checking that:
All cells are reachable from the starting cell.
There are no cycles if desired (for a perfect maze).
The walls array correctly represents the passageways.
Feel free to modify and expand upon this function to suit your needs, such as adding pathfinding algorithms to find a route from start_coord to end_coord.