Section 3

Push A Cell

Our game should provide a way for the user to move a cell orthogonally. The only cells that can be moved are mirror cells. The target cell may not be pushed. Only individual mirror cells may be pushed. For example, if there are mirror cells adjacent, the user cannot move them together.

Moving an individual mirror cell with a push is legal.

Since neighboring cells impact what we do when a push operation is performed, we obviously will be working at the Grid level. Also, the mirror cells are not really pushed. They merely appear to be pushed. In reality we will be swapping their location with the adjacent blank cell.

There is the key detail for our push logic. If the adjacent cell where we want to push is a blank cell, we trade places. Otherwise nothing happens.

We can now write the four push instance methods on the Grid class. They don't have to do anything yet. We just want a method we will call from the unit test we intend to write. Here's the first push-north method.

The other 3 directions are easy to write.

pushCellEastFromLocation: aPoint


pushCellSouthFromLocation: aPoint


pushCellWestFromLocation: aPoint

Let's go write that unit test.

Index Page Next Page

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