2 // GGB_iPhoneAppDelegate.m
5 // Created by Jens Alfke on 3/7/08.
6 // Copyright __MyCompanyName__ 2008. All rights reserved.
9 #import "GGB_iPhoneAppDelegate.h"
10 #import "BoardUIView.h"
12 #import "QuartzUtils.h"
16 @implementation GGB_iPhoneAppDelegate
19 @synthesize window=_window;
20 @synthesize contentView=_contentView;
21 @synthesize headline=_headline;
24 - (void)applicationDidFinishLaunching:(UIApplication *)application
27 self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
28 _window.layer.backgroundColor = GetCGPatternNamed(@"Background.png");
30 // Set up content view
31 CGRect rHeadline,rContent;
32 CGRectDivide([[UIScreen mainScreen] applicationFrame],
33 &rHeadline, &rContent, 35, CGRectMinYEdge);
35 self.contentView = [[[BoardUIView alloc] initWithFrame: rContent] autorelease];
36 [_window addSubview: _contentView];
38 self.headline = [[[UILabel alloc] initWithFrame: rHeadline] autorelease];
39 _headline.backgroundColor = nil;
40 _headline.opaque = NO;
41 _headline.textAlignment = UITextAlignmentCenter;
42 _headline.font = [UIFont boldSystemFontOfSize: 20];
43 _headline.minimumFontSize = 14;
44 _headline.adjustsFontSizeToFit = YES;
45 [_window addSubview: _headline];
48 [self startGameNamed: @"TicTacToeGame"];
51 [_window makeKeyAndVisible];
57 [_contentView release];
64 - (void) startGameNamed: (NSString*)gameClassName
66 Game *game = _contentView.game;
67 [game removeObserver: self forKeyPath: @"currentPlayer"];
68 [game removeObserver: self forKeyPath: @"winner"];
70 if( gameClassName == nil )
71 gameClassName = [[game class] className];
73 [_contentView startGameNamed: gameClassName];
75 game = _contentView.game;
76 [game addObserver: self
77 forKeyPath: @"currentPlayer"
78 options: NSKeyValueObservingOptionInitial
80 [game addObserver: self
87 - (void)observeValueForKeyPath:(NSString *)keyPath
89 change:(NSDictionary *)change
90 context:(void *)context
92 Game *game = _contentView.game;
93 if( object == game ) {
94 Player *p = game.winner;
100 p = game.currentPlayer;
101 msg = @"Your turn, %@";
103 _headline.text = [NSString stringWithFormat: msg, p.name];
107 alert = [[UIAlertView alloc] initWithTitle: msg
108 message: @"Congratulations!"
111 cancelButton:nil otherButtons:nil];
119 - (void)modalView:(UIModalView *)modalView didDismissWithButtonIndex:(NSInteger)buttonIndex;
122 [self startGameNamed: nil];