Source/Player.m
changeset 12 4e567e11f45f
child 13 db7bb080c3d5
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/Source/Player.m	Mon Jul 07 15:47:42 2008 -0700
     1.3 @@ -0,0 +1,105 @@
     1.4 +//
     1.5 +//  Player.m
     1.6 +//  YourMove
     1.7 +//
     1.8 +//  Created by Jens Alfke on 7/3/08.
     1.9 +//  Copyright 2008 Jens Alfke. All rights reserved.
    1.10 +//
    1.11 +
    1.12 +#import "Player.h"
    1.13 +#import "Game.h"
    1.14 +
    1.15 +
    1.16 +#pragma mark -
    1.17 +@implementation Player
    1.18 +
    1.19 +
    1.20 +- (id) initWithGame: (Game*)game
    1.21 +{
    1.22 +    self = [super init];
    1.23 +    if (self != nil) {
    1.24 +        _game = game;
    1.25 +        _local = YES;
    1.26 +    }
    1.27 +    return self;
    1.28 +}
    1.29 +
    1.30 +- (id) initWithName: (NSString*)name
    1.31 +{
    1.32 +    self = [super init];
    1.33 +    if (self != nil) {
    1.34 +        self.name = name;
    1.35 +    }
    1.36 +    return self;
    1.37 +}
    1.38 +
    1.39 +
    1.40 +- (id) initWithCoder: (NSCoder*)decoder
    1.41 +{
    1.42 +    self = [self init];
    1.43 +    if( self ) {
    1.44 +        _game =  [decoder decodeObjectForKey: @"game"];
    1.45 +        _name = [[decoder decodeObjectForKey: @"name"] copy];
    1.46 +        _uuid = [[decoder decodeObjectForKey: @"UUID"] copy];
    1.47 +        _address = [[decoder decodeObjectForKey: @"address"] copy];
    1.48 +        _addressType = [[decoder decodeObjectForKey: @"addressType"] copy];
    1.49 +        _local=  [decoder decodeBoolForKey:   @"local"];
    1.50 +    }
    1.51 +    return self;
    1.52 +}
    1.53 +
    1.54 +- (void) encodeWithCoder: (NSCoder*)coder
    1.55 +{
    1.56 +    [coder encodeObject: _game  forKey: @"game"];
    1.57 +    [coder encodeObject: _name  forKey: @"name"];
    1.58 +    [coder encodeObject: _uuid  forKey: @"UUID"];
    1.59 +    [coder encodeObject: _address forKey: @"address"];
    1.60 +    [coder encodeObject: _addressType forKey: @"addressType"];
    1.61 +    [coder encodeBool:   _local forKey: @"local"];
    1.62 +}
    1.63 +
    1.64 +- (void) dealloc
    1.65 +{
    1.66 +    [_name release];
    1.67 +    [_uuid release];
    1.68 +    [_address release];
    1.69 +    [_addressType release];
    1.70 +    [super dealloc];
    1.71 +}
    1.72 +
    1.73 +
    1.74 +@synthesize game=_game, name=_name, UUID=_uuid, address=_address, addressType=_addressType, local=_local;
    1.75 +
    1.76 +- (BOOL) isCurrent      {return self == _game.currentPlayer;}
    1.77 +- (BOOL) isFriendly     {return self == _game.currentPlayer;}   // could be overridden for games with partners
    1.78 +- (BOOL) isUnfriendly   {return ! self.friendly;}
    1.79 +
    1.80 ++ (NSArray*) keyPathsForValuesAffectingCurrent {return [NSArray arrayWithObject: @"game.currentPlayer"];}
    1.81 +
    1.82 +
    1.83 +- (int) index
    1.84 +{
    1.85 +    return [_game.players indexOfObjectIdenticalTo: self];
    1.86 +}
    1.87 +
    1.88 +- (Player*) nextPlayer
    1.89 +{
    1.90 +    return [_game.players objectAtIndex: (self.index+1) % _game.players.count];
    1.91 +}
    1.92 +
    1.93 +- (Player*) previousPlayer
    1.94 +{
    1.95 +    return [_game.players objectAtIndex: (self.index-1) % _game.players.count];
    1.96 +}
    1.97 +
    1.98 +- (CGImageRef) icon
    1.99 +{
   1.100 +    return [_game iconForPlayer: self.index];
   1.101 +}
   1.102 +
   1.103 +- (NSString*) description
   1.104 +{
   1.105 +    return [NSString stringWithFormat: @"%@[%@]", self.class,self.name];
   1.106 +}
   1.107 +
   1.108 +@end