Fixed some problems reported by the CLANG static analyzer.
1 /* This code is based on Apple's "GeekGameBoard" sample code, version 1.0.
2 http://developer.apple.com/samplecode/GeekGameBoard/
3 Copyright © 2007 Apple Inc. Copyright © 2008 Jens Alfke. All Rights Reserved.
5 Redistribution and use in source and binary forms, with or without modification, are permitted
6 provided that the following conditions are met:
8 * Redistributions of source code must retain the above copyright notice, this list of conditions
9 and the following disclaimer.
10 * Redistributions in binary form must reproduce the above copyright notice, this list of
11 conditions and the following disclaimer in the documentation and/or other materials provided
12 with the distribution.
14 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
15 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRI-
17 BUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
19 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
20 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
21 THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 #import "CheckersGame.h"
26 #import "QuartzUtils.h"
30 #define kKingScale 1.4
33 @implementation CheckersGame
36 static NSMutableDictionary *kPieceStyle1, *kPieceStyle2;
40 if( self == [CheckersGame class] ) {
41 kPieceStyle1 = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
42 (id)GetCGImageNamed(@"Green.png"), @"contents",
43 kCAGravityResizeAspect, @"contentsGravity",
44 kCAFilterLinear, @"minificationFilter",
46 kPieceStyle2 = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
47 (id)GetCGImageNamed(@"Red.png"), @"contents",
48 kCAGravityResizeAspect, @"contentsGravity",
49 kCAFilterLinear, @"minificationFilter",
59 [self setNumberOfPlayers: 2];
64 - (CGImageRef) iconForPlayer: (int)playerNum
66 return GetCGImageNamed( playerNum==0 ?@"Green.png" :@"Red.png" );
69 - (void) _transformPiece: (Piece*)piece
71 CGFloat scale = piece.tag ?kKingScale :1.0;
72 piece.transform = CATransform3DMakeScale(scale, scale/cos(self.tablePerspectiveAngle), scale);
73 piece.anchorPoint = CGPointMake(0.5, 0.5*cos(self.tablePerspectiveAngle));
76 - (Piece*) pieceForPlayer: (int)playerNum
78 Piece *p = [[Piece alloc] init];
79 p.bounds = CGRectMake(0,0,floor(_board.spacing.width),floor(_board.spacing.height));
80 p.style = (playerNum ?kPieceStyle2 :kPieceStyle1);
81 p.owner = [self.players objectAtIndex: playerNum];
82 p.name = playerNum ?@"2" :@"1";
83 [self _transformPiece: p];
84 return [p autorelease];
87 - (void) perspectiveChanged
89 for( GridCell *cell in _board.cells ) {
90 Piece *piece = (Piece*) cell.bit;
92 [self _transformPiece: piece];
96 - (void) makeKing: (Piece*)piece
98 piece.tag = YES; // tag property stores the 'king' flag
99 piece.name = piece.owner.index ?@"4" :@"3";
100 [self _transformPiece: piece];
105 PreloadSound(@"Tink");
106 PreloadSound(@"Funk");
107 PreloadSound(@"Blow");
108 PreloadSound(@"Pop");
110 RectGrid *board = [[RectGrid alloc] initWithRows: 8 columns: 8 frame: _table.bounds];
112 [_table addSublayer: _board];
113 CGPoint pos = _board.position;
114 pos.x = floor((_table.bounds.size.width-board.frame.size.width)/2);
115 board.position = pos;
116 board.allowsMoves = YES;
117 board.allowsCaptures = NO;
118 board.cellColor = CreateGray(0.0, 0.5);
119 board.altCellColor = CreateGray(1.0, 0.25);
120 board.lineColor = nil;
121 board.reversed = ! [[self.players objectAtIndex: 0] isLocal];
123 for( int i=0; i<32; i++ ) {
125 [_board addCellAtRow: row column: 2*(i%4) + (row&1)];
127 [_board release]; // its superlayer still retains it
130 - (NSString*) initialStateString {return @"111111111111--------222222222222";}
131 - (NSString*) stateString {return _board.stateString;}
132 - (void) setStateString: (NSString*)state {_board.stateString = state;}
134 - (Piece*) makePieceNamed: (NSString*)name
136 int which = [name characterAtIndex: 0] - '1';
137 if( which >=0 && which < 4 ) {
138 Piece *piece = [self pieceForPlayer: (which & 1)];
140 [self makeKing: piece];
147 - (BOOL) canBit: (Bit*)bit moveFrom: (id<BitHolder>)srcHolder to: (id<BitHolder>)dstHolder
149 Square *src=(Square*)srcHolder, *dst=(Square*)dstHolder;
151 if( dst==src.bl || dst==src.br || dst==src.l || dst==src.r
152 || (src.bl.bit.unfriendly && dst==src.bl.bl) || (src.br.bit.unfriendly && dst==src.br.br) )
154 return dst==src.fl || dst==src.fr
155 || (src.fl.bit.unfriendly && dst==src.fl.fl) || (src.fr.bit.unfriendly && dst==src.fr.fr);
158 - (void) bit: (Bit*)bit movedFrom: (id<BitHolder>)srcHolder to: (id<BitHolder>)dstHolder
160 Square *src=(Square*)srcHolder, *dst=(Square*)dstHolder;
161 int playerIndex = self.currentPlayer.index;
163 Turn *turn = self.currentTurn;
164 if( turn.move.length==0 )
165 [turn addToMove: src.name];
166 [turn addToMove: @"-"];
167 [turn addToMove: dst.name];
169 BOOL isKing = bit.tag;
170 PlaySound(isKing ?@"Funk" :@"Tink");
172 // "King" a piece that made it to the last row:
173 if( !isKing && (dst.row == (playerIndex ?0 :7)) ) {
175 [self makeKing: (Piece*)bit];
176 [turn addToMove: @"*"];
177 // don't set isKing flag - piece can't jump again after being kinged.
180 // Check for a capture:
181 NSArray *line = [src lineToCell: dst inclusive: NO];
182 if( line.count==1 ) {
183 Square *capture = [line objectAtIndex: 0];
184 [capture destroyBit];
185 [turn addToMove: @"!"];
188 // Now check if another capture is possible. If so, don't end the turn:
189 if( (dst.fl.bit.unfriendly && dst.fl.fl.empty) || (dst.fr.bit.unfriendly && dst.fr.fr.empty) )
192 if( (dst.bl.bit.unfriendly && dst.bl.bl.empty) || (dst.br.bit.unfriendly && dst.br.br.empty) )
200 #pragma mark CHECK FOR WIN:
202 static BOOL canOpponentMoveOrJump( GridCell *first, GridCell *second ) {
203 return first.empty || (first.bit.friendly && second.empty);
206 - (BOOL) canOpponentMoveFrom: (GridCell*)src
208 if( ! src.bit.unfriendly )
210 Square *square = (Square*)src;
211 if( square.bit.tag ) // remember, it's opponent's piece, so directions are reversed
212 if( canOpponentMoveOrJump(square.fl,square.fl.fl) || canOpponentMoveOrJump(square.fr,square.fr.fr) )
214 return canOpponentMoveOrJump(square.bl,square.bl.bl) || canOpponentMoveOrJump(square.br,square.br.br);
217 - (Player*) checkForWinner
219 for( GridCell *cell in _board.cells )
220 if( [self canOpponentMoveFrom: cell] ) {
221 //NSLog(@"Checkers: %@ can move from %@",self.currentPlayer.nextPlayer,cell);
224 return self.currentPlayer;
228 - (BOOL) applyMoveString: (NSString*)move
231 for( NSString *ident in [move componentsSeparatedByString: @"-"] ) {
232 while( [ident hasSuffix: @"!"] || [ident hasSuffix: @"*"] )
233 ident = [ident substringToIndex: ident.length-1];
234 GridCell *dst = [_board cellWithName: ident];
237 if( src && ! [self animateMoveFrom: src to: dst] )