A Bigger Game Board
Our next enhancement will deal with making the game board larger. We're going to be enhancing the GridFactory class mostly. Begin with this new class method on the GridFactory class.
randomizedGridOfExtent: ext
| grid |
grid := Grid newOfSize: ext.
self randomizeGrid: grid targetAt: ((ext x)@1).
^grid
Now we should refactor the #initialize instance method on our LaserGame class. We will begin by adding the following method to LaserGame.
initializeForGrid: aGrid
super initialize.
self moves: 0.
self grid: aGrid.
self boardForm: (Form extent: (self class boardExtentFor: self grid) depth: Display depth).
self boardForm fillColor: LaserGameColors gameBoardBackgroundColor.
self setExtent.
self setupMorphs.
self drawGameBoard.
The #initializeForGrid: method is a lot like the old #initialize method except it assumes it's being handed a Grid. Here's the change to our #initialize method to use this new worker method.
initialize
self initializeForGrid: GridFactory demoGrid
Add the following new class method to LaserGame.
randomizedGridOfExtent: aPoint
| model grid |
model := self basicNew.
grid := GridFactory randomizedGridOfExtent: aPoint.
model initializeForGrid: grid.
^model
We need to modify the #emptyRandomLocationsFor: class method on GridFactory to ensure that the target cell is now always located in the top-most row and corner of any sized game board.
emptyRandomLocationsFor: aGrid
| dict |
dict := Dictionary new.
1 to: aGrid numberOfColumns do: [:x |
1 to: aGrid numberOfRows do: [:y |
| pt |
pt := x@y.
dict at: pt put: false]].
dict at: (aGrid numberOfColumns)@1 put: true. "Target Cell"
^dict
Create a new class method in GridFactory.
randomizeGrid: aGrid targetAt: pt
| emptyList loc howMany |
emptyList := self emptyRandomLocationsFor: aGrid.
aGrid at: pt put: TargetCell new.
howMany := ((aGrid numberOfColumns * aGrid numberOfRows) / 2.5) rounded.
howMany timesRepeat: [
loc := self unusedRandomLocationIn: emptyList forGrid: aGrid.
aGrid at: loc put: self randomizedMirrorCell]
Note how we attempt to calculate a reasonable number of mirror cells based uponb the size of the grid. Now we modify the #randomizeGrid: class method on GridFactory to use our new method.
randomizeGrid: aGrid
self randomizeGrid: aGrid targetAt: (aGrid numberOfColumns@1)