Hardware Reference
In-Depth Information
Step 4 in the algorithm is to pick a random direction if you found any unvisited neighbours,
knock down the wall in that direction and move into that cell. To decide whether you found
any possible directions, you check the length of the possibleDirections list and act if it
is not equal to 0 (!=0) . All of this should be indented under the while loop. If you get lost
in the indenting, consult the full code in Listing 7-1 near the end of this chapter.
Before you start moving your position, you hide the gold brick that shows where you are in
the maze:
hideMaker(xposition, zposition)
if len(possibleDirections)!=0:
directionChosen=random.choice(possibleDirections)
if directionChosen==”left”:
demolish(realx(xposition)-1, realz(zposition))
xposition -= 1
if directionChosen==”right”:
demolish(realx(xposition)+1, realz(zposition))
xposition += 1
if directionChosen==”up”:
demolish(realx(xposition), realz(zposition)-1)
zposition -= 1
if directionChosen==”down”:
demolish(realx(xposition), realz(zposition)+1)
zposition += 1
After you've moved into a new cell, you need to increase your tally of cells visited by one, and
add the new cell to the list that stores the path taken. his is also a good time to show the
gold block in the cell to highlight how the maze is being built:
numberOfVisitedCells += 1
cellsVisitedList.append((xposition, zposition))
showMaker(xposition, zposition)
he way you've stored the list of cells visited deserves some explanation. You've put the
xposition and zposition in parentheses, which are used to indicate a tuple. A tuple is a
data sequence, a bit like a list, with a key diference being that you can't change its values.
Search WWH ::




Custom Search