Game Development Reference
In-Depth Information
@property (nonatomic, retain) NSMutableArray* coins;
@property (nonatomic) int remaingTurns;
@property (nonatomic) int score;
@property (nonatomic) int colCount;
@property (nonatomic) int rowCount;
-(id)initRandomWithRows:(int)rows Cols:(int)cols;
-(NSNumber*)coinForCoord:(Coord)coord;
-(int)indexForCoord:(Coord)coord;
-(void)swap:(Coord)coordA With:(Coord)coordB;
-(NSMutableArray*)findMatchingRows;
CoinsGame
. At the very top of the class, we define
COIN_TRIANGLE
,
COIN_SQUARE
, and
COIN_CIRCLE
. These values represent the three
Coord
that is used to store a
Coord
was used earlier in Listing 4-12 to identify a specific coin.
Coord
. The first, called
CoordMake
, is used to create
Coord
with the corresponding row and column values. The second, called
CoordEqual
, is used to
Coords
are referring to the same coin.
The interface declaration for the class
CoinsGame
, shown in Listing 4-13, conforms to the protocol
NSCoding
, so we know we can archive and unarchive instances of this class. We also see that an
NSMutableArray
called
coins
is defined. The
coins
object is used to store the
NSNumber
values that
represent the coins at each coordinate. In addition,
ints
are used to keep track of the number of
remaining turns, the score, and the number of rows and columns used in this game.
There are various tasks defined for the class
CoinsGame
, and hopefully some of these make sense
already. We know we will need a way to swap two coins, so the use of the
swap:With:
task should
make sense. There are also two tasks,
findMatchingRows
and
findMatchingCols
, which are used
to determine whether any matches exist. The tasks
randomizeRows
and
randomizeCols
are used
to set new coin values after a match has been found. Notice that
findMatchingRows
returns an
NSMutableArray
and that
randomizeRows
takes an
NSMutableArray
. The idea here is that after a match
is found and the animations are all done, we can use the same
NSMutableArray
that represented the
match to indicate which coins should be randomized.
Listing 4-13 also has two tasks that take a
Coord
as an argument. The first one,
coinForCoord:
,
takes a
Coord
and returns
NSNumber
, indicating the coin type for that coordinate. The second task,
indexForCoord
, is used to translate a
Coord
into
int
suitable as an index into an array. This task is
used to find the coin
NSNumber
in
coinForCoord
, and it is also used by
CoinsController
to find the
correct
UIImageView
for a coin.
Let's take a look at the implementation of some of the tasks, because they will be called by
CoinsController
at different points. We'll start with
initRandomWithRow:Col:
, shown in Listing 4-14.