jens@10: // jens@10: // Player.m jens@10: // YourMove jens@10: // jens@10: // Created by Jens Alfke on 7/3/08. jens@10: // Copyright 2008 Jens Alfke. All rights reserved. jens@10: // jens@10: jens@10: #import "Player.h" jens@10: #import "Game.h" jens@10: jens@10: jens@10: #pragma mark - jens@10: @implementation Player jens@10: jens@10: jens@10: - (id) initWithGame: (Game*)game jens@10: { jens@10: self = [super init]; jens@10: if (self != nil) { jens@10: _game = game; jens@10: _local = YES; jens@10: } jens@10: return self; jens@10: } jens@10: jens@10: - (id) initWithName: (NSString*)name jens@10: { jens@10: self = [super init]; jens@10: if (self != nil) { jens@10: self.name = name; jens@10: } jens@10: return self; jens@10: } jens@10: jens@10: jens@10: - (id) initWithCoder: (NSCoder*)decoder jens@10: { jens@10: self = [self init]; jens@10: if( self ) { jens@10: _game = [decoder decodeObjectForKey: @"game"]; jens@10: _name = [[decoder decodeObjectForKey: @"name"] copy]; jens@10: _uuid = [[decoder decodeObjectForKey: @"UUID"] copy]; jens@10: _address = [[decoder decodeObjectForKey: @"address"] copy]; jens@10: _addressType = [[decoder decodeObjectForKey: @"addressType"] copy]; jens@10: _local= [decoder decodeBoolForKey: @"local"]; jens@10: } jens@10: return self; jens@10: } jens@10: jens@10: - (void) encodeWithCoder: (NSCoder*)coder jens@10: { jens@10: [coder encodeObject: _game forKey: @"game"]; jens@10: [coder encodeObject: _name forKey: @"name"]; jens@10: [coder encodeObject: _uuid forKey: @"UUID"]; jens@10: [coder encodeObject: _address forKey: @"address"]; jens@10: [coder encodeObject: _addressType forKey: @"addressType"]; jens@10: [coder encodeBool: _local forKey: @"local"]; jens@10: } jens@10: jens@10: - (void) dealloc jens@10: { jens@10: [_name release]; jens@10: [_uuid release]; jens@10: [_address release]; jens@10: [_addressType release]; jens@10: [super dealloc]; jens@10: } jens@10: jens@10: jens@10: @synthesize game=_game, name=_name, UUID=_uuid, address=_address, addressType=_addressType, local=_local; jens@10: jens@10: - (BOOL) isCurrent {return self == _game.currentPlayer;} jens@10: - (BOOL) isFriendly {return self == _game.currentPlayer;} // could be overridden for games with partners jens@10: - (BOOL) isUnfriendly {return ! self.friendly;} jens@10: jens@10: + (NSArray*) keyPathsForValuesAffectingCurrent {return [NSArray arrayWithObject: @"game.currentPlayer"];} jens@10: jens@10: jens@10: - (int) index jens@10: { jens@10: return [_game.players indexOfObjectIdenticalTo: self]; jens@10: } jens@10: jens@10: - (Player*) nextPlayer jens@10: { jens@10: return [_game.players objectAtIndex: (self.index+1) % _game.players.count]; jens@10: } jens@10: jens@10: - (Player*) previousPlayer jens@10: { jens@10: return [_game.players objectAtIndex: (self.index-1) % _game.players.count]; jens@10: } jens@10: jens@10: - (CGImageRef) icon jens@10: { jens@10: return [_game iconForPlayer: self.index]; jens@10: } jens@10: jens@10: - (NSString*) description jens@10: { jens@10: return [NSString stringWithFormat: @"%@[%@]", self.class,self.name]; jens@10: } jens@10: jens@10: @end