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 "CheckersGame.h"
|
jens@0
|
24 |
#import "Grid.h"
|
jens@0
|
25 |
#import "Piece.h"
|
jens@0
|
26 |
#import "QuartzUtils.h"
|
jens@1
|
27 |
#import "GGBUtils.h"
|
jens@0
|
28 |
|
jens@0
|
29 |
|
jens@0
|
30 |
@implementation CheckersGame
|
jens@0
|
31 |
|
jens@0
|
32 |
|
jens@11
|
33 |
static NSMutableDictionary *kPieceStyle1, *kPieceStyle2;
|
jens@11
|
34 |
|
jens@11
|
35 |
+ (void) initialize
|
jens@11
|
36 |
{
|
jens@11
|
37 |
if( self == [CheckersGame class] ) {
|
jens@11
|
38 |
kPieceStyle1 = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
|
jens@11
|
39 |
(id)GetCGImageNamed(@"Green.png"), @"contents",
|
jens@11
|
40 |
kCAGravityResizeAspect, @"contentsGravity",
|
jens@11
|
41 |
kCAFilterLinear, @"minificationFilter",
|
jens@11
|
42 |
nil];
|
jens@11
|
43 |
kPieceStyle2 = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
|
jens@11
|
44 |
(id)GetCGImageNamed(@"Red.png"), @"contents",
|
jens@11
|
45 |
kCAGravityResizeAspect, @"contentsGravity",
|
jens@11
|
46 |
kCAFilterLinear, @"minificationFilter",
|
jens@11
|
47 |
nil];
|
jens@11
|
48 |
}
|
jens@11
|
49 |
}
|
jens@11
|
50 |
|
jens@11
|
51 |
|
jens@10
|
52 |
- (id) init
|
jens@10
|
53 |
{
|
jens@10
|
54 |
self = [super init];
|
jens@10
|
55 |
if (self != nil) {
|
jens@10
|
56 |
[self setNumberOfPlayers: 2];
|
jens@10
|
57 |
|
jens@10
|
58 |
PreloadSound(@"Tink");
|
jens@10
|
59 |
PreloadSound(@"Funk");
|
jens@10
|
60 |
PreloadSound(@"Blow");
|
jens@10
|
61 |
PreloadSound(@"Pop");
|
jens@10
|
62 |
}
|
jens@10
|
63 |
return self;
|
jens@10
|
64 |
}
|
jens@10
|
65 |
|
jens@10
|
66 |
- (CGImageRef) iconForPlayer: (int)playerNum
|
jens@10
|
67 |
{
|
jens@10
|
68 |
return GetCGImageNamed( playerNum==0 ?@"Green.png" :@"Red.png" );
|
jens@10
|
69 |
}
|
jens@10
|
70 |
|
jens@7
|
71 |
- (Piece*) pieceForPlayer: (int)playerNum
|
jens@0
|
72 |
{
|
jens@11
|
73 |
Piece *p = [[Piece alloc] init];
|
jens@11
|
74 |
p.bounds = CGRectMake(0,0,floor(_grid.spacing.width),floor(_grid.spacing.height));
|
jens@11
|
75 |
p.style = (playerNum ?kPieceStyle2 :kPieceStyle1);
|
jens@7
|
76 |
p.owner = [self.players objectAtIndex: playerNum];
|
jens@7
|
77 |
p.name = playerNum ?@"2" :@"1";
|
jens@7
|
78 |
return [p autorelease];
|
jens@0
|
79 |
}
|
jens@0
|
80 |
|
jens@10
|
81 |
- (void) makeKing: (Piece*)piece
|
jens@10
|
82 |
{
|
jens@10
|
83 |
piece.scale = 1.4;
|
jens@12
|
84 |
piece.tag = YES; // tag property stores the 'king' flag
|
jens@10
|
85 |
piece.name = piece.owner.index ?@"4" :@"3";
|
jens@10
|
86 |
}
|
jens@10
|
87 |
|
jens@10
|
88 |
- (void) setUpBoard
|
jens@0
|
89 |
{
|
jens@11
|
90 |
RectGrid *grid = [[RectGrid alloc] initWithRows: 8 columns: 8 frame: _board.bounds];
|
jens@7
|
91 |
_grid = grid;
|
jens@10
|
92 |
[_board addSublayer: _grid];
|
jens@7
|
93 |
CGPoint pos = _grid.position;
|
jens@0
|
94 |
pos.x = floor((_board.bounds.size.width-grid.frame.size.width)/2);
|
jens@0
|
95 |
grid.position = pos;
|
jens@0
|
96 |
grid.allowsMoves = YES;
|
jens@0
|
97 |
grid.allowsCaptures = NO;
|
jens@1
|
98 |
grid.cellColor = CreateGray(0.0, 0.25);
|
jens@1
|
99 |
grid.altCellColor = CreateGray(1.0, 0.25);
|
jens@0
|
100 |
grid.lineColor = nil;
|
jens@7
|
101 |
|
jens@7
|
102 |
for( int i=0; i<32; i++ ) {
|
jens@7
|
103 |
int row = i/4;
|
jens@12
|
104 |
[_grid addCellAtRow: row column: 2*(i%4) + (row&1)];
|
jens@7
|
105 |
}
|
jens@12
|
106 |
[_grid release]; // its superlayer still retains it
|
jens@0
|
107 |
}
|
jens@0
|
108 |
|
jens@12
|
109 |
- (NSString*) initialStateString {return @"111111111111--------222222222222";}
|
jens@12
|
110 |
- (NSString*) stateString {return _grid.stateString;}
|
jens@12
|
111 |
- (void) setStateString: (NSString*)state {_grid.stateString = state;}
|
jens@12
|
112 |
|
jens@12
|
113 |
- (Piece*) makePieceNamed: (NSString*)name
|
jens@7
|
114 |
{
|
jens@12
|
115 |
int which = [name characterAtIndex: 0] - '1';
|
jens@12
|
116 |
if( which >=0 && which < 4 ) {
|
jens@12
|
117 |
Piece *piece = [self pieceForPlayer: (which & 1)];
|
jens@12
|
118 |
if( which & 2 )
|
jens@12
|
119 |
[self makeKing: piece];
|
jens@12
|
120 |
return piece;
|
jens@12
|
121 |
} else
|
jens@12
|
122 |
return nil;
|
jens@7
|
123 |
}
|
jens@7
|
124 |
|
jens@0
|
125 |
|
jens@0
|
126 |
- (BOOL) canBit: (Bit*)bit moveFrom: (id<BitHolder>)srcHolder to: (id<BitHolder>)dstHolder
|
jens@0
|
127 |
{
|
jens@0
|
128 |
Square *src=(Square*)srcHolder, *dst=(Square*)dstHolder;
|
jens@12
|
129 |
if( bit.tag )
|
jens@0
|
130 |
if( dst==src.bl || dst==src.br || dst==src.l || dst==src.r
|
jens@0
|
131 |
|| (src.bl.bit.unfriendly && dst==src.bl.bl) || (src.br.bit.unfriendly && dst==src.br.br) )
|
jens@0
|
132 |
return YES;
|
jens@0
|
133 |
return dst==src.fl || dst==src.fr
|
jens@0
|
134 |
|| (src.fl.bit.unfriendly && dst==src.fl.fl) || (src.fr.bit.unfriendly && dst==src.fr.fr);
|
jens@0
|
135 |
}
|
jens@0
|
136 |
|
jens@0
|
137 |
- (void) bit: (Bit*)bit movedFrom: (id<BitHolder>)srcHolder to: (id<BitHolder>)dstHolder
|
jens@0
|
138 |
{
|
jens@0
|
139 |
Square *src=(Square*)srcHolder, *dst=(Square*)dstHolder;
|
jens@0
|
140 |
int playerIndex = self.currentPlayer.index;
|
jens@7
|
141 |
|
jens@10
|
142 |
Turn *turn = self.currentTurn;
|
jens@10
|
143 |
if( turn.move.length==0 )
|
jens@10
|
144 |
[turn addToMove: src.name];
|
jens@10
|
145 |
[turn addToMove: @"-"];
|
jens@10
|
146 |
[turn addToMove: dst.name];
|
jens@7
|
147 |
|
jens@12
|
148 |
BOOL isKing = bit.tag;
|
jens@1
|
149 |
PlaySound(isKing ?@"Funk" :@"Tink");
|
jens@0
|
150 |
|
jens@0
|
151 |
// "King" a piece that made it to the last row:
|
jens@12
|
152 |
if( !isKing && (dst.row == (playerIndex ?0 :7)) ) {
|
jens@12
|
153 |
PlaySound(@"Blow");
|
jens@12
|
154 |
[self makeKing: (Piece*)bit];
|
jens@12
|
155 |
[turn addToMove: @"*"];
|
jens@12
|
156 |
// don't set isKing flag - piece can't jump again after being kinged.
|
jens@12
|
157 |
}
|
jens@0
|
158 |
|
jens@0
|
159 |
// Check for a capture:
|
jens@12
|
160 |
NSArray *line = [src lineToCell: dst inclusive: NO];
|
jens@12
|
161 |
if( line.count==1 ) {
|
jens@12
|
162 |
Square *capture = [line objectAtIndex: 0];
|
jens@10
|
163 |
[capture destroyBit];
|
jens@10
|
164 |
[turn addToMove: @"!"];
|
jens@12
|
165 |
PlaySound(@"Pop");
|
jens@0
|
166 |
|
jens@0
|
167 |
// Now check if another capture is possible. If so, don't end the turn:
|
jens@0
|
168 |
if( (dst.fl.bit.unfriendly && dst.fl.fl.empty) || (dst.fr.bit.unfriendly && dst.fr.fr.empty) )
|
jens@0
|
169 |
return;
|
jens@0
|
170 |
if( isKing )
|
jens@0
|
171 |
if( (dst.bl.bit.unfriendly && dst.bl.bl.empty) || (dst.br.bit.unfriendly && dst.br.br.empty) )
|
jens@0
|
172 |
return;
|
jens@0
|
173 |
}
|
jens@0
|
174 |
|
jens@0
|
175 |
[self endTurn];
|
jens@0
|
176 |
}
|
jens@0
|
177 |
|
jens@0
|
178 |
- (Player*) checkForWinner
|
jens@0
|
179 |
{
|
jens@12
|
180 |
NSCountedSet *remaining = _grid.countPiecesByPlayer;
|
jens@12
|
181 |
if( remaining.count==1 )
|
jens@12
|
182 |
return [remaining anyObject];
|
jens@0
|
183 |
else
|
jens@0
|
184 |
return nil;
|
jens@0
|
185 |
}
|
jens@0
|
186 |
|
jens@0
|
187 |
|
jens@7
|
188 |
- (BOOL) applyMoveString: (NSString*)move
|
jens@7
|
189 |
{
|
jens@7
|
190 |
GridCell *src = nil;
|
jens@10
|
191 |
for( NSString *ident in [move componentsSeparatedByString: @"-"] ) {
|
jens@10
|
192 |
while( [ident hasSuffix: @"!"] || [ident hasSuffix: @"*"] )
|
jens@10
|
193 |
ident = [ident substringToIndex: ident.length-1];
|
jens@7
|
194 |
GridCell *dst = [_grid cellWithName: ident];
|
jens@10
|
195 |
if( dst == nil )
|
jens@10
|
196 |
return NO;
|
jens@10
|
197 |
if( src && ! [self animateMoveFrom: src to: dst] )
|
jens@10
|
198 |
return NO;
|
jens@7
|
199 |
src = dst;
|
jens@7
|
200 |
}
|
jens@7
|
201 |
return YES;
|
jens@7
|
202 |
}
|
jens@7
|
203 |
|
jens@7
|
204 |
|
jens@0
|
205 |
@end
|