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@0: - (void) addPieces: (NSString*)imageName jens@0: toGrid: (Grid*)grid jens@0: forPlayer: (int)playerNum jens@0: rows: (NSRange)rows jens@0: alternating: (BOOL)alternating jens@0: { jens@0: Piece *prototype = [[Piece alloc] initWithImageNamed: imageName scale: floor(grid.spacing.width * 0.8)]; jens@0: prototype.owner = [self.players objectAtIndex: playerNum]; jens@0: unsigned cols=grid.columns; jens@0: for( unsigned row=rows.location; row)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@0: BOOL isKing = ([bit valueForKey: @"King"] != nil); jens@0: 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@0: bit.scale = 1.4; jens@0: [bit setValue: @"King" forKey: @"King"]; 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@0: Bit *bit = capture.bit; jens@0: _numPieces[bit.owner.index]--; jens@0: [bit destroy]; 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@0: @end