Creating A Random Maze - Step 3◀ Creating A Random Maze - Step 2▶ Creating A Random Maze - Step 4 Amazon
et’s apply the 3rd step of the Four-Step Programming Model to generate a random maze!
Four Step Programming Model: Step 3
: Elements of lottery and sol can be accessed via , so make sure their indices do not go out of bounds. Pay attention to conditions of any flow-control statements (if, while…).
: xOffset and yOffset should not be changed once their values are assigned. width, height, and numOfCells are the fundamental properties of the maze and their values are not altered once assigned. lottery initially contains all cells’ ids and decreases in size as walls are knocked down and sol contains the solution composed of cells’ ids and increases in size as more cells on the solution path are added to it.
: xOffset and yOffset are needed by displaySol(), so I can give them global scopes. width, height, and numOfCells are also needed by nonmember functions, especially findSol(). Therefore, I give them global scopes as well.
i is just an index, so it stays local. file, file2, and fout can stay local, but fout2 is needed by displaySol(). You can either make fout2 local or global, but if you make it local, you will need to pass it to displaySol(). lottery and sol are both needed by several nonmember functions, so I give them global scope.
Keep in mind you should avoid using global variables as much as possible.
Let's look at the next step!◀ Creating A Random Maze - Step 2▶ Creating A Random Maze - Step 4