Source/iPhoneAppDelegate.m
author Jens Alfke <jens@mooseyard.com>
Mon Mar 10 17:30:57 2008 -0700 (2008-03-10)
changeset 1 3eb7be1dd7b6
child 2 7b0441db81e5
permissions -rwxr-xr-x
Tic-tac-toe works on the iPhone simulator!
     1 //
     2 //  GGB_iPhoneAppDelegate.m
     3 //  GGB-iPhone
     4 //
     5 //  Created by Jens Alfke on 3/7/08.
     6 //  Copyright __MyCompanyName__ 2008. All rights reserved.
     7 //
     8 
     9 #import "GGB_iPhoneAppDelegate.h"
    10 #import "BoardUIView.h"
    11 #import "Game.h"
    12 #import "QuartzUtils.h"
    13 #import "GGBUtils.h"
    14 
    15 
    16 @implementation GGB_iPhoneAppDelegate
    17 
    18 
    19 @synthesize window=_window;
    20 @synthesize contentView=_contentView;
    21 @synthesize headline=_headline;
    22 
    23 
    24 - (void)applicationDidFinishLaunching:(UIApplication *)application 
    25 {	
    26     // Create window
    27     self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    28     _window.layer.backgroundColor = GetCGPatternNamed(@"Background.png");
    29     
    30     // Set up content view
    31     CGRect rHeadline,rContent;
    32     CGRectDivide([[UIScreen mainScreen] applicationFrame],
    33                  &rHeadline, &rContent, 35, CGRectMinYEdge);
    34     
    35     self.contentView = [[[BoardUIView alloc] initWithFrame: rContent] autorelease];
    36     [_window addSubview: _contentView];
    37     
    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];
    46     
    47     // Start game:
    48     [self startGameNamed: @"TicTacToeGame"];
    49     
    50     // Show window
    51     [_window makeKeyAndVisible];
    52 }
    53 
    54 
    55 - (void)dealloc 
    56 {
    57     [_contentView release];
    58     [_headline release];
    59     [_window release];
    60     [super dealloc];
    61 }
    62 
    63 
    64 - (void) startGameNamed: (NSString*)gameClassName
    65 {
    66     Game *game = _contentView.game;
    67     [game removeObserver: self  forKeyPath: @"currentPlayer"];
    68     [game removeObserver: self forKeyPath: @"winner"];
    69     
    70     if( gameClassName == nil )
    71         gameClassName = [[game class] className];
    72     
    73     [_contentView startGameNamed: gameClassName];
    74     
    75     game = _contentView.game;
    76     [game addObserver: self 
    77            forKeyPath: @"currentPlayer"
    78               options: NSKeyValueObservingOptionInitial
    79               context: NULL];
    80     [game addObserver: self
    81            forKeyPath: @"winner"
    82               options: 0 
    83               context: NULL];
    84 }
    85 
    86 
    87 - (void)observeValueForKeyPath:(NSString *)keyPath 
    88                       ofObject:(id)object 
    89                         change:(NSDictionary *)change
    90                        context:(void *)context
    91 {
    92     Game *game = _contentView.game;
    93     if( object == game ) {
    94         Player *p = game.winner;
    95         NSString *msg;
    96         if( p ) {
    97             PlaySound(@"Sosumi");
    98             msg = @"%@ wins!";
    99         } else {
   100             p = game.currentPlayer;
   101             msg = @"Your turn, %@";
   102         }
   103         _headline.text = [NSString stringWithFormat: msg, p.name];
   104         
   105         if( game.winner ) {
   106             UIAlertView *alert;
   107             alert = [[UIAlertView alloc] initWithTitle: msg
   108                                                message: @"Congratulations!"
   109                                               delegate:self
   110                                          defaultButton:@"OK" 
   111                                           cancelButton:nil otherButtons:nil];
   112             [alert show];
   113             [alert release];
   114         }            
   115     }
   116 }
   117 
   118 
   119 - (void)modalView:(UIModalView *)modalView didDismissWithButtonIndex:(NSInteger)buttonIndex;
   120 {
   121     // Start new game:
   122     [self startGameNamed: nil];
   123 }
   124 
   125 
   126 @end