The Depth First Search Algorithm is basically the simplest maze generation algorithm I figure it be a perfect place for me to start. You begin with a grid like this.
1) Start at a random cell in the grid.
2) Look for a random neighbor cell you haven't been to yet.
3) If you find one, move there, knocking down the wall between the cells. If you don't find one, back up to the previous cell.
4) Repeat steps 2 and 3 until you've been to every cell in the grid.
Simple yes?
The code for this looks like:
create a CellStack (LIFO) to hold a list of cell locations
set TotalCells = number of cells in grid
choose a cell at random and call it CurrentCell
set VisitedCells = 1
while VisitedCells < TotalCells
find all neighbors of CurrentCell with all walls intact
if one or more found
choose one at random
knock down the wall between it and CurrentCell
push CurrentCell location on the CellStack
make the new cell CurrentCell
add 1 to VisitedCells
else
pop the most recent cell entry off the CellStack
make it CurrentCell
endIf
endWhile
This might make no sense to you, but to me it looks quite familiar. So, thats my adventure for this week. See you all next week. The semester is ending soon so study hard!
No comments:
Post a Comment