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