这里是DFS算法 http://www.mazeworks.com/mazegen/mazetut/index.htm生成迷宫使用DFS算法
创建CellStack(LIFO)伪代码来保存细胞位置的列表
组TotalCells =在网格
细胞的数目选择在小区随机并调用它CurrentCell
集VisitedCells = 1
而VisitedCells < TotalCells 找到CurrentCell的所有邻居有完整
所有的墙如果一个或多个发现 选择一个随机
击倒它,CurrentCell之间的墙壁上CellStack
使新细胞CurrentCell
推CurrentCell位置 加1 VisitedCells
其他
从CellStack弹出最近的单元格输入
使其成为CurrentCell
endIf ENDWHILE
我的Smalltalk代码
Maze>>initialize
|sampleCell width height n sample |
super initialize.
self borderWidth: 0.
sampleCell := VisibleSquare new.
width := sampleCell width.
height := sampleCell height.
self bounds: ([email protected] extent: ((width + n) @ (height + n)) + (2 * self borderWidth)).
visitedcell :=0.
cells := Matrix rows: 8 columns: 7 tabulate: [:i :j | self newCellAt: i at:j].
这是另一种方法。
Maze>>newCellAt:i at:j
|c|
celltotal:= 8*7.
[(visitedcell< celltotal)] whileTrue:
["Im stuck with selecting cells next to current cell to make it as
Invisible square"
"else do this"
c := VisibleSquare new.
origin := self innerBounds origin.
self addMorphBack: c.
c position: ((i - 1) * c width) @ ((j - 1) * c height) + origin.
^ c
我有2班一个为Visiblesquare无非是红色广场等为Invisiblesquare这是空方
那么,是什么问题? – 2013-03-05 06:52:58