This repository has been archived by the owner on Feb 28, 2018. It is now read-only.
forked from klausthul/kibitz
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Game.m
325 lines (278 loc) · 6.75 KB
/
Game.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/*
$Id$
Copyright 2006 Klaus Thul ([email protected])
This file is part of Kibitz.
Kibitz is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
Kibitz is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with Kibitz; if not, write to the
Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#import <ctype.h>
#import "Game.h"
#import "MoveStore.h"
@implementation Game
- (Game *) init
{
if ((self = [super init]) != nil) {
moves = [[NSMutableArray arrayWithCapacity: 100] retain];
playSound = [[[NSUserDefaults standardUserDefaults] objectForKey: @"soundDefault"] intValue] != 0;
}
return self;
}
- (void) setPlayerNamesWhite: (NSString *) white black: (NSString *) black
{
[whiteName release];
whiteName = [white retain];
[blackName release];
blackName = [black retain];
}
- (NSString *) whiteName
{
return [[whiteName retain] autorelease];
}
- (NSString *) blackName
{
return [[blackName retain] autorelease];
}
- (void) setTimeInitial: (int) initial increment: (int) increment
{
initialTime = initial;
incrementTime = increment;
}
- (int) initialTime
{
return initialTime;
}
- (int) incrementTime
{
return incrementTime;
}
- (ChessMove *) currentBoardPosition
{
return lastMove;
}
- (void) newMove: (ChessMove *) move
{
[lastMove release];
lastMove = [move retain];
MoveStore *ms;
NSEnumerator *e = [moves objectEnumerator];
int n = [move moveNumber];
if (n == 0) {
[self setStartPosition: move];
} else {
while ((ms = [e nextObject]) != nil)
if (ms.moveNum == n)
break;
[self willChangeValueForKey: @"moves"];
if (ms == nil) {
ms = [[[MoveStore alloc] initWithMoveNum: n] autorelease];
[moves addObject: ms];
}
if ([move moveColor] == WHITE)
ms.whiteMove = move;
else
ms.blackMove = move;
[self didChangeValueForKey: @"moves"];
}
}
- (void) dealloc
{
[whiteName release];
[blackName release];
[lastMove release];
[ratingWhite release];
[ratingBlack release];
[type release];
[moves release];
[super dealloc];
}
- (Game *) initWithStyle12: (NSArray *) data
{
if ((self = [self init]) != nil) {
[self updateWithStyle12: data];
int sps = [[[NSUserDefaults standardUserDefaults] objectForKey: @"soundDefault"] intValue];
playSound = (sps == 2) || ((sps == 1) && ([self gameRelationship] == PLAYING_MYMOVE || [self gameRelationship] == PLAYING_OPONENT_MOVE));
}
return self;
}
- (Game *) initWithGameInfo: (NSArray *) data
{
if ((self = [self init]) != nil) {
[self updateWithGameInfo: data];
int sps = [[[NSUserDefaults standardUserDefaults] objectForKey: @"soundDefault"] intValue];
playSound = (sps == 2) || ((sps == 1) && ([self gameRelationship] == PLAYING_MYMOVE || [self gameRelationship] == PLAYING_OPONENT_MOVE));
}
return self;
}
- (void) updateWithStyle12: (NSArray *) data
{
[self setPlayerNamesWhite: data[17] black: data[18]];
[self setTimeInitial: [data[20] intValue] increment: [data[21] intValue]];
gameNumber = [data[16] intValue];
gameRelationship = [data[19] intValue];
}
- (void) updateWithGameInfo: (NSArray *) data
{
NSString *s;
gameNumber = [data[0] intValue];
if ((s = [ChessServerConnection findTag: @"t=" in: data]) != nil) {
[type release];
type = [s retain];
}
if ((s = [ChessServerConnection findTag: @"r=" in: data]) != nil)
rated = (s.intValue == 1);
if ((s = [ChessServerConnection findTag: @"pt=" in: data]) != nil)
partnerGame = s.intValue;
if ((s = [ChessServerConnection findTag: @"rt=" in: data]) != nil) {
NSArray *ra = [s componentsSeparatedByString: @","];
if (ra.count >= 2) {
[ratingWhite release];
ratingWhite = [ra[0] retain];
[ratingBlack release];
ratingBlack = [ra[1] retain];
}
}
}
- (Game *) initWithEmptyGame
{
if ((self = [self init]) != nil) {
[self setPlayerNamesWhite: @"White" black: @"Black"];
[self newMove: [ChessMove initialPosition]];
initialTime = -1;
isEmpty = YES;
}
return self;
}
- (NSString *) gameInfoString
{
if (result)
return [NSString stringWithFormat: @"was Game #%d: %@ - %@, (%d+%d), Result: %@", gameNumber, whiteName, blackName, initialTime, incrementTime, result];
else
return [NSString stringWithFormat: @"Game #%d: %@ - %@ (%d+%d)", gameNumber, whiteName, blackName, initialTime, incrementTime];
}
- (void) setResult: (NSString *) resultV reason: (NSString *) reasonV
{
[result release];
result = [resultV retain];
[reason release];
reason = [reasonV retain];
[lastMove stopClock];
}
- (NSString *) result
{
return result;
}
- (NSString *) reason
{
return reason;
}
- (void) setSideShownOnBottom: (enum Color) color
{
sideShownOnBottom = color;
}
- (void) flipSideShownOnBottom
{
sideShownOnBottom = (sideShownOnBottom == BLACK) ? WHITE : BLACK;
}
- (enum Color) sideShownOnBottom
{
return sideShownOnBottom;
}
- (enum GameRelationship) gameRelationship
{
return gameRelationship;
}
- (void) setDefaultBoardOrientation
{
switch (gameRelationship) {
case PLAYING_OPONENT_MOVE:
[self setSideShownOnBottom: BLACK - [self sideToMove]];
break;
case PLAYING_MYMOVE:
[self setSideShownOnBottom: [self sideToMove]];
break;
default:
[self setSideShownOnBottom: WHITE];
break;
}
}
- (enum Color) sideToMove
{
return [lastMove sideToMove];
}
- (NSString *) ratingWhite
{
return ratingWhite;
}
- (NSString *) ratingBlack
{
return ratingBlack;
}
- (NSString *) type
{
return type;
}
- (bool) rated
{
return rated;
}
- (NSString *) whiteNameRating
{
if (whiteName && ratingWhite)
return [NSString stringWithFormat: @"%@\n%@", whiteName, ratingWhite];
else
return whiteName;
}
- (NSString *) blackNameRating
{
if (blackName && ratingBlack)
return [NSString stringWithFormat: @"%@\n%@", blackName, ratingBlack];
else
return blackName;
}
- (void) setStartPosition: (ChessMove *) move
{
[startPosition release];
startPosition = [move retain];
}
- (ChessMove *) startPosition
{
return startPosition;
}
- (ChessMove *) storedMoveNumber: (unsigned int) num
{
if (num >= moves.count)
return nil;
else {
MoveStore *ms = moves[num];
ChessMove *m = ms.blackMove;
if (m != nil)
return m;
else
return ms.whiteMove;
}
}
- (int) numMoves
{
return moves.count;
}
- (void) passedPiecesWhite: (NSString *) white black: (NSString *) black
{
[lastMove passedPiecesWhite: white black: black];
}
- (BOOL) isEmpty
{
return isEmpty;
}
- (BOOL) playSound
{
return playSound;
}
- (void) setPlaySound: (bool) ps
{
playSound = ps;
}
@end