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