Hardware Reference
In-Depth Information
10. Look at this step very carefully: you now have another for loop. This is called a
nested loop , because one loop is nested inside another. Your program will have
to reset the x coordinate at the start of every new row of the maze, so this has to
be done outside of the for loop that draws a whole row:
x = ORIGIN_X
for cell in data:
11. Each number read in from the row is actually read in as a piece of text (a string),
so for this program to work, you will have to put "" quotes around the number.
his if / else statement chooses whether to build a gap or a wall, based on the
number just read back from the CSV file. A 0 builds a gap, and anything else
builds a wall. Make sure your indentation is correct here: the if statement is
indented twice because it is part of the for cell in data loop, which is part
of the for line in f.readlines() loop. The b variable here is useful as it
makes the program a bit smaller and simpler. (Try rewriting this part without
using the b variable and you will see what I mean!)
if cell == "0":
b = GAP
else:
b = WALL
mc.setBlock(x, ORIGIN_Y, z, b)
mc.setBlock(x, ORIGIN_Y+1, z, b)
mc.setBlock(x, ORIGIN_Y-1, z, FLOOR)
12. Update the x coordinate at the end of the for cell loop. This must be indented
so that it lines up with the previous mc.setBlock() as it is still part of the
body of the for cell in data loop.
x = x + 1
13. Update the z coordinate at the end of the loop that processes each row of data
(this must be indented one level only, as it is part of the for line in f.
readlines() loop).
z = z + 1
Save your program, and double check that all the indentation is correct. If the indentation
is wrong, the program will not work correctly and you will get some very strange-shaped
mazes!
Run your program and a fantastic (and quite hard to solve) maze will be built in front
of you. Walk around the maze and see if you can solve it without breaking down any
walls or flying. FigureĀ 6-2 shows what the maze looks like from ground level.
 
Search WWH ::




Custom Search