我已经在网站上阅读了您之前关于此错误的答案,但不确定如何更改我的代码以修复它。这是一个很长的代码,很抱歉:$
import random
#QUESTION 1a:
#CONSTRUCTOR FUNCTION:
def makestk(): #This function returns a new stack in the form of a tagged tuple.
stk=('stack',[]) #Inserts the tagged tuple into a variable.
return stk #Returns variable holding tagged tuple(empty stack).
#PREDICATE FUNCTION:
def emptystk(stk): #This function returns True if the stack is empty or false if the stack is not empty.
if stk[1] == []: #Checks if the second variable of the tagged tuple is an empty list or not.
return True #Returns True if the second variable of the tagged tuple is indeed an empty list.
else:
return False #Returns False if the second variable of the tagged tuple is a list containing element(s).
#MUTATOR FUNCTIONS:
def contents(s): #This function is created to aid the mutator functions.
return s[1] #Returns the second variable of the tagged tuple.
def pushstk(stk,ele): #This function accepts a stack and an element and adds the element to the stack at position0.
contents(stk).insert(0,ele) #Uses the helper function to get the second variable of the tagged tuple and inserts the element to the stack at position0.
#return stk #Returns the updated version of the tagged tuple entered.
def popstk(stk): #This function accepts a stack and an element; and adds the element to the stack at position0.
contents(stk).pop(0) #Uses the helper function to get the second variable of the tagged tuple and removes the element in position0 of the stack.
#return stk #Returns the updated version of the tagged tuple entered.
#QUESTION 1b:
#CONSTRUCTOR FUNCTION:
def makecell((x,y)): #This function accepts a tuple which consists of the x and y co-ordinate of the cell being created.
cell=('cell',(x,y),['t','b','r','l']) #Inserts the tagged tuple into a variable.(The list represents the four walls of the cell: t-top,b-bottom,r-right,l-left)
return cell #Returns a cell in the form of a tagged tuple.
#ACCESSOR FUNCTIONS:
def getx(cell): #This function returns the x co-ordinate of the cell.
return cell[1][0] #Returns the first value of the second variable in the tagged tuple.
def gety(cell): #This function returns the y co-ordinate of the cell.
return cell[1][1] #Returns the second value of the second variable in the tagged tuple.
def getwalls(cell): #This function returns the lsit of walls that are intact for a given cell.
return cell[2] #Returns the third variable of the tagged tuple.
#MUTATOR FUNCTIONS:
def removetw(cell): #This function removes the top wall of the cell.
getwalls(cell).remove('t') #Uses the 'getwalls' function and removes the first value of list.
return cell #Returns the updated version of the list.
def removebw(cell): #This function removes the bottom wall of the cell.
getwalls(cell).remove('b') #Uses the 'getwalls' function and removes the second value of the list.
return cell #Returns the updated version of the list.
def removerw(cell): #This function removes the right wall of the cell.
getwalls(cell).remove('r') #Uses the 'getwalls' function and removes the third values of the list.
return cell #Returns the updated version of the list.
def removelw(cell): #This function removes the left wall of the cell.
getwalls(cell).remove('l') #Uses the 'getwalls' function and removes the fourth values of the list.
return cell #Returns the updated version of the list.
#QUESTION 2:
def makegrid(w,h): #This function accepts the width and height of the desired grid/matrix of cells and creates it.
grid=[] #Inserts an empty list into a variable.
for x in range (0,(w)): #Allows x to traverse through the range of 0 to the width value.
for y in range (0,(h)): #Allows y to traverse through the range of 0 to the height value.
tup=(x,y) #Inserts the values x and y into a variable.
grid.append(makecell(tup)) #Inserts/updates the variable 'tup' into the list 'grid' and runs function until x and y have completely traversed through the ranges.
return grid #Returns the updated version of grid.
#QUESTION 3:
def getneighbours (cell,w,h): #This function accepts a cell and the width and height of the grid.
lst = []
x = getx(cell)
y = gety(cell)
if y + 1 <= h:
lst = lst + [makecell((x,y + 1))]
if y - 1 >= 0:
lst = lst + [makecell((x,y - 1))]
if x + 1 <= w:
lst = lst + [makecell((x + 1,y))]
if x - 1 >= 0:
lst = lst + [makecell((x - 1,y))]
return lst #Returns the neighbouring cell walls.
#QUESTION 4:
def removewalls(c1,c2): #This function accepts two cells and removes the walls common between the two.accepts two cells
if gety(c1)==gety(c2)-1: #Checks if c1 is above c2.
removetw(c2) and removebw(c1) #Removes the corresponding common wall shared between the cells.
elif gety(c1)==gety(c2)+1: #Checks if c1 is below c2.
removetw(c1) and removebw(c2) #Removes the corresponding common wall shared between the cells.
elif getx(c1)==getx(c2)-1: #Checks if c1 is to the left of c2.
removerw(c1) and removelw(c2) #Removes the corresponding common wall shared between the cells.
elif getx(c1)==getx(c2)+1: #Checks if c1 is to the right of c2.
removerw(c2) and removelw(c1) #Removes the corresponding common wall shared between the cells.
cell0=makecell((0,0))
cell1=makecell((0,1))
#QUESTION 5:
def wallsintact(grid,neighbours):
return [x for x in grid if x in neighbours and getwalls(x)==['t','b','r','l']]
#QUESTION 6:
def createmaze(w,h):
stack=makestk()
totalcells=w*h
grid=makegrid(w,h)
cell=grid[random.randrange(totalcells)]
strt=1
while (strt<totalcells):
neighbours=getneighbours(cell,w,h)
intact=wallsintact(grid,neighbours)
while intact!=[]:
maze=intact[random.randrange(len(intact))]
intact.remove(maze)
removewalls(cell,maze)
pushstk(stack,maze)
strt=strt+1
cell=popstk(stack)
return grid