【问题标题】:Creating a Maze创建迷宫
【发布时间】:2013-02-28 04:54:58
【问题描述】:

我正在尝试使用 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 一起制成表格,以便它们可以随机添加到网格中(或)有没有其他方法可以做到这一点??

【问题讨论】:

  • 用可见和不可见的正方形制表矩阵,我不知道我该怎么做

标签: smalltalk pharo morphic


【解决方案1】:

生成随机数不就是这样吗?

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 Algorithmsthis page 是基于它的,其中包含各种语言的代码示例。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-19
    • 2012-04-25
    • 1970-01-01
    • 2012-08-02
    • 2011-01-26
    • 1970-01-01
    • 1970-01-01
    • 2012-03-08
    相关资源
    最近更新 更多