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 @property (copy) NSArray *players;
29 @property (assign) Player *currentPlayer, *winner;
33 /** WARNING: THIS CODE REQUIRES GARBAGE COLLECTION!
34 ** This sample application uses Objective-C 2.0 garbage collection.
35 ** Therefore, the source code in this file does NOT perform manual object memory management.
36 ** If you reuse any of this code in a process that isn't garbage collected, you will need to
37 ** add all necessary retain/release/autorelease calls, and implement -dealloc methods,
38 ** otherwise unpleasant leakage will occur!
45 + (NSString*) displayName
47 NSString* name = [self description];
48 if( [name hasSuffix: @"Game"] )
49 name = [name substringToIndex: name.length-4];
54 - (id) initWithBoard: (GGBLayer*)board
58 _board = [board retain];
59 // Store a pointer to myself as the value of the "Game" property
60 // of my root layer. (CALayers can have arbitrary KV properties stored into them.)
61 // This is used by the -[CALayer game] category method defined below, to find the Game.
62 [board setValue: self forKey: @"Game"];
76 @synthesize players=_players, currentPlayer=_currentPlayer, winner=_winner;
79 - (void) setNumberOfPlayers: (unsigned)n
81 NSMutableArray *players = [NSMutableArray arrayWithCapacity: n];
82 for( int i=1; i<=n; i++ ) {
83 Player *player = [[Player alloc] initWithGame: self];
84 player.name = [NSString stringWithFormat: @"Player %i",i];
85 [players addObject: player];
89 self.currentPlayer = nil;
90 self.players = players;
96 if( ! _currentPlayer ) {
97 NSLog(@"*** The %@ Begins! ***", self.class);
98 self.currentPlayer = [_players objectAtIndex: 0];
100 self.currentPlayer = _currentPlayer.nextPlayer;
102 NSLog(@"Current player is %@",_currentPlayer);
108 NSLog(@"--- End of turn");
109 Player *winner = [self checkForWinner];
111 NSLog(@"*** The %@ Ends! The winner is %@ ! ***", self.class, winner);
112 self.winner = winner;
119 #pragma mark GAMEPLAY METHODS TO BE OVERRIDDEN:
122 - (BOOL) canBit: (Bit*)bit moveFrom: (id<BitHolder>)src
127 - (BOOL) canBit: (Bit*)bit moveFrom: (id<BitHolder>)src to: (id<BitHolder>)dst
132 - (void) bit: (Bit*)bit movedFrom: (id<BitHolder>)src to: (id<BitHolder>)dst
137 - (BOOL) clickedBit: (Bit*)bit
142 - (Player*) checkForWinner
153 @implementation Player
156 - (id) initWithGame: (Game*)game
166 @synthesize game=_game, name=_name;
168 - (BOOL) isCurrent {return self == _game.currentPlayer;}
169 - (BOOL) isFriendly {return self == _game.currentPlayer;} // could be overridden for games with partners
170 - (BOOL) isUnfriendly {return ! self.friendly;}
174 return [_game.players indexOfObjectIdenticalTo: self];
177 - (Player*) nextPlayer
179 return [_game.players objectAtIndex: (self.index+1) % _game.players.count];
182 - (Player*) previousPlayer
184 return [_game.players objectAtIndex: (self.index-1) % _game.players.count];
187 - (NSString*) description
189 return [NSString stringWithFormat: @"%@[%@]", self.class,self.name];
197 @implementation CALayer (Game)
201 // The Game object stores a pointer to itself as the value of the "Game" property
202 // of its root layer. (CALayers can have arbitrary KV properties stored into them.)
203 for( CALayer *layer = self; layer; layer=layer.superlayer ) {
204 Game *game = [layer valueForKey: @"Game"];
208 NSAssert1(NO,@"Couldn't look up Game from %@",self);