Source/Turn.m
author Jens Alfke <jens@mooseyard.com>
Sat Jul 05 17:46:43 2008 -0700 (2008-07-05)
changeset 11 436cbdf56810
child 15 73f8c889f053
permissions -rw-r--r--
* Improved drag-and-drop (supports CandyBar)
* Fixed DiscPiece
* Inheritable layer styles
etc.
jens@10
     1
//
jens@10
     2
//  Turn.m
jens@10
     3
//  YourMove
jens@10
     4
//
jens@10
     5
//  Created by Jens Alfke on 7/3/08.
jens@10
     6
//  Copyright 2008 Jens Alfke. All rights reserved.
jens@10
     7
//
jens@10
     8
jens@10
     9
#import "Turn.h"
jens@10
    10
#import "Game+Protected.h"
jens@10
    11
#import "Player.h"
jens@10
    12
jens@10
    13
jens@10
    14
NSString* const kTurnCompleteNotification = @"TurnComplete";
jens@10
    15
jens@10
    16
jens@10
    17
@interface Turn ()
jens@10
    18
@property (copy) NSString *move, *boardState;
jens@10
    19
@property (retain) NSDate *date;
jens@10
    20
@end
jens@10
    21
jens@10
    22
jens@10
    23
jens@10
    24
@implementation Turn
jens@10
    25
jens@10
    26
jens@10
    27
- (id) initWithPlayer: (Player*)player
jens@10
    28
{
jens@10
    29
    NSParameterAssert(player!=nil);
jens@10
    30
    self = [super init];
jens@10
    31
    if (self != nil) {
jens@10
    32
        _game = player.game;
jens@10
    33
        _player = player;
jens@10
    34
        _status = kTurnEmpty;
jens@10
    35
        self.boardState = _game.latestTurn.boardState;
jens@10
    36
    }
jens@10
    37
    return self;
jens@10
    38
}
jens@10
    39
jens@10
    40
- (id) initStartOfGame: (Game*)game
jens@10
    41
{
jens@10
    42
    NSParameterAssert(game!=nil);
jens@10
    43
    self = [super init];
jens@10
    44
    if (self != nil) {
jens@10
    45
        _game = game;
jens@10
    46
        _status = kTurnFinished;
jens@10
    47
        self.boardState = game.initialStateString;
jens@10
    48
        self.date = [NSDate date];
jens@10
    49
    }
jens@10
    50
    return self;
jens@10
    51
}
jens@10
    52
jens@10
    53
jens@10
    54
- (id) initWithCoder: (NSCoder*)decoder
jens@10
    55
{
jens@10
    56
    self = [self init];
jens@10
    57
    if( self ) {
jens@10
    58
        _game =        [decoder decodeObjectForKey: @"game"];
jens@10
    59
        _player =      [decoder decodeObjectForKey: @"player"];
jens@10
    60
        _status =      [decoder decodeIntForKey: @"status"];
jens@10
    61
        _move =       [[decoder decodeObjectForKey: @"move"] copy];
jens@10
    62
        _boardState = [[decoder decodeObjectForKey: @"boardState"] copy];
jens@10
    63
        _date =       [[decoder decodeObjectForKey: @"date"] copy];
jens@10
    64
        _comment =    [[decoder decodeObjectForKey: @"comment"] copy];
jens@10
    65
    }
jens@10
    66
    return self;
jens@10
    67
}
jens@10
    68
jens@10
    69
- (void) encodeWithCoder: (NSCoder*)coder
jens@10
    70
{
jens@10
    71
    [coder encodeObject: _game       forKey: @"game"];
jens@10
    72
    [coder encodeObject: _player     forKey: @"player"];
jens@10
    73
    [coder encodeInt:    _status     forKey: @"status"];
jens@10
    74
    [coder encodeObject: _move       forKey: @"move"];
jens@10
    75
    [coder encodeObject: _boardState forKey: @"boardState"];
jens@10
    76
    [coder encodeObject: _date       forKey: @"date"];
jens@10
    77
    [coder encodeObject: _comment    forKey: @"comment"];
jens@10
    78
}
jens@10
    79
jens@10
    80
- (void) dealloc
jens@10
    81
{
jens@10
    82
    [_move release];
jens@10
    83
    [_boardState release];
jens@10
    84
    [_date release];
jens@10
    85
    [_comment release];
jens@10
    86
    [super dealloc];
jens@10
    87
}
jens@10
    88
jens@10
    89
jens@10
    90
- (NSString*) description
jens@10
    91
{
jens@10
    92
    return [NSString stringWithFormat: @"%@[%@, #%i, %@]", self.class, _game.class, self.turnNumber, _move];
jens@10
    93
}
jens@10
    94
jens@10
    95
jens@10
    96
@synthesize game=_game, player=_player, move=_move, boardState=_boardState, date=_date, comment=_comment,
jens@10
    97
            replaying=_replaying;
jens@10
    98
jens@10
    99
jens@10
   100
- (unsigned) turnNumber     {return [_game.turns indexOfObjectIdenticalTo: self];}
jens@10
   101
- (BOOL) isLatestTurn       {return _game.turns.lastObject == self;}
jens@10
   102
- (Turn*) previousTurn      {return [_game.turns objectAtIndex: self.turnNumber-1];}
jens@10
   103
- (Player*) nextPlayer      {return _player ?_player.nextPlayer :[_game.players objectAtIndex: 0];}
jens@10
   104
jens@10
   105
- (TurnStatus) status       {return _status;}
jens@10
   106
jens@10
   107
- (void) setStatus: (TurnStatus)status
jens@10
   108
{
jens@10
   109
    BOOL ok = NO;
jens@10
   110
    switch( _status ) {
jens@10
   111
        case kTurnEmpty:
jens@10
   112
            ok = (status==kTurnPartial) || (status==kTurnComplete);
jens@10
   113
            break;
jens@10
   114
        case kTurnPartial:
jens@10
   115
            ok = (status==kTurnEmpty) || (status==kTurnComplete) || (status==kTurnFinished);
jens@10
   116
            break;
jens@10
   117
        case kTurnComplete:
jens@10
   118
            ok = (status==kTurnEmpty) || (status==kTurnPartial) || (status==kTurnFinished);
jens@10
   119
            break;
jens@10
   120
        case kTurnFinished:
jens@10
   121
            break;
jens@10
   122
    }
jens@10
   123
    NSAssert2(ok,@"Illegal Turn status transition %i -> %i", _status,status);
jens@10
   124
    
jens@10
   125
    [self captureBoardState];
jens@10
   126
    _status = status;
jens@10
   127
    if( _status==kTurnEmpty ) {
jens@10
   128
        self.move = nil;
jens@10
   129
        self.date = nil;
jens@10
   130
    } else
jens@10
   131
        self.date = [NSDate date];
jens@10
   132
}
jens@10
   133
jens@10
   134
jens@10
   135
- (void) addToMove: (NSString*)move
jens@10
   136
{
jens@10
   137
    if( ! _replaying ) {
jens@10
   138
        NSParameterAssert(move.length);
jens@10
   139
        NSAssert(_status<kTurnComplete,@"Complete Turn can't be modified");
jens@10
   140
        if( _move )
jens@10
   141
            move = [_move stringByAppendingString: move];
jens@10
   142
        self.move = move;
jens@10
   143
        [self captureBoardState];
jens@10
   144
        self.date = [NSDate date];
jens@10
   145
        if( _status==kTurnEmpty )
jens@10
   146
            self.status = kTurnPartial;
jens@10
   147
    }
jens@10
   148
}
jens@10
   149
jens@10
   150
jens@10
   151
- (void) captureBoardState
jens@10
   152
{
jens@10
   153
    if( ! _replaying ) {
jens@10
   154
        NSAssert(_status<kTurnFinished,@"Finished Turn can't be modified");
jens@10
   155
        if( _game.board )
jens@10
   156
            self.boardState = _game.stateString;
jens@10
   157
    }
jens@10
   158
}
jens@10
   159
jens@10
   160
jens@10
   161
@end