More tweaks, including a "reversed" property for Grids to show the second player's perspective without turning the pieces upside-down.
5 // Created by Jens Alfke on 7/3/08.
6 // Copyright 2008 Jens Alfke. All rights reserved.
14 @implementation Player
17 - (id) initWithGame: (Game*)game
27 - (id) initWithName: (NSString*)name
37 - (id) initWithCoder: (NSCoder*)decoder
41 _game = [decoder decodeObjectForKey: @"game"];
42 _name = [[decoder decodeObjectForKey: @"name"] copy];
43 _local= [decoder decodeBoolForKey: @"local"];
44 _extraValues = [[decoder decodeObjectForKey: @"extraValues"] mutableCopy];
49 - (void) encodeWithCoder: (NSCoder*)coder
51 [coder encodeObject: _game forKey: @"game"];
52 [coder encodeObject: _name forKey: @"name"];
53 [coder encodeBool: _local forKey: @"local"];
54 [coder encodeObject: _extraValues forKey: @"extraValues"];
60 [_extraValues release];
65 - (id)valueForUndefinedKey:(NSString *)key
67 return [_extraValues objectForKey: key];
70 - (void)setValue:(id)value forUndefinedKey:(NSString *)key
73 _extraValues = [[NSMutableDictionary alloc] init];
75 [_extraValues setObject: value forKey: key];
77 [_extraValues removeObjectForKey: key];
81 - (void) copyFrom: (Player*)player
83 self.local = player.local;
84 self.name = player.name;
85 setObj(&_extraValues, [[player->_extraValues mutableCopy] autorelease]);
89 @synthesize game=_game, name=_name, local=_local;
91 - (BOOL) isCurrent {return self == _game.currentPlayer;}
92 - (BOOL) isFriendly {return self == _game.currentPlayer;} // could be overridden for games with partners
93 - (BOOL) isUnfriendly {return ! self.friendly;}
95 + (NSArray*) keyPathsForValuesAffectingCurrent {return [NSArray arrayWithObject: @"game.currentPlayer"];}
100 return [_game.players indexOfObjectIdenticalTo: self];
103 - (Player*) nextPlayer
105 return [_game.players objectAtIndex: (self.index+1) % _game.players.count];
108 - (Player*) previousPlayer
110 return [_game.players objectAtIndex: (self.index-1) % _game.players.count];
115 return [_game iconForPlayer: self.index];
118 - (NSString*) description
120 return [NSString stringWithFormat: @"%@[%@]", self.class,self.name];