Source/HexchequerGame.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 "HexchequerGame.h"
    24 #import "HexGrid.h"
    25 #import "Piece.h"
    26 #import "QuartzUtils.h"
    27 
    28 
    29 @implementation HexchequerGame
    30 
    31 
    32 - (Grid*) x_makeGrid
    33 {
    34     HexGrid *grid = [[[HexGrid alloc] initWithRows: 9 columns: 9 frame: _board.bounds] autorelease];
    35     CGPoint pos = grid.position;
    36     pos.x = floor((_board.bounds.size.width-grid.frame.size.width)/2);
    37     grid.position = pos;
    38     [grid addCellsInHexagon];
    39     grid.allowsMoves = YES;
    40     grid.allowsCaptures = NO;      // no land-on captures, that is
    41     grid.cellColor = CGColorCreateGenericGray(1.0, 0.25);
    42     grid.lineColor = kTranslucentLightGrayColor;
    43     
    44     [self addPieces: @"Green Ball.png" toGrid: grid forPlayer: 0 rows: NSMakeRange(0,2) alternating: NO];
    45     [self addPieces: @"Red Ball.png"   toGrid: grid forPlayer: 1 rows: NSMakeRange(7,2) alternating: NO];
    46     return grid;
    47 }
    48 
    49 
    50 - (BOOL) canBit: (Bit*)bit moveFrom: (id<BitHolder>)srcHolder to: (id<BitHolder>)dstHolder
    51 {
    52     Hex *src=(Hex*)srcHolder, *dst=(Hex*)dstHolder;
    53     if( [bit valueForKey: @"King"] )
    54         if( dst==src.bl || dst==src.br || dst==src.l || dst==src.r
    55            || (src.bl.bit.unfriendly && dst==src.bl.bl) || (src.br.bit.unfriendly && dst==src.br.br)
    56            || (src.l.bit.unfriendly  && dst==src.l.l)   || (src.r.bit.unfriendly  && dst==src.r.r) )
    57             return YES;    
    58     return dst==src.fl || dst==src.fr
    59         || (src.fl.bit.unfriendly && dst==src.fl.fl) || (src.fr.bit.unfriendly && dst==src.fr.fr);
    60 }
    61 
    62 - (void) bit: (Bit*)bit movedFrom: (id<BitHolder>)srcHolder to: (id<BitHolder>)dstHolder
    63 {
    64     Hex *src=(Hex*)srcHolder, *dst=(Hex*)dstHolder;
    65     int playerIndex = self.currentPlayer.index;
    66     BOOL isKing = ([bit valueForKey: @"King"] != nil);
    67     
    68     [[NSSound soundNamed: (isKing ?@"Funk" :@"Tink")] play];
    69 
    70     // "King" a piece that made it to the last row:
    71     if( dst.row == (playerIndex ?0 :8) )
    72         if( ! isKing ) {
    73             [[NSSound soundNamed: @"Blow"] play];
    74             bit.scale = 1.4;
    75             [bit setValue: @"King" forKey: @"King"];
    76             // don't set isKing flag - piece can't capture again after being kinged.
    77         }
    78 
    79     // Check for a capture:
    80     Hex *capture = nil;
    81     if(dst==src.fl.fl)
    82         capture = src.fl;
    83     else if(dst==src.fr.fr)
    84         capture = src.fr;
    85     else if(dst==src.bl.bl)
    86         capture = src.bl;
    87     else if(dst==src.br.br)
    88         capture = src.br;
    89     else if(dst==src.l.l)
    90         capture = src.l;
    91     else if(dst==src.r.r)
    92         capture = src.r;
    93     
    94     if( capture ) {
    95         [[NSSound soundNamed: @"Pop"] play];
    96         Bit *bit = capture.bit;
    97         _numPieces[bit.owner.index]--;
    98         [bit destroy];
    99         
   100         // Now check if another capture is possible. If so, don't end the turn:
   101         if( (dst.fl.bit.unfriendly && dst.fl.fl.empty) || (dst.fr.bit.unfriendly && dst.fr.fr.empty) )
   102             return;
   103         if( isKing )
   104             if( (dst.bl.bit.unfriendly && dst.bl.bl.empty) || (dst.br.bit.unfriendly && dst.br.br.empty)
   105                     || (dst.l.bit.unfriendly && dst.l.l.empty) || (dst.r.bit.unfriendly && dst.r.r.empty))
   106                 return;
   107     }
   108     
   109     [self endTurn];
   110 }
   111 
   112 
   113 @end