smalltalk - 创建迷宫

标签 smalltalk pharo morphic

我正在尝试使用 PBE-Lighoutgame 作为我的引用创建一个迷宫,而不需要鼠标单击事件 我有 2 节课

这两个类都是 RectangleMorph 的子类

 VisibleSquare>>initialize
  "Visible borders with Some Color"

其他类

  InvisbleSquare>>initialize
   "Everything is transparent Including Borders"

实现 Maze 类,它是 BorderedMorph 的子类

 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]

...我认为也可以表示为

c := rnd > 50 ifTrue[VisibleSquare new] ifFalse:[InVisibleSquare new]

也许这就是您想知道的。然而,由于这是为了生成迷宫布局,因此您可能应该想出一些比随机放置墙壁更复杂的东西。使用 Smalltalk 似乎配备的函数式编程功能来实现可能很有趣的一些算法。考虑看看 Wikipedia page on Maze Generation Algorithms ,其中 this page基于各种语言的代码示例。

关于smalltalk - 创建迷宫,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15127807/

相关文章:

ruby - 是否存在适用于 Ruby 的(实验性)类浏览器?

mongodb - 如何在 Voyage for Pharo 中禁用缓存?

smalltalk - 如何配置 Seaside 3.2 session 超时(年龄)

smalltalk - 容器时如何重新缩放 subMorph?

smalltalk - 如何从有序集合中的文本中获取所有项目

regex - 在正则表达式替换中捕获字符串

smalltalk - SmalltalkHub与SqueakSource3

observer-pattern - 公告与经典 Smalltalk-80 依赖/更改/更新机制有何关系?

pharo - 形态打破边界

smalltalk - 吱吱小声中的动画