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@3
|
42 |
[(Player*)[_players objectAtIndex: 0] setName: @"Red"];
|
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@16
|
51 |
CGSize size = _table.bounds.size;
|
jens@10
|
52 |
CGFloat boardSide = MIN(size.width,size.height);
|
jens@16
|
53 |
RectGrid *board = [[RectGrid alloc] initWithRows: dimensions columns: dimensions
|
jens@10
|
54 |
frame: CGRectMake(floor((size.width-boardSide)/2),
|
jens@10
|
55 |
floor((size.height-boardSide)/2),
|
jens@10
|
56 |
boardSide,boardSide)];
|
jens@16
|
57 |
_board = board;
|
jens@10
|
58 |
/*
|
jens@10
|
59 |
grid.backgroundColor = GetCGPatternNamed(@"Wood.jpg");
|
jens@10
|
60 |
grid.borderColor = kTranslucentLightGrayColor;
|
jens@10
|
61 |
grid.borderWidth = 2;
|
jens@10
|
62 |
*/
|
jens@16
|
63 |
board.lineColor = kTranslucentGrayColor;
|
jens@16
|
64 |
board.cellClass = [GoSquare class];
|
jens@16
|
65 |
[board addAllCells];
|
jens@16
|
66 |
((GoSquare*)[board cellAtRow: 2 column: 2]).dotted = YES;
|
jens@16
|
67 |
((GoSquare*)[board cellAtRow: 6 column: 6]).dotted = YES;
|
jens@16
|
68 |
((GoSquare*)[board cellAtRow: 2 column: 6]).dotted = YES;
|
jens@16
|
69 |
((GoSquare*)[board cellAtRow: 6 column: 2]).dotted = YES;
|
jens@16
|
70 |
board.usesDiagonals = board.allowsMoves = board.allowsCaptures = NO;
|
jens@16
|
71 |
[_table addSublayer: board];
|
jens@16
|
72 |
[board release];
|
jens@10
|
73 |
|
jens@16
|
74 |
CGRect gridFrame = board.frame;
|
jens@16
|
75 |
CGFloat pieceSize = (int)board.spacing.width & ~1; // make sure it's even
|
jens@10
|
76 |
CGFloat captureHeight = gridFrame.size.height-4*pieceSize;
|
jens@10
|
77 |
_captured[0] = [[Stack alloc] initWithStartPos: CGPointMake(2*pieceSize,0)
|
jens@10
|
78 |
spacing: CGSizeMake(0,pieceSize)
|
jens@10
|
79 |
wrapInterval: floor(captureHeight/pieceSize)
|
jens@10
|
80 |
wrapSpacing: CGSizeMake(-pieceSize,0)];
|
jens@10
|
81 |
_captured[0].frame = CGRectMake(CGRectGetMinX(gridFrame)-3*pieceSize,
|
jens@10
|
82 |
CGRectGetMinY(gridFrame)+3*pieceSize,
|
jens@10
|
83 |
2*pieceSize, captureHeight);
|
jens@10
|
84 |
_captured[0].zPosition = kPieceZ+1;
|
jens@16
|
85 |
[_table addSublayer: _captured[0]];
|
jens@10
|
86 |
[_captured[0] release];
|
jens@10
|
87 |
|
jens@10
|
88 |
_captured[1] = [[Stack alloc] initWithStartPos: CGPointMake(0,captureHeight)
|
jens@10
|
89 |
spacing: CGSizeMake(0,-pieceSize)
|
jens@10
|
90 |
wrapInterval: floor(captureHeight/pieceSize)
|
jens@10
|
91 |
wrapSpacing: CGSizeMake(pieceSize,0)];
|
jens@10
|
92 |
_captured[1].frame = CGRectMake(CGRectGetMaxX(gridFrame)+pieceSize,
|
jens@10
|
93 |
CGRectGetMinY(gridFrame)+pieceSize,
|
jens@10
|
94 |
2*pieceSize, captureHeight);
|
jens@10
|
95 |
_captured[1].zPosition = kPieceZ+1;
|
jens@16
|
96 |
[_table addSublayer: _captured[1]];
|
jens@10
|
97 |
[_captured[1] release];
|
jens@0
|
98 |
|
jens@10
|
99 |
PreloadSound(@"Pop");
|
jens@10
|
100 |
}
|
jens@10
|
101 |
|
jens@10
|
102 |
- (CGImageRef) iconForPlayer: (int)playerNum
|
jens@10
|
103 |
{
|
jens@10
|
104 |
return GetCGImageNamed( playerNum ?@"bot086.png" :@"bot089.png" );
|
jens@10
|
105 |
}
|
jens@10
|
106 |
|
jens@10
|
107 |
- (Piece*) pieceForPlayer: (int)index
|
jens@10
|
108 |
{
|
jens@10
|
109 |
NSString *imageName = index ?@"bot086.png" :@"bot089.png";
|
jens@16
|
110 |
CGFloat pieceSize = (int)(_board.spacing.width * 0.9) & ~1; // make sure it's even
|
jens@10
|
111 |
Piece *stone = [[Piece alloc] initWithImageNamed: imageName scale: pieceSize];
|
jens@10
|
112 |
stone.owner = [self.players objectAtIndex: index];
|
jens@10
|
113 |
return [stone autorelease];
|
jens@10
|
114 |
}
|
jens@0
|
115 |
|
jens@3
|
116 |
- (Bit*) bitToPlaceInHolder: (id<BitHolder>)holder
|
jens@3
|
117 |
{
|
jens@3
|
118 |
if( holder.bit != nil || ! [holder isKindOfClass: [GoSquare class]] )
|
jens@3
|
119 |
return nil;
|
jens@10
|
120 |
else
|
jens@10
|
121 |
return [self pieceForPlayer: self.currentPlayer.index];
|
jens@3
|
122 |
}
|
jens@3
|
123 |
|
jens@3
|
124 |
|
jens@7
|
125 |
- (BOOL) canBit: (Bit*)bit moveFrom: (id<BitHolder>)srcHolder
|
jens@7
|
126 |
{
|
jens@7
|
127 |
return (srcHolder==nil);
|
jens@7
|
128 |
}
|
jens@7
|
129 |
|
jens@7
|
130 |
|
jens@0
|
131 |
- (BOOL) canBit: (Bit*)bit moveFrom: (id<BitHolder>)srcHolder to: (id<BitHolder>)dstHolder
|
jens@0
|
132 |
{
|
jens@7
|
133 |
if( srcHolder!=nil || ! [dstHolder isKindOfClass: [Square class]] )
|
jens@6
|
134 |
return NO;
|
jens@0
|
135 |
Square *dst=(Square*)dstHolder;
|
jens@0
|
136 |
|
jens@0
|
137 |
// There should be a check here for a "ko" (repeated position) ... exercise for the reader!
|
jens@0
|
138 |
|
jens@0
|
139 |
// Check for suicidal move. First an easy check for an empty adjacent space:
|
jens@0
|
140 |
NSArray *neighbors = dst.neighbors;
|
jens@0
|
141 |
for( GridCell *c in neighbors )
|
jens@0
|
142 |
if( c.empty )
|
jens@0
|
143 |
return YES; // there's an empty space
|
jens@0
|
144 |
// If the piece is surrounded, check the neighboring groups' liberties:
|
jens@0
|
145 |
for( GridCell *c in neighbors ) {
|
jens@0
|
146 |
int nLiberties;
|
jens@0
|
147 |
[c getGroup: &nLiberties];
|
jens@0
|
148 |
if( c.bit.unfriendly ) {
|
jens@0
|
149 |
if( nLiberties <= 1 )
|
jens@0
|
150 |
return YES; // the move captures, so it's not suicidal
|
jens@0
|
151 |
} else {
|
jens@0
|
152 |
if( nLiberties > 1 )
|
jens@0
|
153 |
return YES; // the stone joins a group with other liberties
|
jens@0
|
154 |
}
|
jens@0
|
155 |
}
|
jens@0
|
156 |
return NO;
|
jens@0
|
157 |
}
|
jens@0
|
158 |
|
jens@0
|
159 |
|
jens@0
|
160 |
- (void) bit: (Bit*)bit movedFrom: (id<BitHolder>)srcHolder to: (id<BitHolder>)dstHolder
|
jens@0
|
161 |
{
|
jens@0
|
162 |
Square *dst=(Square*)dstHolder;
|
jens@10
|
163 |
int curIndex = self.currentPlayer.index;
|
jens@0
|
164 |
// Check for captured enemy groups:
|
jens@0
|
165 |
BOOL captured = NO;
|
jens@0
|
166 |
for( GridCell *c in dst.neighbors )
|
jens@0
|
167 |
if( c.bit.unfriendly ) {
|
jens@0
|
168 |
int nLiberties;
|
jens@0
|
169 |
NSSet *group = [c getGroup: &nLiberties];
|
jens@0
|
170 |
if( nLiberties == 0 ) {
|
jens@0
|
171 |
captured = YES;
|
jens@0
|
172 |
for( GridCell *capture in group )
|
jens@0
|
173 |
[_captured[curIndex] addBit: capture.bit]; // Moves piece to POW camp!
|
jens@0
|
174 |
}
|
jens@0
|
175 |
}
|
jens@0
|
176 |
if( captured )
|
jens@1
|
177 |
PlaySound(@"Pop");
|
jens@10
|
178 |
|
jens@10
|
179 |
[self.currentTurn addToMove: dst.name];
|
jens@10
|
180 |
[self endTurn];
|
jens@0
|
181 |
}
|
jens@0
|
182 |
|
jens@0
|
183 |
|
jens@0
|
184 |
// This sample code makes no attempt to detect the end of the game, or count score,
|
jens@0
|
185 |
// both of which are rather complex to decide in Go.
|
jens@0
|
186 |
|
jens@0
|
187 |
|
jens@10
|
188 |
#pragma mark -
|
jens@10
|
189 |
#pragma mark STATE:
|
jens@10
|
190 |
|
jens@10
|
191 |
|
jens@10
|
192 |
- (NSString*) stateString
|
jens@10
|
193 |
{
|
jens@16
|
194 |
int n = _board.rows;
|
jens@10
|
195 |
unichar state[n*n];
|
jens@10
|
196 |
for( int y=0; y<n; y++ )
|
jens@10
|
197 |
for( int x=0; x<n; x++ ) {
|
jens@16
|
198 |
Bit *bit = [_board cellAtRow: y column: x].bit;
|
jens@10
|
199 |
unichar ch;
|
jens@10
|
200 |
if( bit==nil )
|
jens@10
|
201 |
ch = '-';
|
jens@10
|
202 |
else
|
jens@10
|
203 |
ch = '1' + bit.owner.index;
|
jens@10
|
204 |
state[y*n+x] = ch;
|
jens@10
|
205 |
}
|
jens@10
|
206 |
return [NSString stringWithCharacters: state length: n*n];
|
jens@10
|
207 |
}
|
jens@10
|
208 |
|
jens@10
|
209 |
- (void) setStateString: (NSString*)state
|
jens@10
|
210 |
{
|
jens@10
|
211 |
NSLog(@"Go: setStateString: '%@'",state);
|
jens@16
|
212 |
int n = _board.rows;
|
jens@10
|
213 |
for( int y=0; y<n; y++ )
|
jens@10
|
214 |
for( int x=0; x<n; x++ ) {
|
jens@10
|
215 |
int i = y*n+x;
|
jens@10
|
216 |
Piece *piece = nil;
|
jens@10
|
217 |
if( i < state.length ) {
|
jens@10
|
218 |
int index = [state characterAtIndex: i] - '1';
|
jens@10
|
219 |
if( index==0 || index==1 )
|
jens@10
|
220 |
piece = [self pieceForPlayer: index];
|
jens@10
|
221 |
}
|
jens@16
|
222 |
[_board cellAtRow: y column: x].bit = piece;
|
jens@10
|
223 |
}
|
jens@10
|
224 |
}
|
jens@10
|
225 |
|
jens@10
|
226 |
|
jens@10
|
227 |
- (BOOL) applyMoveString: (NSString*)move
|
jens@10
|
228 |
{
|
jens@10
|
229 |
NSLog(@"Go: applyMoveString: '%@'",move);
|
jens@16
|
230 |
return [self animatePlacementIn: [_board cellWithName: move]];
|
jens@10
|
231 |
}
|
jens@10
|
232 |
|
jens@10
|
233 |
|
jens@0
|
234 |
@end
|
jens@10
|
235 |
|
jens@10
|
236 |
|
jens@10
|
237 |
@implementation Go9Game
|
jens@10
|
238 |
+ (NSString*) displayName {return @"Go (9x9)";}
|
jens@10
|
239 |
+ (int) dimensions {return 9;}
|
jens@10
|
240 |
@end
|
jens@10
|
241 |
|
jens@10
|
242 |
|
jens@10
|
243 |
@implementation Go13Game
|
jens@10
|
244 |
+ (NSString*) displayName {return @"Go (13x13)";}
|
jens@10
|
245 |
+ (int) dimensions {return 13;}
|
jens@10
|
246 |
@end
|