Section 5

Here's a new unit test for GridTestCase to exercise our undo mechanism.

testUndoStackAfterPush
     | grid |
     grid := self generateDemoGrid.
     self should: [grid movesStack isEmpty].
     grid pushCellEastFromLocation: 1@2.
     self should: [grid movesStack size = 1].
     self should: [grid undo].
     self shouldnt: [grid undo].

Now we work on the GUI. Our first step is to create the Undo button. Add the or modify the following instance methods in the LaserGame class.

makeUndoButton
     ^self makeButton: 'Undo' action: #undo state: nil

addButtonsToPanel: panel
     | layout |
     layout := self buttonLayoutFrameForRow: 1 column: 1.
     panel addMorph: self makeQuitGameButton fullFrame: layout.

     layout := self buttonLayoutFrameForRow: 1 column: 2.
     panel addMorph: self makeFireLaserButton fullFrame: layout.

     layout := self buttonLayoutFrameForRow: 2 column: 1.
     panel addMorph: self makeNewGameButton fullFrame: layout.

     layout := self buttonLayoutFrameForRow: 2 column: 2.
     panel addMorph: self makeUndoButton fullFrame: layout.

     ^panel

undo
     | completed |
     completed := self grid undo.
     completed ifTrue: [
         self incrementMoves.
         self updateGameBoardAndControls]

Index Page Next Page

Copyright © 2007, 2008, 2009, 2010 Stephan B Wessels    stevewessels@me.com