문제

I am trying to Create a Maze with PBE-Lighoutgame as my ref without Mouse Click events I have a 2 classes

Both these classes are subclass of RectangleMorph

 VisibleSquare>>initialize
  "Visible borders with Some Color"

other Class

  InvisbleSquare>>initialize
   "Everything is transparent Including Borders"

Implementing Maze Class which is subclass of 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].

other Method

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

How can i tabulate Matrix with both VisibleSquare & InvisibleSquare such that they can be randomly added in grid (or) Is there any other way to do this??

도움이 되었습니까?

해결책

Wouldn't generating a random number be simply something like this?

rnd := (1 to: 100) atRandom.

Once you've got it, you can assign alternative messages to the receiver c:

(rnd > 50) ifTrue:[c := VisibleSquare new]
(rnd < 51) ifTrue:[c := InvisibleSquare new]

...which I think can as well be expressed as

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

Maybe this is what you wanted to know. However, since this is for the generation of a maze layout, you maybe should come up with something more sophisticated than just randomly putting walls. There are probably some algorithms which are fun to implement with the functional programming features that smalltalk seems to be equipped with. Think about having a look at the Wikipedia page on Maze Generation Algorithms, which this page is based on, featuring code samples in various languages.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top