Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python3
- import sys, argparse, random
- argparser = argparse.ArgumentParser(description='ASCII Maze Generator')
- argparser.add_argument('width', type=int, help='Width of the maze, in cells')
- argparser.add_argument('height', type=int, help='Height of the maze, in cells')
- argparser.add_argument('--characters', default=None, help='Characters to use in place of \'+|- \', in that order')
- argparser.add_argument('--seed', type=int, default=None, help='Seed for the random number generator')
- argparser.add_argument('--fullwidth', action='store_true', help='Use fullwidth defaults if no characters specified')
- args = argparser.parse_args()
- # Set the default character set if none specified
- if args.characters is None:
- if args.fullwidth:
- args.characters = ' -|+'
- else:
- args.characters = ' -|+'
- # Directions array, for finding adjacent cells
- directions = [(-1, 0), (0, -1), (1, 0), (0, 1)]
- def generateMaze(width, height, chars, seed=None):
- """Generate a maze using a depth-first search based algorithm"""
- rng = random.Random()
- if seed is not None:
- rng.seed(seed)
- # Create initial maze 2d array with no empty space
- maze = [[1]*(2*width+1) for _ in range(2*height+1)]
- # Add empty space for each cell
- for x in range(width):
- for y in range(height):
- maze[2*y+1][2*x+1] = 0
- cells = [] # Stack containing visited cells
- visited = 1 # Number of visited cells
- # Select random cell to start at
- currentCell = [rng.randrange(width), rng.randrange(height)]
- while visited < width * height:
- adjacent = []
- # Find adjacent cells to current one that haven't had any walls removed yet
- for d in directions:
- cell = currentCell.copy()
- cell[0] += d[0]
- cell[1] += d[1]
- # Skip cells outside of the maze area
- if not ((0 <= cell[0] < height) and (0 <= cell[1] < width)):
- continue
- if all(maze[cell[0]*2+1+z[0]][cell[1]*2+1+z[1]] for z in directions):
- # All walls of cell still exist, so add to list along with direction
- adjacent.append((cell, d));
- if adjacent:
- cell, d = rng.choice(adjacent)
- # Remove the wall between the current cell and random adjacent one
- maze[currentCell[0]*2+1+d[0]][currentCell[1]*2+1+d[1]] = 0
- cells.append(currentCell)
- currentCell = cell;
- visited += 1
- else:
- currentCell = cells.pop()
- # Translate binary maze to ascii/unicode characters
- char_maze = []
- for y in range(2*height+1):
- row = ''
- for x in range(2*width+1):
- if not maze[y][x]:
- # Character is empty space, use space character
- row += chars[0]
- else:
- # Character is a wall
- vertical = False
- horizontal = False
- # Determine whether wall has adjacent walls horizontally or vertically
- for i, d in enumerate(directions):
- ny, nx = y+d[0], x+d[1]
- if not ((0 <= ny < 2*height+1) and (0 <= nx < 2*width+1)):
- continue
- if i%2 == 0 and maze[ny][nx]:
- vertical = True
- if i%2 == 1 and maze[ny][nx]:
- horizontal = True
- # Add corresponding character for wall
- row += chars[horizontal + 2*vertical]
- char_maze.append(row)
- return '\n'.join(char_maze)
- print(generateMaze(args.width, args.height, args.characters, args.seed))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement