Search code examples
python-3.xmaze

How can I increase corridor width in a maze?


I'm using a maze generator code for python from the link here(can also be seen below): https://rosettacode.org/wiki/Maze_generation

I'm trying to increase the width of the corridors of the maze as I'm converting the maze to a height map. However, the corridors are too tight to fit my character in the maze.

I've tried to change some of the settings of the code below, but was unable to get the desired outcome without making the maze behave weirdly.

def make_maze(w = 16, h = 8):
    vis = [[0] * w + [1] for _ in range(h)] + [[1] * (w + 1)]
    ver = [["|  "] * w + ['|'] for _ in range(h)] + [[]]
    hor = [["+--"] * w + ['+'] for _ in range(h + 1)]

    def walk(x, y):
        vis[y][x] = 1

        d = [(x - 1, y), (x, y + 1), (x + 1, y), (x, y - 1)]
        shuffle(d)
        for (xx, yy) in d:
            if vis[yy][xx]: continue
            if xx == x: hor[max(y, yy)][x] = "+  "
            if yy == y: ver[y][max(x, xx)] = "   "
            walk(xx, yy)

    walk(randrange(w), randrange(h))

    s = ""
    for (a, b) in zip(hor, ver):
        s += ''.join(a + ['\n'] + b + ['\n'])
    return s

Solution

  • The code provided is hardcoded; in addition the maze representation in memory, is mixed with the maze generation, and the maze visual output, making it rather inflexible and difficult to modify. There is also little possibility to use this maze with a search algorithm.
    This maze is also missing its entry and exit points.

    Nevertheless, I added several parameters that allow some measure of scaling of the maze corridors, both horizontally, and vertically.

    Going further will require refactoring the generator to separate the generation, from the representation in memory, from its graphical output, and allow for a representation of the variouth paths through the maze.

    import random
    
    
    def make_maze(w = 16, h = 8, scale=0):
    
        h0, h1, h2, h3 = "+--", "+  ", "|  ", "   "
        h0 += scale * '----'
        h1 += scale * '    '
        h2 += scale * '    '
        h3 += scale * '    '
        vis = [[0] * w + [1] for _ in range(h)] + [[1] * (w + 1)]
        ver = [[h2] * w + ['|'] for _ in range(h)] + [[]]
        hor = [[h0] * w + ['+'] for _ in range(h + 1)]
    
        def walk(x, y):
            vis[y][x] = 1
    
            d = [(x - 1, y), (x, y + 1), (x + 1, y), (x, y - 1)]
            random.shuffle(d)
            for (xx, yy) in d:
                if vis[yy][xx]: continue
                if xx == x: hor[max(y, yy)][x] = h1
                if yy == y: ver[y][max(x, xx)] = h3
                walk(xx, yy)
    
        walk(random.randrange(w), random.randrange(h))
    
        s = ""
        for (a, b) in zip(hor, ver):
            s += ''.join(a + ['\n'] + b + ['\n'])
            for _ in range(scale):
                s += ''.join(b + ['\n'])
        return s
    
    
    
    print(make_maze(scale=0))
    print('\n\n')
    print(make_maze(scale=1))
    print('\n\n')
    print(make_maze(scale=2))
    print('\n\n')
    print(make_maze(scale=3))
    print('\n\n')
    

    output maze scale: 0:

    +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
    |              |        |           |           |
    +  +--+--+  +  +  +--+  +  +--+--+  +  +  +--+--+
    |  |     |  |  |  |     |  |     |  |  |  |     |
    +  +  +  +  +--+  +  +--+  +  +  +  +  +--+  +  +
    |     |  |        |  |     |  |  |  |     |  |  |
    +--+--+  +--+  +--+  +  +--+--+  +  +--+  +  +  +
    |        |  |  |  |  |  |        |     |  |  |  |
    +  +--+--+  +  +  +  +  +--+  +  +--+  +  +  +  +
    |  |  |     |     |  |  |     |  |     |  |  |  |
    +  +  +  +--+--+--+  +  +  +--+--+  +--+  +  +  +
    |     |              |  |  |     |     |     |  |
    +--+  +--+--+--+--+--+  +  +  +  +--+  +  +--+  +
    |  |        |           |  |  |  |     |  |     |
    +  +--+--+  +--+--+  +--+  +  +  +  +--+--+  +  +
    |                    |        |              |  |
    +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
    

    output maze scale: 1:

    +------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+
    |                                  |      |                           |                                         |
    |                                  |      |                           |                                         |
    +      +------+      +------+      +      +      +      +------+      +      +------+      +------+------+------+
    |      |             |                    |      |             |      |             |             |             |
    |      |             |                    |      |             |      |             |             |             |
    +------+      +------+------+------+------+      +------+      +      +------+      +------+      +------+      +
    |             |                           |             |      |             |             |             |      |
    |             |                           |             |      |             |             |             |      |
    +      +      +      +------+------+      +      +------+      +------+      +------+      +------+      +      +
    |      |      |                    |             |             |             |                    |      |      |
    |      |      |                    |             |             |             |                    |      |      |
    +      +------+------+------+      +------+------+      +------+      +------+------+------+------+      +      +
    |      |                    |                    |             |                                         |      |
    |      |                    |                    |             |                                         |      |
    +      +      +------+      +------+------+      +------+      +------+------+------+------+------+------+      +
    |             |             |                    |             |                                         |      |
    |             |             |                    |             |                                         |      |
    +      +------+      +------+      +------+------+      +------+      +------+------+------+------+      +      +
    |      |      |      |             |             |                    |             |             |      |      |
    |      |      |      |             |             |                    |             |             |      |      |
    +      +      +      +      +------+------+      +------+------+------+      +      +------+      +      +      +
    |             |                                                              |                    |             |
    |             |                                                              |                    |             |
    +------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+
    

    output maze scale: 2:

    +----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+
    |          |                                |                                |                                                                 |                                |
    |          |                                |                                |                                                                 |                                |
    |          |                                |                                |                                                                 |                                |
    +          +          +          +----------+          +----------+          +          +----------+----------+----------+          +          +----------+----------+          +
    |                     |                     |                     |                     |                                           |                                |          |
    |                     |                     |                     |                     |                                           |                                |          |
    |                     |                     |                     |                     |                                           |                                |          |
    +          +----------+----------+          +----------+          +----------+----------+          +----------+----------+----------+----------+----------+          +          +
    |                                |                                |                     |          |                     |                     |                     |          |
    |                                |                                |                     |          |                     |                     |                     |          |
    |                                |                                |                     |          |                     |                     |                     |          |
    +----------+----------+          +----------+----------+----------+          +----------+          +          +          +          +          +          +----------+          +
    |                     |                                                      |                     |          |                     |          |                     |          |
    |                     |                                                      |                     |          |                     |          |                     |          |
    |                     |                                                      |                     |          |                     |          |                     |          |
    +          +          +          +----------+----------+----------+----------+          +----------+          +----------+----------+----------+----------+          +          +
    |          |                     |                                                      |          |                                                      |          |          |
    |          |                     |                                                      |          |                                                      |          |          |
    |          |                     |                                                      |          |                                                      |          |          |
    +          +----------+----------+          +----------+          +----------+----------+          +          +----------+----------+----------+          +          +          +
    |          |                     |                     |          |                                |                     |                     |          |          |          |
    |          |                     |                     |          |                                |                     |                     |          |          |          |
    |          |                     |                     |          |                                |                     |                     |          |          |          |
    +          +----------+          +----------+          +----------+          +----------+----------+----------+----------+          +          +          +          +          +
    |          |                                |                     |                                                                 |          |          |          |          |
    |          |                                |                     |                                                                 |          |          |          |          |
    |          |                                |                     |                                                                 |          |          |          |          |
    +          +          +----------+          +----------+          +----------+----------+----------+----------+----------+----------+          +          +          +          +
    |                     |                                |                                                                                       |                                |
    |                     |                                |                                                                                       |                                |
    |                     |                                |                                                                                       |                                |
    +----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+
    

    output maze scale: 3:

    +--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+
    |                                                                                                                                                                                                                                |              |
    |                                                                                                                                                                                                                                |              |
    |                                                                                                                                                                                                                                |              |
    |                                                                                                                                                                                                                                |              |
    +              +--------------+--------------+--------------+--------------+              +--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+              +              +
    |                             |                             |                             |                             |                                                           |                             |                             |
    |                             |                             |                             |                             |                                                           |                             |                             |
    |                             |                             |                             |                             |                                                           |                             |                             |
    |                             |                             |                             |                             |                                                           |                             |                             |
    +--------------+--------------+              +              +              +--------------+              +              +              +--------------+              +              +              +--------------+--------------+              +
    |                                            |                             |                             |              |                             |              |                             |                             |              |
    |                                            |                             |                             |              |                             |              |                             |                             |              |
    |                                            |                             |                             |              |                             |              |                             |                             |              |
    |                                            |                             |                             |              |                             |              |                             |                             |              |
    +--------------+--------------+--------------+--------------+--------------+              +--------------+              +--------------+--------------+              +              +--------------+              +              +              +
    |              |                                                                          |                             |                                            |              |                             |                             |
    |              |                                                                          |                             |                                            |              |                             |                             |
    |              |                                                                          |                             |                                            |              |                             |                             |
    |              |                                                                          |                             |                                            |              |                             |                             |
    +              +              +--------------+--------------+              +--------------+--------------+              +              +--------------+--------------+              +              +--------------+--------------+--------------+
    |              |                                            |                                            |                             |                                            |                                            |              |
    |              |                                            |                                            |                             |                                            |                                            |              |
    |              |                                            |                                            |                             |                                            |                                            |              |
    |              |                                            |                                            |                             |                                            |                                            |              |
    +              +--------------+--------------+              +--------------+--------------+              +--------------+--------------+--------------+--------------+              +--------------+--------------+              +              +
    |                                            |              |                             |                             |                                            |                                            |                             |
    |                                            |              |                             |                             |                                            |                                            |                             |
    |                                            |              |                             |                             |                                            |                                            |                             |
    |                                            |              |                             |                             |                                            |                                            |                             |
    +--------------+--------------+              +              +              +              +--------------+              +              +--------------+              +--------------+--------------+--------------+--------------+              +
    |                                            |                             |              |                             |                             |                                                                                         |
    |                                            |                             |              |                             |                             |                                                                                         |
    |                                            |                             |              |                             |                             |                                                                                         |
    |                                            |                             |              |                             |                             |                                                                                         |
    +              +--------------+--------------+--------------+--------------+              +              +--------------+--------------+              +--------------+--------------+--------------+--------------+--------------+--------------+
    |                                                                                         |                                                                                                                                                     |
    |                                                                                         |                                                                                                                                                     |
    |                                                                                         |                                                                                                                                                     |
    |                                                                                         |                                                                                                                                                     |
    +--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+