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.
28 #import "QuartzUtils.h"
32 @implementation GoGame
35 + (int) dimensions {return 19;}
41 [self setNumberOfPlayers: 2];
42 [(Player*)[_players objectAtIndex: 0] setName: @"Red"];
43 [(Player*)[_players objectAtIndex: 1] setName: @"White"];
50 int dimensions = [[self class] dimensions];
51 CGSize size = _table.bounds.size;
52 CGFloat boardSide = MIN(size.width,size.height);
53 RectGrid *board = [[RectGrid alloc] initWithRows: dimensions columns: dimensions
54 frame: CGRectMake(floor((size.width-boardSide)/2),
55 floor((size.height-boardSide)/2),
56 boardSide,boardSide)];
59 grid.backgroundColor = GetCGPatternNamed(@"Wood.jpg");
60 grid.borderColor = kTranslucentLightGrayColor;
63 board.lineColor = kTranslucentGrayColor;
64 board.cellClass = [GoSquare class];
66 ((GoSquare*)[board cellAtRow: 2 column: 2]).dotted = YES;
67 ((GoSquare*)[board cellAtRow: 6 column: 6]).dotted = YES;
68 ((GoSquare*)[board cellAtRow: 2 column: 6]).dotted = YES;
69 ((GoSquare*)[board cellAtRow: 6 column: 2]).dotted = YES;
70 board.usesDiagonals = board.allowsMoves = board.allowsCaptures = NO;
71 [_table addSublayer: board];
74 CGRect gridFrame = board.frame;
75 CGFloat pieceSize = (int)board.spacing.width & ~1; // make sure it's even
76 CGFloat captureHeight = gridFrame.size.height-4*pieceSize;
77 _captured[0] = [[Stack alloc] initWithStartPos: CGPointMake(2*pieceSize,0)
78 spacing: CGSizeMake(0,pieceSize)
79 wrapInterval: floor(captureHeight/pieceSize)
80 wrapSpacing: CGSizeMake(-pieceSize,0)];
81 _captured[0].frame = CGRectMake(CGRectGetMinX(gridFrame)-3*pieceSize,
82 CGRectGetMinY(gridFrame)+3*pieceSize,
83 2*pieceSize, captureHeight);
84 _captured[0].zPosition = kPieceZ+1;
85 [_table addSublayer: _captured[0]];
86 [_captured[0] release];
88 _captured[1] = [[Stack alloc] initWithStartPos: CGPointMake(0,captureHeight)
89 spacing: CGSizeMake(0,-pieceSize)
90 wrapInterval: floor(captureHeight/pieceSize)
91 wrapSpacing: CGSizeMake(pieceSize,0)];
92 _captured[1].frame = CGRectMake(CGRectGetMaxX(gridFrame)+pieceSize,
93 CGRectGetMinY(gridFrame)+pieceSize,
94 2*pieceSize, captureHeight);
95 _captured[1].zPosition = kPieceZ+1;
96 [_table addSublayer: _captured[1]];
97 [_captured[1] release];
102 - (CGImageRef) iconForPlayer: (int)playerNum
104 return GetCGImageNamed( playerNum ?@"bot086.png" :@"bot089.png" );
107 - (Piece*) pieceForPlayer: (int)index
109 NSString *imageName = index ?@"bot086.png" :@"bot089.png";
110 CGFloat pieceSize = (int)(_board.spacing.width * 0.9) & ~1; // make sure it's even
111 Piece *stone = [[Piece alloc] initWithImageNamed: imageName scale: pieceSize];
112 stone.owner = [self.players objectAtIndex: index];
113 return [stone autorelease];
116 - (Bit*) bitToPlaceInHolder: (id<BitHolder>)holder
118 if( holder.bit != nil || ! [holder isKindOfClass: [GoSquare class]] )
121 return [self pieceForPlayer: self.currentPlayer.index];
125 - (BOOL) canBit: (Bit*)bit moveFrom: (id<BitHolder>)srcHolder
127 return (srcHolder==nil);
131 - (BOOL) canBit: (Bit*)bit moveFrom: (id<BitHolder>)srcHolder to: (id<BitHolder>)dstHolder
133 if( srcHolder!=nil || ! [dstHolder isKindOfClass: [Square class]] )
135 Square *dst=(Square*)dstHolder;
137 // There should be a check here for a "ko" (repeated position) ... exercise for the reader!
139 // Check for suicidal move. First an easy check for an empty adjacent space:
140 NSArray *neighbors = dst.neighbors;
141 for( GridCell *c in neighbors )
143 return YES; // there's an empty space
144 // If the piece is surrounded, check the neighboring groups' liberties:
145 for( GridCell *c in neighbors ) {
147 [c getGroup: &nLiberties];
148 if( c.bit.unfriendly ) {
149 if( nLiberties <= 1 )
150 return YES; // the move captures, so it's not suicidal
153 return YES; // the stone joins a group with other liberties
160 - (void) bit: (Bit*)bit movedFrom: (id<BitHolder>)srcHolder to: (id<BitHolder>)dstHolder
162 Square *dst=(Square*)dstHolder;
163 int curIndex = self.currentPlayer.index;
164 // Check for captured enemy groups:
166 for( GridCell *c in dst.neighbors )
167 if( c.bit.unfriendly ) {
169 NSSet *group = [c getGroup: &nLiberties];
170 if( nLiberties == 0 ) {
172 for( GridCell *capture in group )
173 [_captured[curIndex] addBit: capture.bit]; // Moves piece to POW camp!
179 [self.currentTurn addToMove: dst.name];
184 // This sample code makes no attempt to detect the end of the game, or count score,
185 // both of which are rather complex to decide in Go.
192 - (NSString*) stateString
196 for( int y=0; y<n; y++ )
197 for( int x=0; x<n; x++ ) {
198 Bit *bit = [_board cellAtRow: y column: x].bit;
203 ch = '1' + bit.owner.index;
206 return [NSString stringWithCharacters: state length: n*n];
209 - (void) setStateString: (NSString*)state
211 NSLog(@"Go: setStateString: '%@'",state);
213 for( int y=0; y<n; y++ )
214 for( int x=0; x<n; x++ ) {
217 if( i < state.length ) {
218 int index = [state characterAtIndex: i] - '1';
219 if( index==0 || index==1 )
220 piece = [self pieceForPlayer: index];
222 [_board cellAtRow: y column: x].bit = piece;
227 - (BOOL) applyMoveString: (NSString*)move
229 NSLog(@"Go: applyMoveString: '%@'",move);
230 return [self animatePlacementIn: [_board cellWithName: move]];
237 @implementation Go9Game
238 + (NSString*) displayName {return @"Go (9x9)";}
239 + (int) dimensions {return 9;}
243 @implementation Go13Game
244 + (NSString*) displayName {return @"Go (13x13)";}
245 + (int) dimensions {return 13;}