jens@0: /* This code is based on Apple's "GeekGameBoard" sample code, version 1.0. jens@0: http://developer.apple.com/samplecode/GeekGameBoard/ jens@0: Copyright © 2007 Apple Inc. Copyright © 2008 Jens Alfke. All Rights Reserved. jens@0: jens@0: Redistribution and use in source and binary forms, with or without modification, are permitted jens@0: provided that the following conditions are met: jens@0: jens@0: * Redistributions of source code must retain the above copyright notice, this list of conditions jens@0: and the following disclaimer. jens@0: * Redistributions in binary form must reproduce the above copyright notice, this list of jens@0: conditions and the following disclaimer in the documentation and/or other materials provided jens@0: with the distribution. jens@0: jens@0: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR jens@0: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND jens@0: FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRI- jens@0: BUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES jens@0: (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR jens@0: PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN jens@0: CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF jens@0: THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. jens@0: */ jens@0: #import "CheckersGame.h" jens@0: #import "Grid.h" jens@0: #import "Piece.h" jens@0: #import "QuartzUtils.h" jens@1: #import "GGBUtils.h" jens@0: jens@0: jens@0: @implementation CheckersGame jens@0: jens@0: jens@10: - (id) init jens@10: { jens@10: self = [super init]; jens@10: if (self != nil) { jens@10: _cells = [[NSMutableArray alloc] init]; jens@10: [self setNumberOfPlayers: 2]; jens@10: jens@10: PreloadSound(@"Tink"); jens@10: PreloadSound(@"Funk"); jens@10: PreloadSound(@"Blow"); jens@10: PreloadSound(@"Pop"); jens@10: } jens@10: return self; jens@10: } jens@10: jens@10: - (CGImageRef) iconForPlayer: (int)playerNum jens@10: { jens@10: return GetCGImageNamed( playerNum==0 ?@"Green.png" :@"Red.png" ); jens@10: } jens@10: jens@7: - (Piece*) pieceForPlayer: (int)playerNum jens@0: { jens@9: Piece *p = [[Piece alloc] initWithImageNamed: (playerNum==0 ?@"Green.png" :@"Red.png") jens@9: scale: floor(_grid.spacing.width * 1.0)]; jens@7: p.owner = [self.players objectAtIndex: playerNum]; jens@7: p.name = playerNum ?@"2" :@"1"; jens@7: return [p autorelease]; jens@0: } jens@0: jens@10: - (void) makeKing: (Piece*)piece jens@10: { jens@10: piece.scale = 1.4; jens@10: [piece setValue: @"King" forKey: @"King"]; jens@10: piece.name = piece.owner.index ?@"4" :@"3"; jens@10: } jens@10: jens@10: - (void) setUpBoard jens@0: { jens@0: RectGrid *grid = [[[RectGrid alloc] initWithRows: 8 columns: 8 frame: _board.bounds] autorelease]; jens@7: _grid = grid; jens@10: [_board addSublayer: _grid]; jens@7: CGPoint pos = _grid.position; jens@0: pos.x = floor((_board.bounds.size.width-grid.frame.size.width)/2); jens@0: grid.position = pos; jens@0: grid.allowsMoves = YES; jens@0: grid.allowsCaptures = NO; jens@1: grid.cellColor = CreateGray(0.0, 0.25); jens@1: grid.altCellColor = CreateGray(1.0, 0.25); jens@0: grid.lineColor = nil; jens@7: jens@7: [grid addAllCells]; jens@10: [_cells removeAllObjects]; jens@7: for( int i=0; i<32; i++ ) { jens@7: int row = i/4; jens@7: [_cells addObject: [_grid cellAtRow: row column: 2*(i%4) + (row&1)]]; jens@7: } jens@0: } jens@0: jens@7: - (void) dealloc jens@7: { jens@7: [_cells release]; jens@7: [_grid release]; jens@7: [super dealloc]; jens@7: } jens@7: jens@7: jens@10: - (NSString*) initialStateString jens@10: { jens@10: return @"111111111111--------222222222222"; jens@10: } jens@10: jens@7: - (NSString*) stateString jens@7: { jens@7: unichar state[_cells.count]; jens@7: int i = 0; jens@7: for( GridCell *cell in _cells ) { jens@7: NSString *ident = cell.bit.name; jens@7: if( ident ) jens@7: state[i++] = [ident characterAtIndex: 0]; jens@7: else jens@7: state[i++] = '-'; jens@7: } jens@7: return [NSString stringWithCharacters: state length: i]; jens@7: } jens@7: jens@7: - (void) setStateString: (NSString*)state jens@7: { jens@7: _numPieces[0] = _numPieces[1] = 0; jens@7: int i = 0; jens@7: for( GridCell *cell in _cells ) { jens@7: Piece *piece; jens@10: int which = [state characterAtIndex: i++] - '1'; jens@10: if( which >=0 && which < 4 ) { jens@10: int player = (which & 1); jens@10: piece = [self pieceForPlayer: player]; jens@10: _numPieces[player]++; jens@10: if( which & 2 ) jens@10: [self makeKing: piece]; jens@10: } else jens@10: piece = nil; jens@7: cell.bit = piece; jens@7: } jens@7: } jens@7: jens@0: jens@0: - (BOOL) canBit: (Bit*)bit moveFrom: (id)srcHolder to: (id)dstHolder jens@0: { jens@0: Square *src=(Square*)srcHolder, *dst=(Square*)dstHolder; jens@0: if( [bit valueForKey: @"King"] ) jens@0: if( dst==src.bl || dst==src.br || dst==src.l || dst==src.r jens@0: || (src.bl.bit.unfriendly && dst==src.bl.bl) || (src.br.bit.unfriendly && dst==src.br.br) ) jens@0: return YES; jens@0: return dst==src.fl || dst==src.fr jens@0: || (src.fl.bit.unfriendly && dst==src.fl.fl) || (src.fr.bit.unfriendly && dst==src.fr.fr); jens@0: } jens@0: jens@0: - (void) bit: (Bit*)bit movedFrom: (id)srcHolder to: (id)dstHolder jens@0: { jens@0: Square *src=(Square*)srcHolder, *dst=(Square*)dstHolder; jens@0: int playerIndex = self.currentPlayer.index; jens@7: jens@10: Turn *turn = self.currentTurn; jens@10: if( turn.move.length==0 ) jens@10: [turn addToMove: src.name]; jens@10: [turn addToMove: @"-"]; jens@10: [turn addToMove: dst.name]; jens@7: jens@0: BOOL isKing = ([bit valueForKey: @"King"] != nil); jens@1: PlaySound(isKing ?@"Funk" :@"Tink"); jens@0: jens@0: // "King" a piece that made it to the last row: jens@0: if( dst.row == (playerIndex ?0 :7) ) jens@0: if( ! isKing ) { jens@1: PlaySound(@"Blow"); jens@10: [self makeKing: (Piece*)bit]; jens@10: [turn addToMove: @"*"]; jens@0: // don't set isKing flag - piece can't jump again after being kinged. jens@0: } jens@0: jens@0: // Check for a capture: jens@0: Square *capture = nil; jens@0: if(dst==src.fl.fl) jens@0: capture = src.fl; jens@0: else if(dst==src.fr.fr) jens@0: capture = src.fr; jens@0: else if(dst==src.bl.bl) jens@0: capture = src.bl; jens@0: else if(dst==src.br.br) jens@0: capture = src.br; jens@0: jens@0: if( capture ) { jens@1: PlaySound(@"Pop"); jens@10: _numPieces[capture.bit.owner.index]--; jens@10: [capture destroyBit]; jens@10: [turn addToMove: @"!"]; jens@0: jens@0: // Now check if another capture is possible. If so, don't end the turn: jens@0: if( (dst.fl.bit.unfriendly && dst.fl.fl.empty) || (dst.fr.bit.unfriendly && dst.fr.fr.empty) ) jens@0: return; jens@0: if( isKing ) jens@0: if( (dst.bl.bit.unfriendly && dst.bl.bl.empty) || (dst.br.bit.unfriendly && dst.br.br.empty) ) jens@0: return; jens@0: } jens@0: jens@0: [self endTurn]; jens@0: } jens@0: jens@0: - (Player*) checkForWinner jens@0: { jens@0: // Whoever runs out of pieces loses: jens@0: if( _numPieces[0]==0 ) jens@0: return [self.players objectAtIndex: 1]; jens@0: else if( _numPieces[1]==0 ) jens@0: return [self.players objectAtIndex: 0]; jens@0: else jens@0: return nil; jens@0: } jens@0: jens@0: jens@7: - (BOOL) applyMoveString: (NSString*)move jens@7: { jens@7: GridCell *src = nil; jens@10: for( NSString *ident in [move componentsSeparatedByString: @"-"] ) { jens@10: while( [ident hasSuffix: @"!"] || [ident hasSuffix: @"*"] ) jens@10: ident = [ident substringToIndex: ident.length-1]; jens@7: GridCell *dst = [_grid cellWithName: ident]; jens@10: if( dst == nil ) jens@10: return NO; jens@10: if( src && ! [self animateMoveFrom: src to: dst] ) jens@10: return NO; jens@7: src = dst; jens@7: } jens@7: return YES; jens@7: } jens@7: jens@7: jens@0: @end