Don't load Checkers sound files till a game is displayed on a board; this speeds up launch.
5 // Created by Jens Alfke on 7/3/08.
6 // Copyright 2008 Jens Alfke. All rights reserved.
10 #import "Game+Protected.h"
14 NSString* const kTurnCompleteNotification = @"TurnComplete";
18 @property (copy) NSString *move, *boardState;
19 @property (retain) NSDate *date;
27 - (id) initWithPlayer: (Player*)player
29 NSParameterAssert(player!=nil);
35 self.boardState = _game.latestTurn.boardState;
40 - (id) initStartOfGame: (Game*)game
42 NSParameterAssert(game!=nil);
46 _status = kTurnFinished;
47 self.boardState = game.initialStateString;
48 self.date = [NSDate date];
54 - (id) initWithCoder: (NSCoder*)decoder
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];
69 - (void) encodeWithCoder: (NSCoder*)coder
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"];
83 [_boardState release];
90 - (NSString*) description
92 return [NSString stringWithFormat: @"%@[%@, #%i, %@]", self.class, _game.class, self.turnNumber, _move];
96 @synthesize game=_game, player=_player, move=_move, boardState=_boardState, date=_date, comment=_comment,
100 - (unsigned) turnNumber {return [_game.turns indexOfObjectIdenticalTo: self];}
101 - (BOOL) isLatestTurn {return _game.turns.lastObject == self;}
102 - (Player*) nextPlayer {return _player ?_player.nextPlayer :[_game.players objectAtIndex: 0];}
103 - (TurnStatus) status {return _status;}
105 - (void) setStatus: (TurnStatus)status
110 ok = (status==kTurnPartial) || (status==kTurnComplete);
113 ok = (status==kTurnEmpty) || (status==kTurnComplete) || (status==kTurnFinished);
116 ok = (status==kTurnEmpty) || (status==kTurnPartial) || (status==kTurnFinished);
121 NSAssert2(ok,@"Illegal Turn status transition %i -> %i", _status,status);
123 [self captureBoardState];
125 if( _status==kTurnEmpty ) {
129 self.date = [NSDate date];
135 NSAssert(_status==kTurnFinished,@"Turn must be finished");
136 [self willChangeValueForKey: @"status"];
137 _status = kTurnComplete;
138 [self didChangeValueForKey: @"status"];
142 - (Turn*) previousTurn
144 unsigned n = self.turnNumber;
146 return [_game.turns objectAtIndex: n-1];
153 unsigned n = self.turnNumber;
154 if( n+1 < _game.turns.count )
155 return [_game.turns objectAtIndex: n+1];
161 - (void) addToMove: (NSString*)move
164 NSParameterAssert(move.length);
165 NSAssert(_status<kTurnComplete,@"Complete Turn can't be modified");
167 move = [_move stringByAppendingString: move];
169 [self captureBoardState];
170 self.date = [NSDate date];
171 if( _status==kTurnEmpty )
172 self.status = kTurnPartial;
177 - (void) captureBoardState
180 NSAssert(_status<kTurnFinished,@"Finished Turn can't be modified");
182 self.boardState = _game.stateString;