Added ball graphics to Xcode project, which fixes the Go game. Fixed Tic-Tac-Toe. Fixed an exception in the demo app when changing games.
1 /* This code is based on Apple's "GeekGameBoard" sample code, version 1.0.
2 http://developer.apple.com/samplecode/GeekGameBoard/
3 Copyright © 2007 Apple Inc. Copyright © 2008 Jens Alfke. All Rights Reserved.
5 Redistribution and use in source and binary forms, with or without modification, are permitted
6 provided that the following conditions are met:
8 * Redistributions of source code must retain the above copyright notice, this list of conditions
9 and the following disclaimer.
10 * Redistributions in binary form must reproduce the above copyright notice, this list of
11 conditions and the following disclaimer in the documentation and/or other materials provided
12 with the distribution.
14 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
15 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRI-
17 BUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
19 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
20 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
21 THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 #import "DemoBoardView.h"
27 #import "GGBTextLayer.h"
28 #import "QuartzUtils.h"
31 @implementation DemoBoardView
34 /** Class names of available games */
35 static NSString* const kMenuGameNames[] = {@"KlondikeGame", @"CheckersGame", @"HexchequerGame",
36 @"TicTacToeGame", @"GoGame"};
38 /** Class name of the current game. */
39 static NSString* sCurrentGameName = @"CheckersGame";
42 - (IBAction) toggleRemoteOpponent: (id)sender
44 NSAssert(self.game.currentTurnNo==0,@"Game has already begun");
45 Player *opponent = [self.game.players objectAtIndex: 1];
46 opponent.local = !opponent.local;
50 - (void) startGameNamed: (NSString*)gameClassName
52 [self.game removeObserver: self
53 forKeyPath: @"currentPlayer"];
54 [self.game removeObserver: self
55 forKeyPath: @"winner"];
57 [super startGameNamed: gameClassName];
59 [self.game addObserver: self
60 forKeyPath: @"currentPlayer"
61 options: NSKeyValueObservingOptionInitial
63 [self.game addObserver: self
68 self.window.title = [(id)[self.game class] displayName];
72 - (CGRect) gameBoardFrame
74 CGRect bounds = [super gameBoardFrame];
75 bounds.size.height -= 32; // Leave room for headline
76 return CGRectInset(bounds,4,4);
80 - (BOOL)canBecomeKeyView {return YES;}
81 - (BOOL)acceptsFirstResponder {return YES;}
88 // BoardView supports receiving dragged images, but you have to register for them:
89 [self registerForDraggedTypes: [NSImage imagePasteboardTypes]];
90 [self registerForDraggedTypes: [NSArray arrayWithObject: NSFilenamesPboardType]];
92 CGRect bounds = self.layer.bounds;
93 self.layer.backgroundColor = GetCGPatternNamed(@"Background.png");
95 bounds.size.height -= 32;
96 _headline = [GGBTextLayer textLayerInSuperlayer: self.layer
99 alignment: kCALayerWidthSizable | kCALayerMinYMargin];
101 [self startGameNamed: sCurrentGameName];
103 [_turnSlider bind: @"value" toObject: self withKeyPath: @"game.currentTurnNo" options: nil];
107 - (IBAction) startGameFromMenu: (id)sender
109 sCurrentGameName = kMenuGameNames[ [sender tag] ];
110 [self startGameNamed: sCurrentGameName];
114 - (void)observeValueForKeyPath:(NSString *)keyPath
116 change:(NSDictionary *)change
117 context:(void *)context
119 Game *game = self.game;
120 if( object == game ) {
121 NSLog(@"maxTurnNo = %u, currentTurnNo = %u",
122 self.game.maxTurnNo,self.game.currentTurnNo);
123 NSLog(@"Game state = '%@'", self.game.currentTurn.boardState);
125 _turnSlider.maxValue = self.game.maxTurnNo;
126 _turnSlider.numberOfTickMarks = self.game.maxTurnNo+1;
128 Player *p = game.winner;
132 [[NSSound soundNamed: @"Sosumi"] play];
133 if( self.game.local )
134 msg = @"%@ wins! Congratulations!";
136 msg = p.local ?@"You Win! Congratulations!" :@"You Lose ... :-(";
138 // Otherwise go on to the next turn:
139 p = game.currentPlayer;
140 msg = @"Your turn, %@";
142 _headline.string = [NSString stringWithFormat: msg, p.name];
147 - (IBAction) undo: (id)sender
149 if( self.game.currentTurn > 0 )
150 self.game.currentTurnNo--;
156 - (IBAction) redo: (id)sender
158 if( self.game.currentTurnNo < self.game.maxTurnNo )
159 self.game.currentTurnNo++;
166 #pragma mark NSAPPLICATION DELEGATE:
169 - (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)sender