-3

I am trying to make a python maze generator but I keep getting an IndexError: list index out of range. Any ideas? I'm kinda new to this stuff so I was using the code from rosetta code on maze generation. I am not painting the maze right now I just want the algorithm to work right now.

from random import shuffle, randrange

maze_rows = 10
maze_columns = 10

maze = [[0 for rows in range(maze_rows)] for columns in range(maze_columns)]

def maze_generation(x,y):
    maze[y][x] = 1
    walk = [(x - 1, y), (x, y + 1), (x + 1, y), (x, y - 1)]
    shuffle(walk)

    for (xx, yy) in walk:
        if maze[yy][xx]: continue
        if xx == x:
            maze[max(y,yy)][x] = 1
        if yy == y:
            maze[y][max(x,xx)] = 1

        maze_generation(xx,yy)

maze_generation(randrange(maze_rows),randrange(maze_columns))
Laurel
  • 5,965
  • 14
  • 31
  • 57
Anmol
  • 1

1 Answers1

0

Your code has some resemblance to what rosetta code has. Here is my attempt. I haven't tested it.

from random import shuffle, randrange

def make_maze(w = 16, h = 8):
    vis = [[0] * w + [1] for _ in range(h)] + [[1] * (w + 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
            walk(xx, yy)

    walk(randrange(w), randrange(h))
    return vis

print(make_maze())
muratgu
  • 7,241
  • 3
  • 24
  • 26