2

我正在尝试使用 PBE-Lighoutgame 创建一个没有鼠标点击事件的迷宫作为我的参考我有 2 个类

这两个类都是 RectangleMorph 的子类

 VisibleSquare>>initialize
  "Visible borders with Some Color"

其他类

  InvisbleSquare>>initialize
   "Everything is transparent Including Borders"

实现迷宫类,它是 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 将矩阵制成表格,以便它们可以随机添加到网格中(或)有没有其他方法可以做到这一点?

4

1 回答 1

2

生成随机数不会像这样吗?

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 似乎配备的函数式编程特性来实现。考虑查看Maze Generation Algorithms 的 Wikipedia 页面该页面基于该页面,其中包含各种语言的代码示例。

于 2013-02-28T06:49:13.657 回答