2013-02-28 106 views
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: ([email protected] 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使得它們能夠在網格中隨機添加(或)是否有任何其他方式做到這一點?

+0

製表矩陣與可見和不可見的廣場,我不知道我該怎麼做 – Irfan 2013-02-28 05:07:23

回答

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] 

也許這是你想知道的。然而,由於這是爲了生成迷宮佈局,所以你可能應該想出一些比隨機放置牆更復雜的東西。可能有一些算法很有趣,可以實現小功能似乎配備的功能編程功能。請考慮查看this page所基於的Wikipedia page on Maze Generation Algorithms,其中包含各種語言的代碼示例。