-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathGobanGameDocument.m
689 lines (545 loc) · 17 KB
/
GobanGameDocument.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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
/*$Id: GobanGameDocument.m,v 1.7 2001/10/05 17:44:39 phink Exp $*/
// This is Goban, a Go program for Mac OS X. Contact [email protected],
// or see http://www.sente.ch/software/goban for more information.
//
// Copyright (c) 1997-2001, Sen:te (Sente SA). All rights reserved.
//
// This program 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 - version 2.
//
// This program 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 in file COPYING
// for more details.
//
// You should have received a copy of the GNU General Public
// License along with this program; if not, write to the Free
// Software Foundation, Inc., 59 Temple Place - Suite 330,
// Boston, MA 02111, USA.
#import "GobanGameDocument.h"
#import "GobanPlayerDefaults.h"
#import "GobanHumanPlayer.h"
#import "GobanGnuGoPlayer.h"
#import "GobanView.h"
#import "GobanInspectorController.h"
#import "SenInterfaceValidation.h"
#import <GoGame/GoGame.h>
#import <GoGame/GoPosition.h>
#import <GoGame/GoSymbol.h>
#import <GoGame/GoGeometry.h>
#import <GoGame/GoMove.h>
#import <SenFoundation/SenFoundation.h>
@interface GobanGameDocument (Private)
- (void) setBlackPlayer:(GobanPlayer *) aPlayer;
- (void) setWhitePlayer:(GobanPlayer *) aPlayer;
- (void) setGame:(GoGame *) aGame;
- (void) finishGame;
@end
@implementation GobanGameDocument
static BOOL isNewDocumentNeeded = YES;
- (void) setDefaultPlayers
{
[self setWhitePlayer:[[[[GobanPlayerDefaults sharedInstance] prototypeWhitePlayer] copy] autorelease]];
[self setBlackPlayer:[[[[GobanPlayerDefaults sharedInstance] prototypeBlackPlayer] copy] autorelease]];
}
- (id)init
{
[super init];
if (isNewDocumentNeeded) {
[self setGame:[[[GoGame alloc] init] autorelease]];
}
[self setDefaultPlayers];
return self;
}
- (id) window
{
return [grid window];
}
- (void) close
{
[self finishGame];
[super close];
}
- (void) dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
RELEASE (game);
RELEASE (whitePlayer);
RELEASE (blackPlayer);
RELEASE (scorer);
[super dealloc];
}
- (id) initWithContentsOfFile:(NSString *)fileName ofType:(NSString *)fileType
{
isNewDocumentNeeded = NO;
[super initWithContentsOfFile:fileName ofType:fileType];
isNewDocumentNeeded = YES;
return self;
}
- (id)initWithContentsOfURL:(NSURL *)anURL ofType:(NSString *)docType
{
isNewDocumentNeeded = NO;
[super initWithContentsOfURL:anURL ofType:docType];
isNewDocumentNeeded = YES;
return self;
}
- (NSString *) windowNibName
{
return @"GobanGameDocument";
}
- (GoGame *) game
{
return game;
}
- (NSString *) applicationProperty
{
NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
return [NSString stringWithFormat:@"%@:%@",
[infoDictionary objectForKey:@"CFBundleName"],
[infoDictionary objectForKey:@"CFBundleVersion"]];
}
- (void) setGame:(GoGame *) aGame
{
if (game != nil) {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
ASSIGN (game, aGame);
[game setApplicationProperty:[self applicationProperty]];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(gameDidChange:) name:@"GoGamePositionDidChange" object:game];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(gameDidChange:) name:@"GoGameScoreDidChange" object:game];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(gameRulesDidChange:) name:@"GoGameRulesDidChange" object:game];
}
- (void) scoreGame
{
scorer = [[GobanGnuGoPlayer alloc] init];
[scorer setLaunchPath:@""];
[scorer setLaunchArgumentString:@"--mode gtp --quiet -M 32.0"];
[scorer setLevel:10];
[scorer scoreGame:game];
}
- (void) gameDidChange:(NSNotification *) aNotification
{
if ([game isOver] && ![game isResigned] && (scorer == nil)) {
[self scoreGame];
}
[self redisplay];
}
- (void) gameRulesDidChange:(NSNotification *) aNotification
{
senassert (![game isStarted]);
[grid setGame:game];
[grid update];
}
- (void) redisplay
{
[grid update];
[[self window] update];
}
- (void) windowControllerDidLoadNib:(NSWindowController *) aController
{
[super windowControllerDidLoadNib:aController];
[aController setWindowFrameAutosaveName:@"Goban"];
[grid setGame:game];
[self redisplay];
}
- (BOOL) readFromFile:(NSString *)fileName ofType:(NSString *) docType
{
[self setGame:[[[GoGame alloc] initWithContentsOfFile:fileName] autorelease]];
[self setWhitePlayer:[[[GobanPlayerDefaults sharedInstance] playerNamed:[game whitePlayerName]] copy]];
[self setBlackPlayer:[[[GobanPlayerDefaults sharedInstance] playerNamed:[game blackPlayerName]] copy]];
[self startOrResume:nil];
return YES;
}
- (BOOL) writeToFile:(NSString *)fileName ofType:(NSString *)type
{
return [game writeToFile:fileName atomically:NO];
}
- (void) setWhitePlayer:(GobanPlayer *) aPlayer
{
ASSIGN (whitePlayer, aPlayer);
[whitePlayer setReferee:self];
[game setWhitePlayerName:[whitePlayer name]];
}
- (void) setBlackPlayer:(GobanPlayer *) aPlayer
{
ASSIGN (blackPlayer, aPlayer);
[blackPlayer setReferee:self];
[game setBlackPlayerName:[blackPlayer name]];
}
- (GobanPlayer *) opponentTo:(GobanPlayer *) aPlayer
{
return (aPlayer == blackPlayer) ? whitePlayer : blackPlayer;
}
- (GobanPlayer *) nextPlayer
{
return ([game nextColor] == [GoSymbol black]) ? blackPlayer : whitePlayer;
}
- (void) showGameOver
{
if (![game isResigned]) {
[grid showTerritory];
}
[inspector showInfo:@"Score" shouldOpenDrawer:YES];
}
- (BOOL) isProgramThinking
{
return [game isStarted] && ![game isOver] && ![[self nextPlayer] isInteractive];
}
- (BOOL) isProgramScoring
{
return [game isScoring];
}
- (NSString *) nextPlayerName
{
return [[self nextPlayer] name];
}
- (void) exceptionSheetDidEnd:(NSWindow *)sheet returnCode:(int)returnCode contextInfo:(void *)contextInfo
{
}
- (void)updateChangeCount:(NSDocumentChangeType)change
{
if ((change > 0) && ([[NSUserDefaults standardUserDefaults] boolForKey:@"IsAskingToSaveEditedGames"])) {
[super updateChangeCount:change];
}
}
- (void) playerDidResign:(GobanPlayer *) aPlayer
{
//[self updateChangeCount:1];
[[self opponentTo:aPlayer] opponentDidResign];
[game resign];
if ([game isOver]) {
[self showGameOver];
[self finishGame];
}
}
- (void) playerDidUndo:(GobanPlayer *) aPlayer
{
[game undoPreviousMove];
[game undoPreviousMove];
[[self opponentTo:aPlayer] opponentDidUndo];
}
- (void) player:(GobanPlayer *) aPlayer didPlayMove:(GoMove *) aMove
{
NSException *exception = [game exceptionForMove:aMove];
if (exception != nil) {
NSBeginAlertSheet (
NSLocalizedString(@"Illegal Move", @""),
nil,
nil,
nil,
[self window],
self,
@selector(exceptionSheetDidEnd:returnCode:contextInfo:),
(SEL) 0,
NULL,
[exception reason]
);
}
else {
[self updateChangeCount:1];
[game playMove:aMove];
[aPlayer makeSoundForMove:aMove];
if ([game isAtari]) {
[aPlayer signalAtari];
}
[[self opponentTo:aPlayer] opponentDidPlayMove:aMove];
if ([game isOver]) {
[self showGameOver];
[self finishGame];
}
}
}
- (void) startSheetDidEnd:(NSWindow *)sheet returnCode:(int)returnCode contextInfo:(void *)contextInfo
{
if (returnCode == NSOKButton) {
[self startOrResume:self];
}
}
- (IBAction) playStone:(id) sender
{
GoPoint *playedPoint = [grid selectedPoint];
if (playedPoint != nil) {
if (![game isReady]) {
[game placeHandicapStoneAtPoint:playedPoint];
}
else if (![game isAtLastMove]) {
[game gotoNextMove];
}
else if (![game isOver]) {
GobanPlayer *player = [self nextPlayer];
if ([player isInteractive]) {
if (![game isStarted]) {
[self startOrResume:nil];
}
[self player:player didPlayMove:[game moveForColor:[player color] atPoint:playedPoint]];
}
else if (![game isStarted]) {
NSBeginAlertSheet (
NSLocalizedString (@"First Move", @""),
NSLocalizedString(@"Make First Move", @""),
NSLocalizedString(@"Cancel", @""),
nil,
[self window],
self,
@selector(startSheetDidEnd:returnCode:contextInfo:),
(SEL) 0,
NULL,
NSLocalizedString(@"Not your turn to make first move", @"")
);
}
}
else if ([grid isShowingTerritory]) {
[game toggleIsAliveAtPoint:playedPoint];
}
}
}
- (BOOL) validatePass:(id <SenValidatedUserInterfaceItem>) anItem
{
return [game isStarted] && ![game isOver] && [[self nextPlayer] isInteractive];
}
- (IBAction) pass:(id) sender
{
GobanPlayer *player = [self nextPlayer];
GoMove *move = [game passMoveForColor:[player color]];
[self player:player didPlayMove:move];
}
- (BOOL) isResignationValid
{
return [game isStarted] && ![game isOver] && [[self nextPlayer] isInteractive];
}
- (BOOL) validateResign:(id <SenValidatedUserInterfaceItem>) anItem
{
return [self isResignationValid];
}
- (void) resignSheetDidEnd:(NSWindow *)sheet returnCode:(int)returnCode contextInfo:(void *)contextInfo
{
if (returnCode == NSOKButton) {
GobanPlayer *player = [self nextPlayer];
[self playerDidResign:player];
}
}
- (IBAction) resign:(id) sender
{
NSBeginCriticalAlertSheet (
NSLocalizedString (@"Resign", @""),
NSLocalizedString(@"Resign", @""),
NSLocalizedString(@"Cancel", @""),
nil,
[self window],
self,
@selector(resignSheetDidEnd:returnCode:contextInfo:),
(SEL) 0,
NULL,
NSLocalizedString(@"Do you really want to resign this game?", @"")
);
}
- (IBAction) undoMove:(id) sender
{
[self playerDidUndo:[self nextPlayer]];
}
- (IBAction) showPreviousMove:(id) sender
{
[game gotoPreviousMove];
}
- (IBAction) showNextMove:(id) sender
{
[game gotoNextMove];
}
- (IBAction) showGameStart:(id) sender
{
[game gotoStart];
}
- (IBAction) showGameEnd:(id) sender
{
[game gotoEnd];
}
- (IBAction) startOrResume:(id) sender
{
[whitePlayer playGame:game withColor:[GoSymbol white]];
[blackPlayer playGame:game withColor:[GoSymbol black]];
}
- (void) finishGame
{
[whitePlayer finishGame];
[blackPlayer finishGame];
RELEASE (whitePlayer);
RELEASE (blackPlayer);
}
- (IBAction) showInfo:(id) sender
{
[inspector toggleInfo];
}
- (BOOL) validateShowInfo:(id <SenValidatedUserInterfaceItem>) anItem
{
return [inspector validateShowInfo:(id <SenValidatedUserInterfaceItem>) anItem];
}
- (BOOL) isShowingTerritory
{
return [grid isShowingTerritory];
}
- (IBAction) showTerritory:(id) sender
{
if ([grid isShowingTerritory]) {
[grid hideTerritory];
}
else {
[grid showTerritory];
}
[self redisplay];
}
- (IBAction) showLabels:(id) sender
{
if ([grid isShowingLabels]) {
[grid hideLabels];
}
else {
[grid showLabels];
}
[self redisplay];
}
- (GobanPlayer *) whitePlayer
{
return whitePlayer;
}
- (GobanPlayer *) blackPlayer
{
return blackPlayer;
}
- (BOOL) validateUndoMove:(id <SenValidatedUserInterfaceItem>) anItem
{
return [game isStarted] && ![game isOver] && [[self nextPlayer] isInteractive];
}
- (BOOL) isStartOrResumeValid
{
return (![game isStarted]) && (![[self nextPlayer] isInteractive]) && (![[self nextPlayer] isPlaying]);
}
- (BOOL) validateStartOrResume: (id <SenValidatedUserInterfaceItem>) anItem
{
return [self isStartOrResumeValid];
}
- (BOOL) validateShowTerritory: (id <SenValidatedUserInterfaceItem>) anItem
{
[anItem setTitle:NSLocalizedString (([grid isShowingTerritory]) ? @"Hide Territories" : @"Show Territories", @"")];
return [game isOver] && ![game isResigned];
}
- (BOOL) validateShowLabels: (id <SenValidatedUserInterfaceItem>) anItem
{
[anItem setTitle:NSLocalizedString (([grid isShowingLabels]) ? @"Hide Labels" : @"Show Labels", @"")];
return YES;
}
- (BOOL) validateUserInterfaceItem: (id <SenValidatedUserInterfaceItem>) anItem
{
SEL action = [anItem action];
if (action != 0) {
NSString *actionSelectorString = NSStringFromSelector(action);
NSString *validationSelectorString = [NSString stringWithFormat:@"validate%@%@",
[[actionSelectorString substringToIndex:1] capitalizedString],
[actionSelectorString substringFromIndex:1]];
SEL validationSelector = NSSelectorFromString(validationSelectorString);
if ([self respondsToSelector:validationSelector]) {
return ([self performSelector:validationSelector withObject:anItem] != nil);
}
}
return [super validateUserInterfaceItem:anItem];
}
- (void) reset
{
[inspector setInspected:nil];
[self finishGame];
RELEASE (scorer);
[self setGame:[[[GoGame alloc] init] autorelease]];
[self setDefaultPlayers];
[game rulesChanged];
[inspector setInspected:self];
[inspector showInfo:@"Rules" shouldOpenDrawer:NO];
[grid setGame:game];
}
- (void) resetSheetDidEnd:(NSWindow *)sheet returnCode:(int)returnCode contextInfo:(void *)contextInfo
{
if (returnCode == NSOKButton) {
[self reset];
}
}
- (IBAction) reset:(id) sender
{
if ([self isResignationValid]) {
NSBeginCriticalAlertSheet (
NSLocalizedString (@"Resign", @""),
NSLocalizedString(@"Resign", @""),
NSLocalizedString(@"Cancel", @""),
nil,
[self window],
self,
@selector(resetSheetDidEnd:returnCode:contextInfo:),
(SEL) 0,
NULL,
NSLocalizedString(@"Do you really want to resign this game?", @"")
);
}
else {
[self reset];
}
}
- (void) windowDidResize:(NSNotification *)notification
{
if ([notification object] == [self window]) {
[inspector resize];
}
}
- (BOOL) windowShouldZoom:(NSWindow *)sender toFrame:(NSRect)newFrame
{
return YES;
}
- (NSRect)windowWillUseStandardFrame:(NSWindow *)sender defaultFrame:(NSRect)defaultFrame
{
return NSMakeRect (defaultFrame.origin.x, defaultFrame.origin.y, defaultFrame.size.height / 1.1, defaultFrame.size.height);
}
- (NSSize) windowWillResize:(NSWindow *)sender toSize:(NSSize)proposedFrameSize
{
return proposedFrameSize;
/*
NSRect proposedRect = NSMakeRect (0, 0, proposedFrameSize.width, proposedFrameSize.height);
NSRect contentRect = [NSWindow contentRectForFrameRect:proposedRect styleMask:[sender styleMask]];
NSSize adjustedContentSize = [grid windowWillResizeToSize:contentRect.size];
NSRect adjustedContentRect = NSMakeRect (0, 0, adjustedContentSize.width, adjustedContentSize.height);
return [NSWindow frameRectForContentRect:adjustedContentRect styleMask:[sender styleMask]].size;
*/
}
- (BOOL) isProgramActive
{
return [self isProgramScoring] || [self isProgramThinking];
}
- (NSString *) statusString
{
if ([game isOver]) {
if ([game isScoring]) {
return NSLocalizedString(@"Scoring in progress", @"");
}
else if ([game isResigned]) {
return NSLocalizedString([game scoreString], @"");
}
else {
GoPosition *position = [game scorePosition];
float whiteScore = [position whiteScore];
float blackScore = [position blackScore];
if (whiteScore > blackScore) {
return [NSString stringWithFormat:NSLocalizedString(@"White wins by %.1f", @""), whiteScore - blackScore];
}
else if (blackScore > whiteScore) {
return [NSString stringWithFormat:NSLocalizedString(@"Black wins by %.1f", @""), blackScore - whiteScore];
}
else {
return NSLocalizedString(@"Tie game", @"");
}
}
}
else if ([self isProgramThinking]) {
return [NSString stringWithFormat:NSLocalizedString(@"%@ thinking", @""), [self nextPlayerName]];
}
else {
return [NSString stringWithFormat:NSLocalizedString(@"%@'s turn", @""), [self nextPlayerName]];
}
}
@end