Source/CheckersGame.m
author Jens Alfke <jens@mooseyard.com>
Fri Mar 07 11:43:02 2008 -0800 (2008-03-07)
changeset 0 e9f7ba4718e1
child 1 3eb7be1dd7b6
permissions -rw-r--r--
Initial check-in into Mercurial. Branched from 1.0 release of Apple's sample code. No longer requires garbage collection. Fixed some memory leaks of CG objects. Fixed a bug when advancing to the 8th row in the Checkers game.
     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.
     4 
     5     Redistribution and use in source and binary forms, with or without modification, are permitted
     6     provided that the following conditions are met:
     7 
     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.
    13 
    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.
    22 */
    23 #import "CheckersGame.h"
    24 #import "Grid.h"
    25 #import "Piece.h"
    26 #import "QuartzUtils.h"
    27 
    28 
    29 @implementation CheckersGame
    30 
    31 
    32 - (void) addPieces: (NSString*)imageName
    33             toGrid: (Grid*)grid
    34          forPlayer: (int)playerNum
    35               rows: (NSRange)rows
    36        alternating: (BOOL)alternating
    37 {
    38     Piece *prototype = [[Piece alloc] initWithImageNamed: imageName scale: floor(grid.spacing.width * 0.8)];
    39     prototype.owner = [self.players objectAtIndex: playerNum];
    40     unsigned cols=grid.columns;
    41     for( unsigned row=rows.location; row<NSMaxRange(rows); row++ )
    42         for( unsigned col=0; col<cols; col++ ) {
    43             if( !alternating || ((row+col) & 1) == 0 ) {
    44                 GridCell *cell = [grid cellAtRow: row column: col];
    45                 if( cell ) {
    46                     Piece *piece = [prototype copy];
    47                     cell.bit = piece;
    48                     [piece release];
    49                     //cell.bit.rotation = random() % 360; // keeps pieces from looking too samey
    50                     _numPieces[playerNum]++;
    51                 }
    52             }
    53         }
    54     [prototype release];
    55 }
    56 
    57 
    58 - (Grid*) x_makeGrid
    59 {
    60     RectGrid *grid = [[[RectGrid alloc] initWithRows: 8 columns: 8 frame: _board.bounds] autorelease];
    61     CGPoint pos = grid.position;
    62     pos.x = floor((_board.bounds.size.width-grid.frame.size.width)/2);
    63     [grid addAllCells];
    64     grid.position = pos;
    65     grid.allowsMoves = YES;
    66     grid.allowsCaptures = NO;
    67     grid.cellColor = CGColorCreateGenericGray(0.0, 0.25);
    68     grid.altCellColor = CGColorCreateGenericGray(1.0, 0.25);
    69     grid.lineColor = nil;
    70     [self addPieces: @"Green Ball.png" toGrid: grid forPlayer: 0 rows: NSMakeRange(0,3) alternating: YES];
    71     [self addPieces: @"Red Ball.png"   toGrid: grid forPlayer: 1 rows: NSMakeRange(5,3) alternating: YES];
    72     return grid;
    73 }
    74 
    75 
    76 - (id) initWithBoard: (CALayer*)board
    77 {
    78     self = [super initWithBoard: board];
    79     if (self != nil) {
    80         [self setNumberOfPlayers: 2];
    81         [board addSublayer: [self x_makeGrid]];
    82         [self nextPlayer];
    83     }
    84     return self;
    85 }
    86 
    87 
    88 - (BOOL) canBit: (Bit*)bit moveFrom: (id<BitHolder>)srcHolder to: (id<BitHolder>)dstHolder
    89 {
    90     Square *src=(Square*)srcHolder, *dst=(Square*)dstHolder;
    91     if( [bit valueForKey: @"King"] )
    92         if( dst==src.bl || dst==src.br || dst==src.l || dst==src.r
    93            || (src.bl.bit.unfriendly && dst==src.bl.bl) || (src.br.bit.unfriendly && dst==src.br.br) )
    94             return YES;    
    95     return dst==src.fl || dst==src.fr
    96         || (src.fl.bit.unfriendly && dst==src.fl.fl) || (src.fr.bit.unfriendly && dst==src.fr.fr);
    97 }
    98 
    99 - (void) bit: (Bit*)bit movedFrom: (id<BitHolder>)srcHolder to: (id<BitHolder>)dstHolder
   100 {
   101     Square *src=(Square*)srcHolder, *dst=(Square*)dstHolder;
   102     int playerIndex = self.currentPlayer.index;
   103     BOOL isKing = ([bit valueForKey: @"King"] != nil);
   104     
   105     [[NSSound soundNamed: (isKing ?@"Funk" :@"Tink")] play];
   106 
   107     // "King" a piece that made it to the last row:
   108     if( dst.row == (playerIndex ?0 :7) )
   109         if( ! isKing ) {
   110             [[NSSound soundNamed: @"Blow"] play];
   111             bit.scale = 1.4;
   112             [bit setValue: @"King" forKey: @"King"];
   113             // don't set isKing flag - piece can't jump again after being kinged.
   114         }
   115 
   116     // Check for a capture:
   117     Square *capture = nil;
   118     if(dst==src.fl.fl)
   119         capture = src.fl;
   120     else if(dst==src.fr.fr)
   121         capture = src.fr;
   122     else if(dst==src.bl.bl)
   123         capture = src.bl;
   124     else if(dst==src.br.br)
   125         capture = src.br;
   126     
   127     if( capture ) {
   128         [[NSSound soundNamed: @"Pop"] play];
   129         Bit *bit = capture.bit;
   130         _numPieces[bit.owner.index]--;
   131         [bit destroy];
   132         
   133         // Now check if another capture is possible. If so, don't end the turn:
   134         if( (dst.fl.bit.unfriendly && dst.fl.fl.empty) || (dst.fr.bit.unfriendly && dst.fr.fr.empty) )
   135             return;
   136         if( isKing )
   137             if( (dst.bl.bit.unfriendly && dst.bl.bl.empty) || (dst.br.bit.unfriendly && dst.br.br.empty) )
   138                 return;
   139     }
   140     
   141     [self endTurn];
   142 }
   143 
   144 - (Player*) checkForWinner
   145 {
   146     // Whoever runs out of pieces loses:
   147     if( _numPieces[0]==0 )
   148         return [self.players objectAtIndex: 1];
   149     else if( _numPieces[1]==0 )
   150         return [self.players objectAtIndex: 0];
   151     else
   152         return nil;
   153 }
   154 
   155 
   156 @end