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.
5 Redistribution and use in source and binary forms, with or without modification, are permitted
6 provided that the following conditions are met:
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.
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.
23 #import "HexchequerGame.h"
26 #import "QuartzUtils.h"
30 @implementation HexchequerGame
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"];
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];
53 - (NSString*) initialStateString
55 return @"1111-1111--1111---1111-----------------2222---2222--2222-2222";
59 - (BOOL) canBit: (Bit*)bit moveFrom: (id<BitHolder>)srcHolder to: (id<BitHolder>)dstHolder
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) )
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);
73 - (void) bit: (Bit*)bit movedFrom: (id<BitHolder>)srcHolder to: (id<BitHolder>)dstHolder
75 Hex *src=(Hex*)srcHolder, *dst=(Hex*)dstHolder;
77 Turn *turn = self.currentTurn;
78 if( turn.move.length==0 )
79 [turn addToMove: src.name];
80 [turn addToMove: @"-"];
81 [turn addToMove: dst.name];
83 BOOL isKing = ([bit valueForKey: @"King"] != nil);
84 PlaySound(isKing ?@"Funk" :@"Tink");
86 // "King" a piece that made it to the last row:
91 [bit setValue: @"King" forKey: @"King"];
92 // don't set isKing flag - piece can't capture again after being kinged.
95 // Check for a capture:
99 else if(dst==src.fr.fr)
101 else if(dst==src.bl.bl)
103 else if(dst==src.br.br)
105 else if(dst==src.l.l)
107 else if(dst==src.r.r)
112 [turn addToMove: @"!"];
113 [capture destroyBit];
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) )
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) )