build a perfect maze recursively in python

2024/7/7 6:56:31

I have this project to build a perfect maze recursively by using python. I have a MyStack class which creates a stack to track the path that I go through. And a Cell class which represent each square within the maze and store some information. I think I complete the code but IDLE gives me some error that I couldn't figure out. Here is the code.

from random import *
from graphics import *class MyStack:def __init__(self):self.S = []def push(self, item):self.S.insert(0, item)def pop(self):return self.S.pop(0)def isEmpty(self):return True if len(self.S) == 0 else Falsedef size(self):return len(self.S)class Maze:def __init__(self, N):self.size = Nself.maze = [[i for i in range(N + 2)] for i in range(N + 2)]for r in range(self.size + 2):for c in range(self.size + 2):self.maze[r][c] = Cell()def walk(self, s, x, y):neighboor = [(x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1)]if s.size() == self.size**2: returnelse:new = choice(neighboor)while self.maze[new[0]][new[1]].getVisit():while new[0] < 1 or new[1] > self.size:new = choice(neighboor)if neighboor != []: new = choice(neighboor.remove(new))else:temp = s.pop(s)x, y = temp[0], temp[1]self.walk(s, x, y)if new == neighboor[0]:self.maze[x][y].changeNorth()self.maze[new[0]][new[1]].changeSouth()elif new == neighboor[1]:self.maze[x][y].changeSouth()self.maze[new[0]][new[1]].changeNorth()elif new == neighboor[2]:self.maze[x][y].changeEast()self.maze[new[0]][new[1]].changeWest()elif new == neighboor[3]:self.maze[x][y].changeWest()self.maze[new[0]][new[1]].changeEast()s.push(new)self.walk(s, new[0], new[1])def search(self):startX, startY = randint(1, self.size), randint(1, self.size)s = MyStack()temp = (startX, startY)s.push(temp)self.maze[startX][startY].changeVisit()self.walk(s, startX, startY)def draw(self):win = GraphWin()startXY = Point(27, 27)start = Circle(startXY, 5)start.setOutline('orange')start.setFill('orange')start.draw(win)x, y = 20, 20for r in range(1, self.size + 1):for c in range(1, self.size + 1):if self.maze[r][c].getNorth():unit = Line(Point(x, y), Point(x + 15, y))unit.draw(win)x, y = x + 15, yx, y = 20, y + 15x, y = 20, 20for c in range(1, self.size + 1):for r in range(1, self.size + 1):if self.maze[r][c].getWest():#print(self.maze[r][c].getWest())unit = Line(Point(x, y), Point(x, y + 15))unit.draw(win)x, y = x, y + 15x, y = x + 15, 20x, y = 20, self.size * 15 + 20for c in range(1, self.size + 1):if self.maze[self.size][c].getSouth():unit = Line(Point(x, y), Point(x + 15, y))unit.draw(win)x, y = x + 15, yx, y = self.size * 15 + 20, 20for r in range(1, self.size + 1):if self.maze[self.size][c].getEast():unit = Line(Point(x, y), Point(x, y + 15))unit.draw(win)x, y = x, y + 15class Cell:def __init__(self):#self.x = x#self.y = yself.north = Trueself.south = Trueself.east = Trueself.west = Trueself.visit = Falsedef changeVisit(self):self.visit = Truedef changeNorth(self):self.north = Falsedef changeSouth(self):self.south = Falsedef changeEast(self):self.east = Falsedef changeWest(self):self.west = Falsedef getVisit(self):return self.visitdef getNorth(self):return self.northdef getSouth(self):return self.southdef getEast(self):return self.eastdef getWest(self):return self.west

Here is the Error I got:

>>> a = Maze(5)
>>> a.search()
Traceback (most recent call last):File "<pyshell#1>", line 1, in <module>a.search()File "C:\Users\Serena\Desktop\XAproject\Stack.py", line 91, in searchself.walk(s, startX, startY)File "C:\Users\Serena\Desktop\XAproject\Stack.py", line 76, in walkself.walk(s, new[0], new[1])File "C:\Users\Serena\Desktop\XAproject\Stack.py", line 76, in walkself.walk(s, new[0], new[1])File "C:\Users\Serena\Desktop\XAproject\Stack.py", line 76, in walkself.walk(s, new[0], new[1])File "C:\Users\Serena\Desktop\XAproject\Stack.py", line 76, in walkself.walk(s, new[0], new[1])File "C:\Users\Serena\Desktop\XAproject\Stack.py", line 76, in walkself.walk(s, new[0], new[1])File "C:\Users\Serena\Desktop\XAproject\Stack.py", line 76, in walkself.walk(s, new[0], new[1])File "C:\Users\Serena\Desktop\XAproject\Stack.py", line 76, in walkself.walk(s, new[0], new[1])File "C:\Users\Serena\Desktop\XAproject\Stack.py", line 76, in walkself.walk(s, new[0], new[1])File "C:\Users\Serena\Desktop\XAproject\Stack.py", line 76, in walkself.walk(s, new[0], new[1])File "C:\Users\Serena\Desktop\XAproject\Stack.py", line 48, in walkwhile self.maze[new[0]][new[1]].getVisit():
IndexError: list index out of range

I'm new to programming, any help would be appreciate. Thank you~

After fix the previous one, I got an error for the line

    if len(neighboor) != 0: new = choice(neighboor.remove(new))

The error message is

Traceback (most recent call last):File "<pyshell#11>", line 1, in <module>a.search()File "C:\Users\Serena\Desktop\XAproject\Stack.py", line 88, in searchself.walk(s, startX, startY)File "C:\Users\Serena\Desktop\XAproject\Stack.py", line 73, in walkself.walk(s, new[0], new[1])File "C:\Users\Serena\Desktop\XAproject\Stack.py", line 52, in walkif len(neighboor) != 0: new = choice(neighboor.remove(new))File "C:\Python34\lib\random.py", line 253, in choicei = self._randbelow(len(seq))
TypeError: object of type 'NoneType' has no len()

But I define 'neighboor' as a list, it should has a len()

Thank you so much for help! plzplz~

Answer

The problem is that you're not ensuring that the steps in your walk are valid.

The way you have it currently, walk could pick a neighboor that is out of the bounds of the maze. For example, if a maze is 5x5, attempting to access maze[5][?] or maze[?][5] will result in an IndexError like you get.

To fix this, you could define an is_valid method for your maze class, e.g.:

def is_valid(self, x, y):return (0 <= x < self.size) and (0 <= y < self.size)

Then, you when you pick a neighboor, you ensure it's valid:

#...
else:new = choice(neighboor)while self.is_valid(new[0], new[1]) == False:new = choice(neighboor)while self.maze[new[0]][new[1]].getVisit():
#...

This snippet picks a neighboor, then, if it's not valid, regenerates new until it finds a valid one.

But this loop would be better written as:

#...
else:while True:new = choice(neighboor)if self.is_valid(new[0], new[1]): breakwhile self.maze[new[0]][new[1]].getVisit():
#...

There are more problems with your code, however, as you'll eventually see, but this will get you past this specific one.

https://en.xdnf.cn/q/120634.html

Related Q&A

How do I select random text on screen [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…

Conditional return with no else [duplicate]

This question already has answers here:Python Ternary Operator Without else(9 answers)Closed 10 months ago.Im try to do something like that in Python:return None if a is NoneInstead of having:if a is N…

Finding the position of an item in another list from a number in a different list

Developing on my previous question Im wondering whether there is a way to have an element in a list and find that position in another list and return what is in the position as a variable. for example:…

Why am I getting array instead of vector size? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 3 years ago.Improve…

Sending Email attachment (.txt file) using Python 2.7 (smtplib) [duplicate]

This question already has answers here:How to send email attachments?(21 answers)Closed 9 years ago.So Im trying to send a .txt file as an attachment and I cant find the right code to work. Here is my…

Python selenium drop down menu click

i want to select option from a drop down menu, for this i use that :br.find_element_by_xpath("//*[@id=adyen-encrypted-form]/fieldset/div[3]/div[2]/div/div/div/div/div[2]/div/ul/li[5]/span").c…

TypeError: Argument must be rect style object - Pygame (Python [duplicate]

This question already has answers here:Closed 11 years ago.Possible Duplicate:Pygame (Python) - TypeError: Argument must be rect style object I am trying to make a brick breaker game in Pygame (with P…

How to compare dates in python and find the greater one

I want to compare 2 date and predict a label true if date 1 greater than date 2 and predict false date 1 less than date 2. I have trained the model but model is predicting wrong for near by dates that …

Python: is isalnum() the same as isalpha with isdigit?

Is there a way to concretely verify this? I tried to solve a coding question but it seems one of the test cases (not revealed to me) takes this as wrong. In what kinds of cases does this fail to be tr…

Python code works fine first time, but fails second time

The first time I run this block of code from Notebook it works fine:#Which letters and how many letters = ["a","b","c"] noOfLetters = len(letters)#Looking for all permutat…