Source/HexchequerGame.m
author Jens Alfke <jens@mooseyard.com>
Wed Jul 09 17:07:45 2008 -0700 (2008-07-09)
changeset 15 73f8c889f053
parent 12 4e567e11f45f
child 16 28392c9a969f
permissions -rw-r--r--
More tweaks, including a "reversed" property for Grids to show the second player's perspective without turning the pieces upside-down.
     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     HexGrid *grid = [[HexGrid alloc] initWithRows: 9 columns: 9 frame: _board.bounds];
    36     _grid = grid;
    37     [_board addSublayer: _grid];
    38     CGPoint pos = grid.position;
    39     pos.x += grid.spacing.width / 4;    // The right edge of the grid is blank because of the unused cells outside the hexagon
    40     grid.position = pos;
    41     grid.allowsMoves = YES;
    42     grid.allowsCaptures = NO;      // no land-on captures, that is
    43     grid.cellColor = CreateGray(1.0, 0.25);
    44     grid.lineColor = kTranslucentLightGrayColor;
    45     grid.reversed = ! [[self.players objectAtIndex: 0] isLocal];
    46     [grid addCellsInHexagon];
    47 }
    48 
    49 
    50 - (NSString*) initialStateString
    51 {
    52     return @"111111111111111111-------------------------222222222222222222";
    53 }
    54 
    55 
    56 - (BOOL) canBit: (Bit*)bit moveFrom: (id<BitHolder>)srcHolder to: (id<BitHolder>)dstHolder
    57 {
    58     Hex *src=(Hex*)srcHolder, *dst=(Hex*)dstHolder;
    59     if( [bit valueForKey: @"King"] )
    60         if( dst==src.bl || dst==src.br || dst==src.l || dst==src.r
    61            || (src.bl.bit.unfriendly && dst==src.bl.bl) || (src.br.bit.unfriendly && dst==src.br.br)
    62            || (src.l.bit.unfriendly  && dst==src.l.l)   || (src.r.bit.unfriendly  && dst==src.r.r) )
    63             return YES;    
    64     return dst==src.fl || dst==src.fr
    65         || (src.fl.bit.unfriendly && dst==src.fl.fl) || (src.fr.bit.unfriendly && dst==src.fr.fr);
    66 }
    67 
    68 - (void) bit: (Bit*)bit movedFrom: (id<BitHolder>)srcHolder to: (id<BitHolder>)dstHolder
    69 {
    70     Hex *src=(Hex*)srcHolder, *dst=(Hex*)dstHolder;
    71     int playerIndex = self.currentPlayer.index;
    72 
    73     Turn *turn = self.currentTurn;
    74     if( turn.move.length==0 )
    75         [turn addToMove: src.name];
    76     [turn addToMove: @"-"];
    77     [turn addToMove: dst.name];
    78     
    79     BOOL isKing = ([bit valueForKey: @"King"] != nil);    
    80     PlaySound(isKing ?@"Funk" :@"Tink");
    81 
    82     // "King" a piece that made it to the last row:
    83     if( dst.row == (playerIndex ?0 :8) )
    84         if( ! isKing ) {
    85             PlaySound(@"Blow");
    86             bit.scale = 1.4;
    87             [bit setValue: @"King" forKey: @"King"];
    88             // don't set isKing flag - piece can't capture again after being kinged.
    89         }
    90 
    91     // Check for a capture:
    92     Hex *capture = nil;
    93     if(dst==src.fl.fl)
    94         capture = src.fl;
    95     else if(dst==src.fr.fr)
    96         capture = src.fr;
    97     else if(dst==src.bl.bl)
    98         capture = src.bl;
    99     else if(dst==src.br.br)
   100         capture = src.br;
   101     else if(dst==src.l.l)
   102         capture = src.l;
   103     else if(dst==src.r.r)
   104         capture = src.r;
   105     
   106     if( capture ) {
   107         PlaySound(@"Pop");
   108         [turn addToMove: @"!"];
   109         [capture destroyBit];
   110         
   111         // Now check if another capture is possible. If so, don't end the turn:
   112         if( (dst.fl.bit.unfriendly && dst.fl.fl.empty) || (dst.fr.bit.unfriendly && dst.fr.fr.empty) )
   113             return;
   114         if( isKing )
   115             if( (dst.bl.bit.unfriendly && dst.bl.bl.empty) || (dst.br.bit.unfriendly && dst.br.br.empty)
   116                     || (dst.l.bit.unfriendly && dst.l.l.empty) || (dst.r.bit.unfriendly && dst.r.r.empty))
   117                 return;
   118     }
   119     
   120     [self endTurn];
   121 }
   122 
   123 
   124 @end