Source/Turn.m
author Jens Alfke <jens@mooseyard.com>
Mon Jul 21 17:32:21 2008 -0700 (2008-07-21)
changeset 21 2eb229411d73
parent 15 73f8c889f053
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.
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
- (Player*) nextPlayer      {return _player ?_player.nextPlayer :[_game.players objectAtIndex: 0];}
jens@10
   103
- (TurnStatus) status       {return _status;}
jens@10
   104
jens@10
   105
- (void) setStatus: (TurnStatus)status
jens@10
   106
{
jens@10
   107
    BOOL ok = NO;
jens@10
   108
    switch( _status ) {
jens@10
   109
        case kTurnEmpty:
jens@10
   110
            ok = (status==kTurnPartial) || (status==kTurnComplete);
jens@10
   111
            break;
jens@10
   112
        case kTurnPartial:
jens@10
   113
            ok = (status==kTurnEmpty) || (status==kTurnComplete) || (status==kTurnFinished);
jens@10
   114
            break;
jens@10
   115
        case kTurnComplete:
jens@10
   116
            ok = (status==kTurnEmpty) || (status==kTurnPartial) || (status==kTurnFinished);
jens@10
   117
            break;
jens@10
   118
        case kTurnFinished:
jens@10
   119
            break;
jens@10
   120
    }
jens@10
   121
    NSAssert2(ok,@"Illegal Turn status transition %i -> %i", _status,status);
jens@10
   122
    
jens@10
   123
    [self captureBoardState];
jens@10
   124
    _status = status;
jens@10
   125
    if( _status==kTurnEmpty ) {
jens@10
   126
        self.move = nil;
jens@10
   127
        self.date = nil;
jens@10
   128
    } else
jens@10
   129
        self.date = [NSDate date];
jens@10
   130
}
jens@10
   131
jens@10
   132
jens@16
   133
- (void) _unfinish
jens@16
   134
{
jens@16
   135
    NSAssert(_status==kTurnFinished,@"Turn must be finished");
jens@16
   136
    [self willChangeValueForKey: @"status"];
jens@16
   137
    _status = kTurnComplete;
jens@16
   138
    [self didChangeValueForKey: @"status"];
jens@16
   139
}
jens@16
   140
jens@16
   141
jens@15
   142
- (Turn*) previousTurn
jens@15
   143
{
jens@15
   144
    unsigned n = self.turnNumber;
jens@15
   145
    if( n > 0 )
jens@15
   146
        return [_game.turns objectAtIndex: n-1];
jens@15
   147
    else
jens@15
   148
        return nil;
jens@15
   149
}
jens@15
   150
jens@15
   151
- (Turn*) nextTurn
jens@15
   152
{
jens@15
   153
    unsigned n = self.turnNumber;
jens@15
   154
    if( n+1 < _game.turns.count )
jens@15
   155
        return [_game.turns objectAtIndex: n+1];
jens@15
   156
    else
jens@15
   157
        return nil;
jens@15
   158
}
jens@15
   159
jens@15
   160
jens@10
   161
- (void) addToMove: (NSString*)move
jens@10
   162
{
jens@10
   163
    if( ! _replaying ) {
jens@10
   164
        NSParameterAssert(move.length);
jens@10
   165
        NSAssert(_status<kTurnComplete,@"Complete Turn can't be modified");
jens@10
   166
        if( _move )
jens@10
   167
            move = [_move stringByAppendingString: move];
jens@10
   168
        self.move = move;
jens@10
   169
        [self captureBoardState];
jens@10
   170
        self.date = [NSDate date];
jens@10
   171
        if( _status==kTurnEmpty )
jens@10
   172
            self.status = kTurnPartial;
jens@10
   173
    }
jens@10
   174
}
jens@10
   175
jens@10
   176
jens@10
   177
- (void) captureBoardState
jens@10
   178
{
jens@10
   179
    if( ! _replaying ) {
jens@10
   180
        NSAssert(_status<kTurnFinished,@"Finished Turn can't be modified");
jens@16
   181
        if( _game.table )
jens@10
   182
            self.boardState = _game.stateString;
jens@10
   183
    }
jens@10
   184
}
jens@10
   185
jens@10
   186
jens@10
   187
@end