jens@0
|
1 |
/* This code is based on Apple's "GeekGameBoard" sample code, version 1.0.
|
jens@0
|
2 |
http://developer.apple.com/samplecode/GeekGameBoard/
|
jens@0
|
3 |
Copyright © 2007 Apple Inc. Copyright © 2008 Jens Alfke. All Rights Reserved.
|
jens@0
|
4 |
|
jens@0
|
5 |
Redistribution and use in source and binary forms, with or without modification, are permitted
|
jens@0
|
6 |
provided that the following conditions are met:
|
jens@0
|
7 |
|
jens@0
|
8 |
* Redistributions of source code must retain the above copyright notice, this list of conditions
|
jens@0
|
9 |
and the following disclaimer.
|
jens@0
|
10 |
* Redistributions in binary form must reproduce the above copyright notice, this list of
|
jens@0
|
11 |
conditions and the following disclaimer in the documentation and/or other materials provided
|
jens@0
|
12 |
with the distribution.
|
jens@0
|
13 |
|
jens@0
|
14 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
|
jens@0
|
15 |
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
jens@0
|
16 |
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRI-
|
jens@0
|
17 |
BUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
jens@0
|
18 |
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
jens@0
|
19 |
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
jens@0
|
20 |
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
|
jens@0
|
21 |
THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
jens@0
|
22 |
*/
|
jens@0
|
23 |
#import "GoGame.h"
|
jens@0
|
24 |
#import "Grid.h"
|
jens@0
|
25 |
#import "Piece.h"
|
jens@0
|
26 |
#import "Dispenser.h"
|
jens@0
|
27 |
#import "Stack.h"
|
jens@0
|
28 |
#import "QuartzUtils.h"
|
jens@1
|
29 |
#import "GGBUtils.h"
|
jens@0
|
30 |
|
jens@0
|
31 |
|
jens@0
|
32 |
@implementation GoGame
|
jens@0
|
33 |
|
jens@0
|
34 |
|
jens@10
|
35 |
+ (int) dimensions {return 19;}
|
jens@10
|
36 |
|
jens@10
|
37 |
- (id) init
|
jens@0
|
38 |
{
|
jens@10
|
39 |
self = [super init];
|
jens@0
|
40 |
if (self != nil) {
|
jens@0
|
41 |
[self setNumberOfPlayers: 2];
|
jens@21
|
42 |
[(Player*)[_players objectAtIndex: 0] setName: @"Black"];
|
jens@3
|
43 |
[(Player*)[_players objectAtIndex: 1] setName: @"White"];
|
jens@10
|
44 |
}
|
jens@0
|
45 |
return self;
|
jens@0
|
46 |
}
|
jens@10
|
47 |
|
jens@10
|
48 |
- (void) setUpBoard
|
jens@10
|
49 |
{
|
jens@10
|
50 |
int dimensions = [[self class] dimensions];
|
jens@21
|
51 |
CGRect tableBounds = _table.bounds;
|
jens@21
|
52 |
CGSize size = tableBounds.size;
|
jens@21
|
53 |
CGFloat boardSide = MIN(size.width* dimensions/(CGFloat)(dimensions+2),size.height);
|
jens@16
|
54 |
RectGrid *board = [[RectGrid alloc] initWithRows: dimensions columns: dimensions
|
jens@10
|
55 |
frame: CGRectMake(floor((size.width-boardSide)/2),
|
jens@10
|
56 |
floor((size.height-boardSide)/2),
|
jens@10
|
57 |
boardSide,boardSide)];
|
jens@16
|
58 |
_board = board;
|
jens@10
|
59 |
/*
|
jens@10
|
60 |
grid.backgroundColor = GetCGPatternNamed(@"Wood.jpg");
|
jens@10
|
61 |
grid.borderColor = kTranslucentLightGrayColor;
|
jens@10
|
62 |
grid.borderWidth = 2;
|
jens@10
|
63 |
*/
|
jens@16
|
64 |
board.lineColor = kTranslucentGrayColor;
|
jens@16
|
65 |
board.cellClass = [GoSquare class];
|
jens@16
|
66 |
[board addAllCells];
|
jens@16
|
67 |
((GoSquare*)[board cellAtRow: 2 column: 2]).dotted = YES;
|
jens@16
|
68 |
((GoSquare*)[board cellAtRow: 6 column: 6]).dotted = YES;
|
jens@16
|
69 |
((GoSquare*)[board cellAtRow: 2 column: 6]).dotted = YES;
|
jens@16
|
70 |
((GoSquare*)[board cellAtRow: 6 column: 2]).dotted = YES;
|
jens@16
|
71 |
board.usesDiagonals = board.allowsMoves = board.allowsCaptures = NO;
|
jens@16
|
72 |
[_table addSublayer: board];
|
jens@16
|
73 |
[board release];
|
jens@10
|
74 |
|
jens@16
|
75 |
CGRect gridFrame = board.frame;
|
jens@16
|
76 |
CGFloat pieceSize = (int)board.spacing.width & ~1; // make sure it's even
|
jens@21
|
77 |
CGFloat captureMinY = CGRectGetMinY(tableBounds) + pieceSize/2,
|
jens@21
|
78 |
captureHeight = size.height - pieceSize;
|
jens@21
|
79 |
_captured[0] = [[Stack alloc] initWithStartPos: CGPointMake(pieceSize/2,0)
|
jens@10
|
80 |
spacing: CGSizeMake(0,pieceSize)
|
jens@10
|
81 |
wrapInterval: floor(captureHeight/pieceSize)
|
jens@21
|
82 |
wrapSpacing: CGSizeMake(pieceSize,0)];
|
jens@21
|
83 |
_captured[0].frame = CGRectMake(CGRectGetMinX(tableBounds),
|
jens@21
|
84 |
captureMinY,
|
jens@21
|
85 |
CGRectGetMinX(gridFrame)-CGRectGetMinX(tableBounds),
|
jens@21
|
86 |
captureHeight);
|
jens@10
|
87 |
_captured[0].zPosition = kPieceZ+1;
|
jens@16
|
88 |
[_table addSublayer: _captured[0]];
|
jens@10
|
89 |
[_captured[0] release];
|
jens@10
|
90 |
|
jens@21
|
91 |
_captured[1] = [[Stack alloc] initWithStartPos: CGPointMake(pieceSize/2,captureHeight)
|
jens@10
|
92 |
spacing: CGSizeMake(0,-pieceSize)
|
jens@10
|
93 |
wrapInterval: floor(captureHeight/pieceSize)
|
jens@21
|
94 |
wrapSpacing: CGSizeMake(-pieceSize,0)];
|
jens@21
|
95 |
_captured[1].frame = CGRectMake(CGRectGetMaxX(gridFrame),
|
jens@21
|
96 |
captureMinY,
|
jens@21
|
97 |
CGRectGetMaxX(tableBounds)-CGRectGetMaxX(gridFrame),
|
jens@21
|
98 |
captureHeight);
|
jens@21
|
99 |
_captured[1].startPos = CGPointMake(CGRectGetMaxX(_captured[1].bounds)-pieceSize/2, captureHeight);
|
jens@10
|
100 |
_captured[1].zPosition = kPieceZ+1;
|
jens@16
|
101 |
[_table addSublayer: _captured[1]];
|
jens@10
|
102 |
[_captured[1] release];
|
jens@0
|
103 |
|
jens@10
|
104 |
PreloadSound(@"Pop");
|
jens@10
|
105 |
}
|
jens@10
|
106 |
|
jens@10
|
107 |
- (CGImageRef) iconForPlayer: (int)playerNum
|
jens@10
|
108 |
{
|
jens@22
|
109 |
return GetCGImageNamed( playerNum ?@"ball-white.png" :@"ball-black.png" );
|
jens@10
|
110 |
}
|
jens@10
|
111 |
|
jens@10
|
112 |
- (Piece*) pieceForPlayer: (int)index
|
jens@10
|
113 |
{
|
jens@22
|
114 |
NSString *imageName = index ?@"ball-white.png" :@"ball-black.png";
|
jens@22
|
115 |
CGFloat pieceSize = (int)(_board.spacing.width * 1.0) & ~1; // make sure it's even
|
jens@10
|
116 |
Piece *stone = [[Piece alloc] initWithImageNamed: imageName scale: pieceSize];
|
jens@10
|
117 |
stone.owner = [self.players objectAtIndex: index];
|
jens@10
|
118 |
return [stone autorelease];
|
jens@10
|
119 |
}
|
jens@0
|
120 |
|
jens@3
|
121 |
- (Bit*) bitToPlaceInHolder: (id<BitHolder>)holder
|
jens@3
|
122 |
{
|
jens@3
|
123 |
if( holder.bit != nil || ! [holder isKindOfClass: [GoSquare class]] )
|
jens@3
|
124 |
return nil;
|
jens@10
|
125 |
else
|
jens@10
|
126 |
return [self pieceForPlayer: self.currentPlayer.index];
|
jens@3
|
127 |
}
|
jens@3
|
128 |
|
jens@3
|
129 |
|
jens@7
|
130 |
- (BOOL) canBit: (Bit*)bit moveFrom: (id<BitHolder>)srcHolder
|
jens@7
|
131 |
{
|
jens@7
|
132 |
return (srcHolder==nil);
|
jens@7
|
133 |
}
|
jens@7
|
134 |
|
jens@7
|
135 |
|
jens@0
|
136 |
- (BOOL) canBit: (Bit*)bit moveFrom: (id<BitHolder>)srcHolder to: (id<BitHolder>)dstHolder
|
jens@0
|
137 |
{
|
jens@7
|
138 |
if( srcHolder!=nil || ! [dstHolder isKindOfClass: [Square class]] )
|
jens@6
|
139 |
return NO;
|
jens@0
|
140 |
Square *dst=(Square*)dstHolder;
|
jens@0
|
141 |
|
jens@0
|
142 |
// There should be a check here for a "ko" (repeated position) ... exercise for the reader!
|
jens@0
|
143 |
|
jens@0
|
144 |
// Check for suicidal move. First an easy check for an empty adjacent space:
|
jens@0
|
145 |
NSArray *neighbors = dst.neighbors;
|
jens@0
|
146 |
for( GridCell *c in neighbors )
|
jens@0
|
147 |
if( c.empty )
|
jens@0
|
148 |
return YES; // there's an empty space
|
jens@0
|
149 |
// If the piece is surrounded, check the neighboring groups' liberties:
|
jens@0
|
150 |
for( GridCell *c in neighbors ) {
|
jens@0
|
151 |
int nLiberties;
|
jens@0
|
152 |
[c getGroup: &nLiberties];
|
jens@0
|
153 |
if( c.bit.unfriendly ) {
|
jens@0
|
154 |
if( nLiberties <= 1 )
|
jens@0
|
155 |
return YES; // the move captures, so it's not suicidal
|
jens@0
|
156 |
} else {
|
jens@0
|
157 |
if( nLiberties > 1 )
|
jens@0
|
158 |
return YES; // the stone joins a group with other liberties
|
jens@0
|
159 |
}
|
jens@0
|
160 |
}
|
jens@0
|
161 |
return NO;
|
jens@0
|
162 |
}
|
jens@0
|
163 |
|
jens@0
|
164 |
|
jens@0
|
165 |
- (void) bit: (Bit*)bit movedFrom: (id<BitHolder>)srcHolder to: (id<BitHolder>)dstHolder
|
jens@0
|
166 |
{
|
jens@0
|
167 |
Square *dst=(Square*)dstHolder;
|
jens@10
|
168 |
int curIndex = self.currentPlayer.index;
|
jens@0
|
169 |
// Check for captured enemy groups:
|
jens@0
|
170 |
BOOL captured = NO;
|
jens@0
|
171 |
for( GridCell *c in dst.neighbors )
|
jens@0
|
172 |
if( c.bit.unfriendly ) {
|
jens@0
|
173 |
int nLiberties;
|
jens@0
|
174 |
NSSet *group = [c getGroup: &nLiberties];
|
jens@0
|
175 |
if( nLiberties == 0 ) {
|
jens@0
|
176 |
captured = YES;
|
jens@0
|
177 |
for( GridCell *capture in group )
|
jens@0
|
178 |
[_captured[curIndex] addBit: capture.bit]; // Moves piece to POW camp!
|
jens@0
|
179 |
}
|
jens@0
|
180 |
}
|
jens@0
|
181 |
if( captured )
|
jens@1
|
182 |
PlaySound(@"Pop");
|
jens@10
|
183 |
|
jens@10
|
184 |
[self.currentTurn addToMove: dst.name];
|
jens@10
|
185 |
[self endTurn];
|
jens@0
|
186 |
}
|
jens@0
|
187 |
|
jens@0
|
188 |
|
jens@0
|
189 |
// This sample code makes no attempt to detect the end of the game, or count score,
|
jens@0
|
190 |
// both of which are rather complex to decide in Go.
|
jens@0
|
191 |
|
jens@0
|
192 |
|
jens@10
|
193 |
#pragma mark -
|
jens@10
|
194 |
#pragma mark STATE:
|
jens@10
|
195 |
|
jens@10
|
196 |
|
jens@10
|
197 |
- (NSString*) stateString
|
jens@10
|
198 |
{
|
jens@16
|
199 |
int n = _board.rows;
|
jens@10
|
200 |
unichar state[n*n];
|
jens@10
|
201 |
for( int y=0; y<n; y++ )
|
jens@10
|
202 |
for( int x=0; x<n; x++ ) {
|
jens@16
|
203 |
Bit *bit = [_board cellAtRow: y column: x].bit;
|
jens@10
|
204 |
unichar ch;
|
jens@10
|
205 |
if( bit==nil )
|
jens@10
|
206 |
ch = '-';
|
jens@10
|
207 |
else
|
jens@10
|
208 |
ch = '1' + bit.owner.index;
|
jens@10
|
209 |
state[y*n+x] = ch;
|
jens@10
|
210 |
}
|
jens@21
|
211 |
NSMutableString *stateString = [NSMutableString stringWithCharacters: state length: n*n];
|
jens@21
|
212 |
|
jens@21
|
213 |
NSUInteger cap0=_captured[0].numberOfBits, cap1=_captured[1].numberOfBits;
|
jens@21
|
214 |
if( cap0 || cap1 )
|
jens@21
|
215 |
[stateString appendFormat: @",%i,%i", cap0,cap1];
|
jens@21
|
216 |
return stateString;
|
jens@10
|
217 |
}
|
jens@10
|
218 |
|
jens@10
|
219 |
- (void) setStateString: (NSString*)state
|
jens@10
|
220 |
{
|
jens@21
|
221 |
//NSLog(@"Go: setStateString: '%@'",state);
|
jens@21
|
222 |
NSArray *components = [state componentsSeparatedByString: @","];
|
jens@21
|
223 |
state = [components objectAtIndex: 0];
|
jens@16
|
224 |
int n = _board.rows;
|
jens@10
|
225 |
for( int y=0; y<n; y++ )
|
jens@10
|
226 |
for( int x=0; x<n; x++ ) {
|
jens@10
|
227 |
int i = y*n+x;
|
jens@10
|
228 |
Piece *piece = nil;
|
jens@10
|
229 |
if( i < state.length ) {
|
jens@10
|
230 |
int index = [state characterAtIndex: i] - '1';
|
jens@10
|
231 |
if( index==0 || index==1 )
|
jens@10
|
232 |
piece = [self pieceForPlayer: index];
|
jens@10
|
233 |
}
|
jens@16
|
234 |
[_board cellAtRow: y column: x].bit = piece;
|
jens@10
|
235 |
}
|
jens@21
|
236 |
|
jens@21
|
237 |
if( components.count < 3 )
|
jens@21
|
238 |
components = nil;
|
jens@21
|
239 |
for( int player=0; player<=1; player++ ) {
|
jens@21
|
240 |
NSUInteger nCaptured = [[components objectAtIndex: 1+player] intValue];
|
jens@21
|
241 |
NSUInteger curNCaptured = _captured[player].numberOfBits;
|
jens@21
|
242 |
if( nCaptured < curNCaptured )
|
jens@21
|
243 |
_captured[player].numberOfBits = nCaptured;
|
jens@21
|
244 |
else
|
jens@21
|
245 |
for( int i=curNCaptured; i<nCaptured; i++ )
|
jens@21
|
246 |
[_captured[player] addBit: [self pieceForPlayer: 1-player]];
|
jens@21
|
247 |
}
|
jens@10
|
248 |
}
|
jens@10
|
249 |
|
jens@10
|
250 |
|
jens@10
|
251 |
- (BOOL) applyMoveString: (NSString*)move
|
jens@10
|
252 |
{
|
jens@21
|
253 |
//NSLog(@"Go: applyMoveString: '%@'",move);
|
jens@16
|
254 |
return [self animatePlacementIn: [_board cellWithName: move]];
|
jens@10
|
255 |
}
|
jens@10
|
256 |
|
jens@10
|
257 |
|
jens@0
|
258 |
@end
|
jens@10
|
259 |
|
jens@10
|
260 |
|
jens@10
|
261 |
@implementation Go9Game
|
jens@10
|
262 |
+ (NSString*) displayName {return @"Go (9x9)";}
|
jens@10
|
263 |
+ (int) dimensions {return 9;}
|
jens@10
|
264 |
@end
|
jens@10
|
265 |
|
jens@10
|
266 |
|
jens@10
|
267 |
@implementation Go13Game
|
jens@10
|
268 |
+ (NSString*) displayName {return @"Go (13x13)";}
|
jens@10
|
269 |
+ (int) dimensions {return 13;}
|
jens@10
|
270 |
@end
|