Source/Player.m
author Jens Alfke <jens@mooseyard.com>
Mon Jul 21 17:32:21 2008 -0700 (2008-07-21)
changeset 21 2eb229411d73
parent 14 4585c74d809c
permissions -rw-r--r--
* Added API to Stack for removing bits.
* GoGame correctly saves/restores number of captured pieces.
* Improved positioning of captured-piece Stacks in GoGame.
* New Go piece icons.
* Added "Warn" function to GGBUtils.
     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 #import "GGBUtils.h"
    12 
    13 
    14 #pragma mark -
    15 @implementation Player
    16 
    17 
    18 - (id) initWithGame: (Game*)game
    19 {
    20     self = [super init];
    21     if (self != nil) {
    22         _game = game;
    23         _local = YES;
    24     }
    25     return self;
    26 }
    27 
    28 - (id) initWithName: (NSString*)name
    29 {
    30     self = [super init];
    31     if (self != nil) {
    32         self.name = name;
    33     }
    34     return self;
    35 }
    36 
    37 
    38 - (id) initWithCoder: (NSCoder*)decoder
    39 {
    40     self = [self init];
    41     if( self ) {
    42         _game =  [decoder decodeObjectForKey: @"game"];
    43         _name = [[decoder decodeObjectForKey: @"name"] copy];
    44         _local=  [decoder decodeBoolForKey:   @"local"];
    45         _extraValues = [[decoder decodeObjectForKey: @"extraValues"] mutableCopy];
    46     }
    47     return self;
    48 }
    49 
    50 - (void) encodeWithCoder: (NSCoder*)coder
    51 {
    52     [coder encodeObject: _game  forKey: @"game"];
    53     [coder encodeObject: _name  forKey: @"name"];
    54     [coder encodeBool:   _local forKey: @"local"];
    55     [coder encodeObject: _extraValues forKey: @"extraValues"];
    56 }
    57 
    58 - (void) dealloc
    59 {
    60     [_name release];
    61     [_extraValues release];
    62     [super dealloc];
    63 }
    64 
    65 
    66 - (id)valueForUndefinedKey:(NSString *)key
    67 {
    68     return [_extraValues objectForKey: key];
    69 }
    70 
    71 - (void)setValue:(id)value forUndefinedKey:(NSString *)key
    72 {
    73     if( ! _extraValues )
    74         _extraValues = [[NSMutableDictionary alloc] init];
    75     if( value )
    76         [_extraValues setObject: value forKey: key];
    77     else
    78         [_extraValues removeObjectForKey: key];
    79 }
    80 
    81 
    82 - (void) copyFrom: (Player*)player
    83 {
    84     self.local = player.local;
    85     self.name = player.name;
    86     setObj(&_extraValues, [[player->_extraValues mutableCopy] autorelease]);
    87 }
    88 
    89 
    90 @synthesize game=_game, name=_name, local=_local;
    91 
    92 - (BOOL) isCurrent      {return self == _game.currentPlayer;}
    93 - (BOOL) isFriendly     {return self == _game.currentPlayer;}   // could be overridden for games with partners
    94 - (BOOL) isUnfriendly   {return ! self.friendly;}
    95 
    96 + (NSArray*) keyPathsForValuesAffectingCurrent {return [NSArray arrayWithObject: @"game.currentPlayer"];}
    97 
    98 
    99 - (int) index
   100 {
   101     return [_game.players indexOfObjectIdenticalTo: self];
   102 }
   103 
   104 - (Player*) nextPlayer
   105 {
   106     return [_game.players objectAtIndex: (self.index+1) % _game.players.count];
   107 }
   108 
   109 - (Player*) previousPlayer
   110 {
   111     return [_game.players objectAtIndex: (self.index-1) % _game.players.count];
   112 }
   113 
   114 - (CGImageRef) icon
   115 {
   116     return [_game iconForPlayer: self.index];
   117 }
   118 
   119 - (NSString*) description
   120 {
   121     return [NSString stringWithFormat: @"%@[%@]", self.class,self.name];
   122 }
   123 
   124 @end