jens@1: // jens@2: // iPhoneAppDelegate.m jens@1: // GGB-iPhone jens@1: // jens@1: // Created by Jens Alfke on 3/7/08. jens@1: // Copyright __MyCompanyName__ 2008. All rights reserved. jens@1: // jens@1: jens@2: #import "iPhoneAppDelegate.h" jens@1: #import "BoardUIView.h" jens@1: #import "Game.h" jens@1: #import "QuartzUtils.h" jens@1: #import "GGBUtils.h" jens@1: jens@1: jens@1: @implementation GGB_iPhoneAppDelegate jens@1: jens@1: jens@1: @synthesize window=_window; jens@1: @synthesize contentView=_contentView; jens@1: @synthesize headline=_headline; jens@1: jens@1: jens@1: - (void)applicationDidFinishLaunching:(UIApplication *)application jens@1: { jens@1: // Create window jens@1: self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; jens@1: _window.layer.backgroundColor = GetCGPatternNamed(@"Background.png"); jens@1: jens@1: // Set up content view jens@1: CGRect rHeadline,rContent; jens@1: CGRectDivide([[UIScreen mainScreen] applicationFrame], jens@1: &rHeadline, &rContent, 35, CGRectMinYEdge); jens@1: jens@1: self.contentView = [[[BoardUIView alloc] initWithFrame: rContent] autorelease]; jens@1: [_window addSubview: _contentView]; jens@1: jens@1: self.headline = [[[UILabel alloc] initWithFrame: rHeadline] autorelease]; jens@1: _headline.backgroundColor = nil; jens@1: _headline.opaque = NO; jens@1: _headline.textAlignment = UITextAlignmentCenter; jens@1: _headline.font = [UIFont boldSystemFontOfSize: 20]; jens@1: _headline.minimumFontSize = 14; jens@1: _headline.adjustsFontSizeToFit = YES; jens@1: [_window addSubview: _headline]; jens@1: jens@1: // Start game: jens@1: [self startGameNamed: @"TicTacToeGame"]; jens@1: jens@1: // Show window jens@1: [_window makeKeyAndVisible]; jens@1: } jens@1: jens@1: jens@1: - (void)dealloc jens@1: { jens@1: [_contentView release]; jens@1: [_headline release]; jens@1: [_window release]; jens@1: [super dealloc]; jens@1: } jens@1: jens@1: jens@1: - (void) startGameNamed: (NSString*)gameClassName jens@1: { jens@1: Game *game = _contentView.game; jens@1: [game removeObserver: self forKeyPath: @"currentPlayer"]; jens@1: [game removeObserver: self forKeyPath: @"winner"]; jens@1: jens@1: if( gameClassName == nil ) jens@1: gameClassName = [[game class] className]; jens@1: jens@1: [_contentView startGameNamed: gameClassName]; jens@1: jens@1: game = _contentView.game; jens@1: [game addObserver: self jens@1: forKeyPath: @"currentPlayer" jens@1: options: NSKeyValueObservingOptionInitial jens@1: context: NULL]; jens@1: [game addObserver: self jens@1: forKeyPath: @"winner" jens@1: options: 0 jens@1: context: NULL]; jens@1: } jens@1: jens@1: jens@1: - (void)observeValueForKeyPath:(NSString *)keyPath jens@1: ofObject:(id)object jens@1: change:(NSDictionary *)change jens@1: context:(void *)context jens@1: { jens@1: Game *game = _contentView.game; jens@1: if( object == game ) { jens@1: Player *p = game.winner; jens@1: NSString *msg; jens@1: if( p ) { jens@1: PlaySound(@"Sosumi"); jens@1: msg = @"%@ wins!"; jens@1: } else { jens@1: p = game.currentPlayer; jens@1: msg = @"Your turn, %@"; jens@1: } jens@1: _headline.text = [NSString stringWithFormat: msg, p.name]; jens@1: jens@1: if( game.winner ) { jens@1: UIAlertView *alert; jens@1: alert = [[UIAlertView alloc] initWithTitle: msg jens@1: message: @"Congratulations!" jens@1: delegate:self jens@1: defaultButton:@"OK" jens@1: cancelButton:nil otherButtons:nil]; jens@1: [alert show]; jens@1: [alert release]; jens@1: } jens@1: } jens@1: } jens@1: jens@1: jens@1: - (void)modalView:(UIModalView *)modalView didDismissWithButtonIndex:(NSInteger)buttonIndex; jens@1: { jens@1: // Start new game: jens@1: [self startGameNamed: nil]; jens@1: } jens@1: jens@1: jens@1: @end