1.1 --- a/Source/CheckersGame.m Thu May 29 15:04:06 2008 -0700
1.2 +++ b/Source/CheckersGame.m Thu Jul 03 17:44:30 2008 -0700
1.3 @@ -30,6 +30,26 @@
1.4 @implementation CheckersGame
1.5
1.6
1.7 +- (id) init
1.8 +{
1.9 + self = [super init];
1.10 + if (self != nil) {
1.11 + _cells = [[NSMutableArray alloc] init];
1.12 + [self setNumberOfPlayers: 2];
1.13 +
1.14 + PreloadSound(@"Tink");
1.15 + PreloadSound(@"Funk");
1.16 + PreloadSound(@"Blow");
1.17 + PreloadSound(@"Pop");
1.18 + }
1.19 + return self;
1.20 +}
1.21 +
1.22 +- (CGImageRef) iconForPlayer: (int)playerNum
1.23 +{
1.24 + return GetCGImageNamed( playerNum==0 ?@"Green.png" :@"Red.png" );
1.25 +}
1.26 +
1.27 - (Piece*) pieceForPlayer: (int)playerNum
1.28 {
1.29 Piece *p = [[Piece alloc] initWithImageNamed: (playerNum==0 ?@"Green.png" :@"Red.png")
1.30 @@ -39,10 +59,18 @@
1.31 return [p autorelease];
1.32 }
1.33
1.34 -- (Grid*) x_makeGrid
1.35 +- (void) makeKing: (Piece*)piece
1.36 +{
1.37 + piece.scale = 1.4;
1.38 + [piece setValue: @"King" forKey: @"King"];
1.39 + piece.name = piece.owner.index ?@"4" :@"3";
1.40 +}
1.41 +
1.42 +- (void) setUpBoard
1.43 {
1.44 RectGrid *grid = [[[RectGrid alloc] initWithRows: 8 columns: 8 frame: _board.bounds] autorelease];
1.45 _grid = grid;
1.46 + [_board addSublayer: _grid];
1.47 CGPoint pos = _grid.position;
1.48 pos.x = floor((_board.bounds.size.width-grid.frame.size.width)/2);
1.49 grid.position = pos;
1.50 @@ -53,31 +81,11 @@
1.51 grid.lineColor = nil;
1.52
1.53 [grid addAllCells];
1.54 + [_cells removeAllObjects];
1.55 for( int i=0; i<32; i++ ) {
1.56 int row = i/4;
1.57 [_cells addObject: [_grid cellAtRow: row column: 2*(i%4) + (row&1)]];
1.58 }
1.59 - self.stateString = @"111111111111--------222222222222";
1.60 - return grid;
1.61 -}
1.62 -
1.63 -
1.64 -- (id) initWithBoard: (GGBLayer*)board
1.65 -{
1.66 - self = [super initWithBoard: board];
1.67 - if (self != nil) {
1.68 - [self setNumberOfPlayers: 2];
1.69 - _cells = [[NSMutableArray alloc] init];
1.70 - [self x_makeGrid];
1.71 - [board addSublayer: _grid];
1.72 - [self nextPlayer];
1.73 -
1.74 - PreloadSound(@"Tink");
1.75 - PreloadSound(@"Funk");
1.76 - PreloadSound(@"Blow");
1.77 - PreloadSound(@"Pop");
1.78 - }
1.79 - return self;
1.80 }
1.81
1.82 - (void) dealloc
1.83 @@ -88,6 +96,11 @@
1.84 }
1.85
1.86
1.87 +- (NSString*) initialStateString
1.88 +{
1.89 + return @"111111111111--------222222222222";
1.90 +}
1.91 +
1.92 - (NSString*) stateString
1.93 {
1.94 unichar state[_cells.count];
1.95 @@ -108,11 +121,15 @@
1.96 int i = 0;
1.97 for( GridCell *cell in _cells ) {
1.98 Piece *piece;
1.99 - switch( [state characterAtIndex: i++] ) {
1.100 - case '1': piece = [self pieceForPlayer: 0]; _numPieces[0]++; break;
1.101 - case '2': piece = [self pieceForPlayer: 1]; _numPieces[1]++; break;
1.102 - default: piece = nil; break;
1.103 - }
1.104 + int which = [state characterAtIndex: i++] - '1';
1.105 + if( which >=0 && which < 4 ) {
1.106 + int player = (which & 1);
1.107 + piece = [self pieceForPlayer: player];
1.108 + _numPieces[player]++;
1.109 + if( which & 2 )
1.110 + [self makeKing: piece];
1.111 + } else
1.112 + piece = nil;
1.113 cell.bit = piece;
1.114 }
1.115 }
1.116 @@ -134,9 +151,11 @@
1.117 Square *src=(Square*)srcHolder, *dst=(Square*)dstHolder;
1.118 int playerIndex = self.currentPlayer.index;
1.119
1.120 - if( self.currentMove.length==0 )
1.121 - [self.currentMove appendString: src.name];
1.122 - [self.currentMove appendString: dst.name];
1.123 + Turn *turn = self.currentTurn;
1.124 + if( turn.move.length==0 )
1.125 + [turn addToMove: src.name];
1.126 + [turn addToMove: @"-"];
1.127 + [turn addToMove: dst.name];
1.128
1.129 BOOL isKing = ([bit valueForKey: @"King"] != nil);
1.130 PlaySound(isKing ?@"Funk" :@"Tink");
1.131 @@ -145,8 +164,8 @@
1.132 if( dst.row == (playerIndex ?0 :7) )
1.133 if( ! isKing ) {
1.134 PlaySound(@"Blow");
1.135 - bit.scale = 1.4;
1.136 - [bit setValue: @"King" forKey: @"King"];
1.137 + [self makeKing: (Piece*)bit];
1.138 + [turn addToMove: @"*"];
1.139 // don't set isKing flag - piece can't jump again after being kinged.
1.140 }
1.141
1.142 @@ -163,9 +182,9 @@
1.143
1.144 if( capture ) {
1.145 PlaySound(@"Pop");
1.146 - Bit *bit = capture.bit;
1.147 - _numPieces[bit.owner.index]--;
1.148 - [bit destroy];
1.149 + _numPieces[capture.bit.owner.index]--;
1.150 + [capture destroyBit];
1.151 + [turn addToMove: @"!"];
1.152
1.153 // Now check if another capture is possible. If so, don't end the turn:
1.154 if( (dst.fl.bit.unfriendly && dst.fl.fl.empty) || (dst.fr.bit.unfriendly && dst.fr.fr.empty) )
1.155 @@ -192,16 +211,15 @@
1.156
1.157 - (BOOL) applyMoveString: (NSString*)move
1.158 {
1.159 - int length = move.length;
1.160 - if( length<4 || (length&1) )
1.161 - return NO;
1.162 GridCell *src = nil;
1.163 - for( int i=0; i<length; i+=2 ) {
1.164 - NSString *ident = [move substringWithRange: NSMakeRange(i,2)];
1.165 + for( NSString *ident in [move componentsSeparatedByString: @"-"] ) {
1.166 + while( [ident hasSuffix: @"!"] || [ident hasSuffix: @"*"] )
1.167 + ident = [ident substringToIndex: ident.length-1];
1.168 GridCell *dst = [_grid cellWithName: ident];
1.169 - if( i > 0 )
1.170 - if( ! [self animateMoveFrom: src to: dst] )
1.171 - return NO;
1.172 + if( dst == nil )
1.173 + return NO;
1.174 + if( src && ! [self animateMoveFrom: src to: dst] )
1.175 + return NO;
1.176 src = dst;
1.177 }
1.178 return YES;