Smalltalk 创造迷宫
我正在尝试创建一个以PBE Lighoutgame作为参考的迷宫,而不使用鼠标点击事件 我有两门课 这两个类都是矩形变形的子类Smalltalk 创造迷宫,smalltalk,pharo,morphic,Smalltalk,Pharo,Morphic,我正在尝试创建一个以PBE Lighoutgame作为参考的迷宫,而不使用鼠标点击事件 我有两门课 这两个类都是矩形变形的子类 VisibleSquare>>initialize "Visible borders with Some Color" 其他类 InvisbleSquare>>initialize "Everything is transparent Including Borders" 实现BordedMorph的子类Maze类 Maze
VisibleSquare>>initialize
"Visible borders with Some Color"
其他类
InvisbleSquare>>initialize
"Everything is transparent Including Borders"
实现BordedMorph的子类Maze类
Maze>>initialize
initialize
|sampleCell width height n sample|
super initialize.
self borderWidth: 0.
n := self cellsPerSide.
sampleCell := VisibleSquare new.
sample:= InvisibleSquare new.
width := sampleCell width.
height := sample height.
self bounds: (5@5 extent: ((width + n) @ (height + n)) + (2 * self borderWidth)).
cells := Matrix new: n tabulate: [:i :j | self newCellAt: i at: j].
其他方法
Maze>> newCellAt: i at: j
"Create a cell for position (i,j) and add it to my on-screen
representation at the appropriate screen position. Answer the new cell"
|c origin b |
c := VisibleSquare new.
origin := self innerBounds origin.
self addMorph: c.
c position: ((i - 1) * c width) @ ((j - 1) * c height) + origin.
^ c
我如何用VisibleSquare和InvisibleSquare将矩阵制成表格,以便它们可以随机添加到网格中(或者)是否有其他方法可以做到这一点???生成随机数不就是这样吗
rnd := (1 to: 100) atRandom.
一旦你得到了它,你就可以给接收者分配替代信息了c
:
(rnd > 50) ifTrue:[c := VisibleSquare new]
(rnd < 51) ifTrue:[c := InvisibleSquare new]
也许这就是你想知道的。然而,由于这是为了生成迷宫布局,您可能应该想出比随机放置墙更复杂的方法。可能有一些算法很有趣,可以通过smalltalk似乎配备的函数式编程功能来实现。考虑一下看看基于的,以各种语言的代码示例为特色的 用可见和不可见的正方形来制表矩阵,我不知道该怎么做
c := rnd > 50 ifTrue[VisibleSquare new] ifFalse:[InVisibleSquare new]