Source/Turn.m
changeset 13 db7bb080c3d5
child 15 73f8c889f053
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/Source/Turn.m	Tue Jul 08 13:12:01 2008 -0700
     1.3 @@ -0,0 +1,161 @@
     1.4 +//
     1.5 +//  Turn.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 "Turn.h"
    1.13 +#import "Game+Protected.h"
    1.14 +#import "Player.h"
    1.15 +
    1.16 +
    1.17 +NSString* const kTurnCompleteNotification = @"TurnComplete";
    1.18 +
    1.19 +
    1.20 +@interface Turn ()
    1.21 +@property (copy) NSString *move, *boardState;
    1.22 +@property (retain) NSDate *date;
    1.23 +@end
    1.24 +
    1.25 +
    1.26 +
    1.27 +@implementation Turn
    1.28 +
    1.29 +
    1.30 +- (id) initWithPlayer: (Player*)player
    1.31 +{
    1.32 +    NSParameterAssert(player!=nil);
    1.33 +    self = [super init];
    1.34 +    if (self != nil) {
    1.35 +        _game = player.game;
    1.36 +        _player = player;
    1.37 +        _status = kTurnEmpty;
    1.38 +        self.boardState = _game.latestTurn.boardState;
    1.39 +    }
    1.40 +    return self;
    1.41 +}
    1.42 +
    1.43 +- (id) initStartOfGame: (Game*)game
    1.44 +{
    1.45 +    NSParameterAssert(game!=nil);
    1.46 +    self = [super init];
    1.47 +    if (self != nil) {
    1.48 +        _game = game;
    1.49 +        _status = kTurnFinished;
    1.50 +        self.boardState = game.initialStateString;
    1.51 +        self.date = [NSDate date];
    1.52 +    }
    1.53 +    return self;
    1.54 +}
    1.55 +
    1.56 +
    1.57 +- (id) initWithCoder: (NSCoder*)decoder
    1.58 +{
    1.59 +    self = [self init];
    1.60 +    if( self ) {
    1.61 +        _game =        [decoder decodeObjectForKey: @"game"];
    1.62 +        _player =      [decoder decodeObjectForKey: @"player"];
    1.63 +        _status =      [decoder decodeIntForKey: @"status"];
    1.64 +        _move =       [[decoder decodeObjectForKey: @"move"] copy];
    1.65 +        _boardState = [[decoder decodeObjectForKey: @"boardState"] copy];
    1.66 +        _date =       [[decoder decodeObjectForKey: @"date"] copy];
    1.67 +        _comment =    [[decoder decodeObjectForKey: @"comment"] copy];
    1.68 +    }
    1.69 +    return self;
    1.70 +}
    1.71 +
    1.72 +- (void) encodeWithCoder: (NSCoder*)coder
    1.73 +{
    1.74 +    [coder encodeObject: _game       forKey: @"game"];
    1.75 +    [coder encodeObject: _player     forKey: @"player"];
    1.76 +    [coder encodeInt:    _status     forKey: @"status"];
    1.77 +    [coder encodeObject: _move       forKey: @"move"];
    1.78 +    [coder encodeObject: _boardState forKey: @"boardState"];
    1.79 +    [coder encodeObject: _date       forKey: @"date"];
    1.80 +    [coder encodeObject: _comment    forKey: @"comment"];
    1.81 +}
    1.82 +
    1.83 +- (void) dealloc
    1.84 +{
    1.85 +    [_move release];
    1.86 +    [_boardState release];
    1.87 +    [_date release];
    1.88 +    [_comment release];
    1.89 +    [super dealloc];
    1.90 +}
    1.91 +
    1.92 +
    1.93 +- (NSString*) description
    1.94 +{
    1.95 +    return [NSString stringWithFormat: @"%@[%@, #%i, %@]", self.class, _game.class, self.turnNumber, _move];
    1.96 +}
    1.97 +
    1.98 +
    1.99 +@synthesize game=_game, player=_player, move=_move, boardState=_boardState, date=_date, comment=_comment,
   1.100 +            replaying=_replaying;
   1.101 +
   1.102 +
   1.103 +- (unsigned) turnNumber     {return [_game.turns indexOfObjectIdenticalTo: self];}
   1.104 +- (BOOL) isLatestTurn       {return _game.turns.lastObject == self;}
   1.105 +- (Turn*) previousTurn      {return [_game.turns objectAtIndex: self.turnNumber-1];}
   1.106 +- (Player*) nextPlayer      {return _player ?_player.nextPlayer :[_game.players objectAtIndex: 0];}
   1.107 +
   1.108 +- (TurnStatus) status       {return _status;}
   1.109 +
   1.110 +- (void) setStatus: (TurnStatus)status
   1.111 +{
   1.112 +    BOOL ok = NO;
   1.113 +    switch( _status ) {
   1.114 +        case kTurnEmpty:
   1.115 +            ok = (status==kTurnPartial) || (status==kTurnComplete);
   1.116 +            break;
   1.117 +        case kTurnPartial:
   1.118 +            ok = (status==kTurnEmpty) || (status==kTurnComplete) || (status==kTurnFinished);
   1.119 +            break;
   1.120 +        case kTurnComplete:
   1.121 +            ok = (status==kTurnEmpty) || (status==kTurnPartial) || (status==kTurnFinished);
   1.122 +            break;
   1.123 +        case kTurnFinished:
   1.124 +            break;
   1.125 +    }
   1.126 +    NSAssert2(ok,@"Illegal Turn status transition %i -> %i", _status,status);
   1.127 +    
   1.128 +    [self captureBoardState];
   1.129 +    _status = status;
   1.130 +    if( _status==kTurnEmpty ) {
   1.131 +        self.move = nil;
   1.132 +        self.date = nil;
   1.133 +    } else
   1.134 +        self.date = [NSDate date];
   1.135 +}
   1.136 +
   1.137 +
   1.138 +- (void) addToMove: (NSString*)move
   1.139 +{
   1.140 +    if( ! _replaying ) {
   1.141 +        NSParameterAssert(move.length);
   1.142 +        NSAssert(_status<kTurnComplete,@"Complete Turn can't be modified");
   1.143 +        if( _move )
   1.144 +            move = [_move stringByAppendingString: move];
   1.145 +        self.move = move;
   1.146 +        [self captureBoardState];
   1.147 +        self.date = [NSDate date];
   1.148 +        if( _status==kTurnEmpty )
   1.149 +            self.status = kTurnPartial;
   1.150 +    }
   1.151 +}
   1.152 +
   1.153 +
   1.154 +- (void) captureBoardState
   1.155 +{
   1.156 +    if( ! _replaying ) {
   1.157 +        NSAssert(_status<kTurnFinished,@"Finished Turn can't be modified");
   1.158 +        if( _game.board )
   1.159 +            self.boardState = _game.stateString;
   1.160 +    }
   1.161 +}
   1.162 +
   1.163 +
   1.164 +@end