Python Maze Generation

2024/7/7 6:43:38

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, randrangemaze_rows = 10
maze_columns = 10maze = [[0 for rows in range(maze_rows)] for columns in range(maze_columns)]def maze_generation(x,y):maze[y][x] = 1walk = [(x - 1, y), (x, y + 1), (x + 1, y), (x, y - 1)]shuffle(walk)for (xx, yy) in walk:if maze[yy][xx]: continueif xx == x:maze[max(y,yy)][x] = 1if yy == y:maze[y][max(x,xx)] = 1maze_generation(xx,yy)maze_generation(randrange(maze_rows),randrange(maze_columns))
Answer

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

from random import shuffle, randrangedef make_maze(w = 16, h = 8):vis = [[0] * w + [1] for _ in range(h)] + [[1] * (w + 1)]def walk(x, y):vis[y][x] = 1d = [(x - 1, y), (x, y + 1), (x + 1, y), (x, y - 1)]shuffle(d)for (xx, yy) in d:if vis[yy][xx]: continuewalk(xx, yy)walk(randrange(w), randrange(h))return visprint(make_maze())
https://en.xdnf.cn/q/120445.html

Related Q&A

Why does Pythons `any` function not return True or False? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to repro…

Write a list of lists into csv in Python [duplicate]

This question already has answers here:Writing a Python list of lists to a csv file(12 answers)Closed 5 years ago.I have a list of lists of pure data like thisa=[[1,2,3],[4,5,6],[7,8,9]]How can I write…

Upper limit of points pyplot

Just being a curious George here. But Im handling 279 million data points (x,y) and am wondering if pyplot can scatter such a number?Thanks.

how to delete the u and before the of database table display by python [closed]

Its difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying thi…

python list permutations [duplicate]

This question already has answers here:Closed 12 years ago.Possible Duplicate:How to generate all permutations of a list in Python I am given a list [1,2,3] and the task is to create all the possible …

Repeating Characters in the Middle of a String

Here is the problem I am trying to solve but having trouble solving:Define a function called repeat_middle which receives as parameter one string (with at least one character), and it should return a n…

Remove a big list of of special characters [duplicate]

This question already has answers here:Remove specific characters from a string in Python(27 answers)Closed 5 years ago.I want to remove each of the following special characters from my documents: symb…

How to change a html page from flask to Django [duplicate]

This question already has an answer here:How to specify URLs in Django templates?(1 answer)Closed 7 years ago.I am working on an app that requires changing a flask template to that of Django.How to ch…

How to get the text of Checkbuttons?

Checkbuttons gets generated dynamically and they are getting text from a python list. I need a logic for capturing selected checkbuttons text . As per my research everywhere they are returning the stat…

Working out an equation

Im trying to solve a differential equation numerically, and am writing an equation that will give me an array of the solution to each time point.import numpy as np import matplotlib.pylab as pltpi=np.p…