Source/Player.m
author Jens Alfke <jens@mooseyard.com>
Tue Jul 08 13:12:01 2008 -0700 (2008-07-08)
changeset 13 db7bb080c3d5
parent 10 6c78cc6bd7a6
child 14 4585c74d809c
permissions -rw-r--r--
Fixed some memory leaks, and took the address-related properties out of Player.
     1 //
     2 //  Player.m
     3 //  YourMove
     4 //
     5 //  Created by Jens Alfke on 7/3/08.
     6 //  Copyright 2008 Jens Alfke. All rights reserved.
     7 //
     8 
     9 #import "Player.h"
    10 #import "Game.h"
    11 
    12 
    13 #pragma mark -
    14 @implementation Player
    15 
    16 
    17 - (id) initWithGame: (Game*)game
    18 {
    19     self = [super init];
    20     if (self != nil) {
    21         _game = game;
    22         _local = YES;
    23     }
    24     return self;
    25 }
    26 
    27 - (id) initWithName: (NSString*)name
    28 {
    29     self = [super init];
    30     if (self != nil) {
    31         self.name = name;
    32     }
    33     return self;
    34 }
    35 
    36 
    37 - (id) initWithCoder: (NSCoder*)decoder
    38 {
    39     self = [self init];
    40     if( self ) {
    41         _game =  [decoder decodeObjectForKey: @"game"];
    42         _name = [[decoder decodeObjectForKey: @"name"] copy];
    43         _local=  [decoder decodeBoolForKey:   @"local"];
    44         _extraValues = [[decoder decodeObjectForKey: @"extraValues"] mutableCopy];
    45     }
    46     return self;
    47 }
    48 
    49 - (void) encodeWithCoder: (NSCoder*)coder
    50 {
    51     [coder encodeObject: _game  forKey: @"game"];
    52     [coder encodeObject: _name  forKey: @"name"];
    53     [coder encodeBool:   _local forKey: @"local"];
    54     [coder encodeObject: _extraValues forKey: @"extraValues"];
    55 }
    56 
    57 - (void) dealloc
    58 {
    59     [_name release];
    60     [_extraValues release];
    61     [super dealloc];
    62 }
    63 
    64 
    65 - (id)valueForUndefinedKey:(NSString *)key
    66 {
    67     return [_extraValues objectForKey: key];
    68 }
    69 
    70 - (void)setValue:(id)value forUndefinedKey:(NSString *)key
    71 {
    72     if( ! _extraValues )
    73         _extraValues = [[NSMutableDictionary alloc] init];
    74     if( value )
    75         [_extraValues setObject: value forKey: key];
    76     else
    77         [_extraValues removeObjectForKey: key];
    78 }
    79 
    80 
    81 @synthesize game=_game, name=_name, local=_local;
    82 
    83 - (BOOL) isCurrent      {return self == _game.currentPlayer;}
    84 - (BOOL) isFriendly     {return self == _game.currentPlayer;}   // could be overridden for games with partners
    85 - (BOOL) isUnfriendly   {return ! self.friendly;}
    86 
    87 + (NSArray*) keyPathsForValuesAffectingCurrent {return [NSArray arrayWithObject: @"game.currentPlayer"];}
    88 
    89 
    90 - (int) index
    91 {
    92     return [_game.players indexOfObjectIdenticalTo: self];
    93 }
    94 
    95 - (Player*) nextPlayer
    96 {
    97     return [_game.players objectAtIndex: (self.index+1) % _game.players.count];
    98 }
    99 
   100 - (Player*) previousPlayer
   101 {
   102     return [_game.players objectAtIndex: (self.index-1) % _game.players.count];
   103 }
   104 
   105 - (CGImageRef) icon
   106 {
   107     return [_game iconForPlayer: self.index];
   108 }
   109 
   110 - (NSString*) description
   111 {
   112     return [NSString stringWithFormat: @"%@[%@]", self.class,self.name];
   113 }
   114 
   115 @end