Source/HexchequerGame.m
author Jens Alfke <jens@mooseyard.com>
Thu Jul 17 13:29:04 2008 -0700 (2008-07-17)
changeset 19 3b750982ff39
parent 15 73f8c889f053
child 20 7c9ecb09a612
permissions -rw-r--r--
Don't load Checkers sound files till a game is displayed on a board; this speeds up launch.
     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 #import "GGBUtils.h"
    28 
    29 
    30 @implementation HexchequerGame
    31 
    32 
    33 - (void) setUpBoard
    34 {
    35     // Create a hex grid and rotate it 30 degrees so the cells are edge-up:
    36     CGRect tableBounds = _table.bounds;
    37     CGFloat s = tableBounds.size.height / 9;
    38     HexGrid *board = [[HexGrid alloc] initWithRows: 9 columns: 9
    39                                            spacing: CGSizeMake(s,s)
    40                                           position: GetCGRectCenter(tableBounds)];
    41     board.anchorPoint = CGPointMake(0.47,0.5);  // Missing half-cells on right edge perturb center pt
    42     [board setValue: [NSNumber numberWithDouble: M_PI/6] forKeyPath: @"transform.rotation"];
    43     _board = board;
    44     [_table addSublayer: _board];
    45     board.allowsMoves = YES;
    46     board.allowsCaptures = NO;      // no land-on captures, that is
    47     board.cellColor = CreateGray(1.0, 0.25);
    48     board.lineColor = kTranslucentLightGrayColor;
    49     board.reversed = ! [[self.players objectAtIndex: 0] isLocal];
    50     [board addCellsInHexagon];
    51 }
    52 
    53 - (NSString*) initialStateString
    54 {
    55       return @"1111-1111--1111---1111-----------------2222---2222--2222-2222";
    56 }
    57 
    58 
    59 - (BOOL) canBit: (Bit*)bit moveFrom: (id<BitHolder>)srcHolder to: (id<BitHolder>)dstHolder
    60 {
    61     Hex *src=(Hex*)srcHolder, *dst=(Hex*)dstHolder;
    62     if( [bit valueForKey: @"King"] )
    63         if( dst==src.bl || dst==src.br || dst==src.l
    64            || (src.bl.bit.unfriendly && dst==src.bl.bl) || (src.br.bit.unfriendly && dst==src.br.br)
    65            || (src.l.bit.unfriendly  && dst==src.l.l) )
    66             return YES;    
    67     return dst==src.fl || dst==src.fr || dst==src.r
    68             || (src.fl.bit.unfriendly && dst==src.fl.fl) 
    69             || (src.fr.bit.unfriendly && dst==src.fr.fr) 
    70             || (src. r.bit.unfriendly && dst==src. r. r);
    71 }
    72 
    73 - (void) bit: (Bit*)bit movedFrom: (id<BitHolder>)srcHolder to: (id<BitHolder>)dstHolder
    74 {
    75     Hex *src=(Hex*)srcHolder, *dst=(Hex*)dstHolder;
    76 
    77     Turn *turn = self.currentTurn;
    78     if( turn.move.length==0 )
    79         [turn addToMove: src.name];
    80     [turn addToMove: @"-"];
    81     [turn addToMove: dst.name];
    82     
    83     BOOL isKing = ([bit valueForKey: @"King"] != nil);    
    84     PlaySound(isKing ?@"Funk" :@"Tink");
    85 
    86     // "King" a piece that made it to the last row:
    87     if( dst.fr == nil )
    88         if( ! isKing ) {
    89             PlaySound(@"Blow");
    90             bit.scale = 1.4;
    91             [bit setValue: @"King" forKey: @"King"];
    92             // don't set isKing flag - piece can't capture again after being kinged.
    93         }
    94 
    95     // Check for a capture:
    96     Hex *capture = nil;
    97     if(dst==src.fl.fl)
    98         capture = src.fl;
    99     else if(dst==src.fr.fr)
   100         capture = src.fr;
   101     else if(dst==src.bl.bl)
   102         capture = src.bl;
   103     else if(dst==src.br.br)
   104         capture = src.br;
   105     else if(dst==src.l.l)
   106         capture = src.l;
   107     else if(dst==src.r.r)
   108         capture = src.r;
   109     
   110     if( capture ) {
   111         PlaySound(@"Pop");
   112         [turn addToMove: @"!"];
   113         [capture destroyBit];
   114         
   115         // Now check if another capture is possible. If so, don't end the turn:
   116         if( (dst.fl.bit.unfriendly && dst.fl.fl.empty) 
   117                 || (dst.fr.bit.unfriendly && dst.fr.fr.empty) 
   118                 || (dst. r.bit.unfriendly && dst. r. r.empty) )
   119             return;
   120         if( isKing )
   121             if( (dst.bl.bit.unfriendly && dst.bl.bl.empty)
   122                     || (dst.br.bit.unfriendly && dst.br.br.empty)
   123                     || (dst.l.bit.unfriendly && dst.l.l.empty) )
   124                 return;
   125     }
   126     
   127     [self endTurn];
   128 }
   129 
   130 
   131 @end