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 "Game.h"
|
jens@0
|
24 |
#import "Bit.h"
|
jens@0
|
25 |
|
jens@0
|
26 |
|
jens@0
|
27 |
@interface Game ()
|
jens@0
|
28 |
@property (copy) NSArray *players;
|
jens@0
|
29 |
@property (assign) Player *currentPlayer, *winner;
|
jens@0
|
30 |
@end
|
jens@0
|
31 |
|
jens@0
|
32 |
|
jens@0
|
33 |
/** WARNING: THIS CODE REQUIRES GARBAGE COLLECTION!
|
jens@0
|
34 |
** This sample application uses Objective-C 2.0 garbage collection.
|
jens@0
|
35 |
** Therefore, the source code in this file does NOT perform manual object memory management.
|
jens@0
|
36 |
** If you reuse any of this code in a process that isn't garbage collected, you will need to
|
jens@0
|
37 |
** add all necessary retain/release/autorelease calls, and implement -dealloc methods,
|
jens@0
|
38 |
** otherwise unpleasant leakage will occur!
|
jens@0
|
39 |
**/
|
jens@0
|
40 |
|
jens@0
|
41 |
|
jens@0
|
42 |
@implementation Game
|
jens@0
|
43 |
|
jens@0
|
44 |
|
jens@0
|
45 |
+ (NSString*) displayName
|
jens@0
|
46 |
{
|
jens@0
|
47 |
NSString* name = [self description];
|
jens@0
|
48 |
if( [name hasSuffix: @"Game"] )
|
jens@0
|
49 |
name = [name substringToIndex: name.length-4];
|
jens@0
|
50 |
return name;
|
jens@0
|
51 |
}
|
jens@0
|
52 |
|
jens@0
|
53 |
|
jens@1
|
54 |
- (id) initWithBoard: (GGBLayer*)board
|
jens@0
|
55 |
{
|
jens@0
|
56 |
self = [super init];
|
jens@0
|
57 |
if (self != nil) {
|
jens@0
|
58 |
_board = [board retain];
|
jens@0
|
59 |
// Store a pointer to myself as the value of the "Game" property
|
jens@0
|
60 |
// of my root layer. (CALayers can have arbitrary KV properties stored into them.)
|
jens@0
|
61 |
// This is used by the -[CALayer game] category method defined below, to find the Game.
|
jens@0
|
62 |
[board setValue: self forKey: @"Game"];
|
jens@0
|
63 |
}
|
jens@0
|
64 |
return self;
|
jens@0
|
65 |
}
|
jens@0
|
66 |
|
jens@0
|
67 |
|
jens@0
|
68 |
- (void) dealloc
|
jens@0
|
69 |
{
|
jens@0
|
70 |
[_board release];
|
jens@0
|
71 |
[_players release];
|
jens@0
|
72 |
[super dealloc];
|
jens@0
|
73 |
}
|
jens@0
|
74 |
|
jens@0
|
75 |
|
jens@0
|
76 |
@synthesize players=_players, currentPlayer=_currentPlayer, winner=_winner;
|
jens@0
|
77 |
|
jens@0
|
78 |
|
jens@0
|
79 |
- (void) setNumberOfPlayers: (unsigned)n
|
jens@0
|
80 |
{
|
jens@0
|
81 |
NSMutableArray *players = [NSMutableArray arrayWithCapacity: n];
|
jens@0
|
82 |
for( int i=1; i<=n; i++ ) {
|
jens@0
|
83 |
Player *player = [[Player alloc] initWithGame: self];
|
jens@0
|
84 |
player.name = [NSString stringWithFormat: @"Player %i",i];
|
jens@0
|
85 |
[players addObject: player];
|
jens@0
|
86 |
[player release];
|
jens@0
|
87 |
}
|
jens@0
|
88 |
self.winner = nil;
|
jens@0
|
89 |
self.currentPlayer = nil;
|
jens@0
|
90 |
self.players = players;
|
jens@0
|
91 |
}
|
jens@0
|
92 |
|
jens@0
|
93 |
|
jens@0
|
94 |
- (void) nextPlayer
|
jens@0
|
95 |
{
|
jens@0
|
96 |
if( ! _currentPlayer ) {
|
jens@0
|
97 |
NSLog(@"*** The %@ Begins! ***", self.class);
|
jens@0
|
98 |
self.currentPlayer = [_players objectAtIndex: 0];
|
jens@0
|
99 |
} else {
|
jens@0
|
100 |
self.currentPlayer = _currentPlayer.nextPlayer;
|
jens@0
|
101 |
}
|
jens@0
|
102 |
NSLog(@"Current player is %@",_currentPlayer);
|
jens@0
|
103 |
}
|
jens@0
|
104 |
|
jens@0
|
105 |
|
jens@0
|
106 |
- (void) endTurn
|
jens@0
|
107 |
{
|
jens@0
|
108 |
NSLog(@"--- End of turn");
|
jens@0
|
109 |
Player *winner = [self checkForWinner];
|
jens@0
|
110 |
if( winner ) {
|
jens@0
|
111 |
NSLog(@"*** The %@ Ends! The winner is %@ ! ***", self.class, winner);
|
jens@0
|
112 |
self.winner = winner;
|
jens@0
|
113 |
} else
|
jens@0
|
114 |
[self nextPlayer];
|
jens@0
|
115 |
}
|
jens@0
|
116 |
|
jens@0
|
117 |
|
jens@0
|
118 |
#pragma mark -
|
jens@0
|
119 |
#pragma mark GAMEPLAY METHODS TO BE OVERRIDDEN:
|
jens@0
|
120 |
|
jens@0
|
121 |
|
jens@0
|
122 |
- (BOOL) canBit: (Bit*)bit moveFrom: (id<BitHolder>)src
|
jens@0
|
123 |
{
|
jens@0
|
124 |
return YES;
|
jens@0
|
125 |
}
|
jens@0
|
126 |
|
jens@0
|
127 |
- (BOOL) canBit: (Bit*)bit moveFrom: (id<BitHolder>)src to: (id<BitHolder>)dst
|
jens@0
|
128 |
{
|
jens@0
|
129 |
return YES;
|
jens@0
|
130 |
}
|
jens@0
|
131 |
|
jens@0
|
132 |
- (void) bit: (Bit*)bit movedFrom: (id<BitHolder>)src to: (id<BitHolder>)dst
|
jens@0
|
133 |
{
|
jens@0
|
134 |
[self endTurn];
|
jens@0
|
135 |
}
|
jens@0
|
136 |
|
jens@0
|
137 |
- (BOOL) clickedBit: (Bit*)bit
|
jens@0
|
138 |
{
|
jens@0
|
139 |
return YES;
|
jens@0
|
140 |
}
|
jens@0
|
141 |
|
jens@0
|
142 |
- (Player*) checkForWinner
|
jens@0
|
143 |
{
|
jens@0
|
144 |
return nil;
|
jens@0
|
145 |
}
|
jens@0
|
146 |
|
jens@0
|
147 |
|
jens@0
|
148 |
@end
|
jens@0
|
149 |
|
jens@0
|
150 |
|
jens@0
|
151 |
|
jens@0
|
152 |
|
jens@0
|
153 |
@implementation Player
|
jens@0
|
154 |
|
jens@0
|
155 |
|
jens@0
|
156 |
- (id) initWithGame: (Game*)game
|
jens@0
|
157 |
{
|
jens@0
|
158 |
self = [super init];
|
jens@0
|
159 |
if (self != nil) {
|
jens@0
|
160 |
_game = game;
|
jens@0
|
161 |
}
|
jens@0
|
162 |
return self;
|
jens@0
|
163 |
}
|
jens@0
|
164 |
|
jens@0
|
165 |
|
jens@0
|
166 |
@synthesize game=_game, name=_name;
|
jens@0
|
167 |
|
jens@0
|
168 |
- (BOOL) isCurrent {return self == _game.currentPlayer;}
|
jens@0
|
169 |
- (BOOL) isFriendly {return self == _game.currentPlayer;} // could be overridden for games with partners
|
jens@0
|
170 |
- (BOOL) isUnfriendly {return ! self.friendly;}
|
jens@0
|
171 |
|
jens@0
|
172 |
- (int) index
|
jens@0
|
173 |
{
|
jens@0
|
174 |
return [_game.players indexOfObjectIdenticalTo: self];
|
jens@0
|
175 |
}
|
jens@0
|
176 |
|
jens@0
|
177 |
- (Player*) nextPlayer
|
jens@0
|
178 |
{
|
jens@0
|
179 |
return [_game.players objectAtIndex: (self.index+1) % _game.players.count];
|
jens@0
|
180 |
}
|
jens@0
|
181 |
|
jens@0
|
182 |
- (Player*) previousPlayer
|
jens@0
|
183 |
{
|
jens@0
|
184 |
return [_game.players objectAtIndex: (self.index-1) % _game.players.count];
|
jens@0
|
185 |
}
|
jens@0
|
186 |
|
jens@0
|
187 |
- (NSString*) description
|
jens@0
|
188 |
{
|
jens@0
|
189 |
return [NSString stringWithFormat: @"%@[%@]", self.class,self.name];
|
jens@0
|
190 |
}
|
jens@0
|
191 |
|
jens@0
|
192 |
@end
|
jens@0
|
193 |
|
jens@0
|
194 |
|
jens@0
|
195 |
|
jens@0
|
196 |
|
jens@0
|
197 |
@implementation CALayer (Game)
|
jens@0
|
198 |
|
jens@0
|
199 |
- (Game*) game
|
jens@0
|
200 |
{
|
jens@0
|
201 |
// The Game object stores a pointer to itself as the value of the "Game" property
|
jens@0
|
202 |
// of its root layer. (CALayers can have arbitrary KV properties stored into them.)
|
jens@0
|
203 |
for( CALayer *layer = self; layer; layer=layer.superlayer ) {
|
jens@0
|
204 |
Game *game = [layer valueForKey: @"Game"];
|
jens@0
|
205 |
if( game )
|
jens@0
|
206 |
return game;
|
jens@0
|
207 |
}
|
jens@0
|
208 |
NSAssert1(NO,@"Couldn't look up Game from %@",self);
|
jens@0
|
209 |
return nil;
|
jens@0
|
210 |
}
|
jens@0
|
211 |
|
jens@0
|
212 |
@end
|