Source/iPhoneAppDelegate.m
author Jens Alfke <jens@mooseyard.com>
Mon Jul 14 21:00:15 2008 -0700 (2008-07-14)
changeset 16 28392c9a969f
parent 9 a59acc683080
permissions -rwxr-xr-x
Tweaks and fixes, including renaming Game's "board" property/ivar to "table", which is less confusing.
Released as part of Your Move 1.0a2.
     1 //
     2 //  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 "iPhoneAppDelegate.h"
    10 #import "BoardUIView.h"
    11 #import "Game.h"
    12 #import "Player.h"
    13 #import "QuartzUtils.h"
    14 #import "GGBUtils.h"
    15 
    16 
    17 #if 0
    18 // Temporary HACK to fix logging problem in beta 6 iPhone OS
    19 extern void _NSSetLogCStringFunction(void (*)(const char *string, unsigned length, BOOL withSyslogBanner));
    20 static void PrintNSLogMessage(const char *string, unsigned length, BOOL withSyslogBanner)
    21 {
    22 	puts(string);
    23 }
    24 static void HackNSLog(void) __attribute__((constructor));
    25 static void HackNSLog(void)
    26 {
    27 	_NSSetLogCStringFunction(PrintNSLogMessage);
    28 }
    29 #endif
    30 
    31 
    32 @implementation GGB_iPhoneAppDelegate
    33 
    34 
    35 @synthesize window=_window;
    36 @synthesize contentView=_contentView;
    37 @synthesize headline=_headline;
    38 
    39 
    40 - (void)applicationDidFinishLaunching:(UIApplication *)application 
    41 {	
    42     // Create window
    43     self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    44     _window.layer.backgroundColor = GetCGPatternNamed(@"Background.png");
    45     
    46     // Set up content view
    47     CGRect rHeadline,rContent;
    48     CGRectDivide([[UIScreen mainScreen] applicationFrame],
    49                  &rHeadline, &rContent, 35, CGRectMinYEdge);
    50     
    51     self.contentView = [[[BoardUIView alloc] initWithFrame: rContent] autorelease];
    52     [_window addSubview: _contentView];
    53     
    54     self.headline = [[[UILabel alloc] initWithFrame: rHeadline] autorelease];
    55     _headline.backgroundColor = nil;
    56     _headline.opaque = NO;
    57     _headline.textAlignment = UITextAlignmentCenter;
    58     _headline.font = [UIFont boldSystemFontOfSize: 20];
    59     _headline.minimumFontSize = 14;
    60     _headline.adjustsFontSizeToFitWidth = YES;
    61     [_window addSubview: _headline];
    62     
    63     // Start game:
    64     [self startGameNamed: @"CheckersGame"];
    65     
    66     // Show window
    67     [_window makeKeyAndVisible];
    68 }
    69 
    70 
    71 - (void)dealloc 
    72 {
    73     [_contentView release];
    74     [_headline release];
    75     [_window release];
    76     [super dealloc];
    77 }
    78 
    79 
    80 - (void) startGameNamed: (NSString*)gameClassName
    81 {
    82     Game *game = _contentView.game;
    83     [game removeObserver: self  forKeyPath: @"currentPlayer"];
    84     [game removeObserver: self forKeyPath: @"winner"];
    85     
    86     if( gameClassName == nil )
    87         gameClassName = [[game class] description];
    88     
    89     [_contentView startGameNamed: gameClassName];
    90     
    91     game = _contentView.game;
    92     [game addObserver: self 
    93            forKeyPath: @"currentPlayer"
    94               options: NSKeyValueObservingOptionInitial
    95               context: NULL];
    96     [game addObserver: self
    97            forKeyPath: @"winner"
    98               options: 0 
    99               context: NULL];
   100 }
   101 
   102 
   103 - (void)observeValueForKeyPath:(NSString *)keyPath 
   104                       ofObject:(id)object 
   105                         change:(NSDictionary *)change
   106                        context:(void *)context
   107 {
   108     Game *game = _contentView.game;
   109     if( object == game ) {
   110         Player *p = game.winner;
   111         NSString *msg;
   112         if( p ) {
   113             PlaySound(@"Sosumi");
   114             msg = @"%@ wins!";
   115         } else {
   116             p = game.currentPlayer;
   117             msg = @"Your turn, %@";
   118         }
   119         _headline.text = [NSString stringWithFormat: msg, p.name];
   120         
   121         if( game.winner ) {
   122             UIAlertView *alert;
   123             alert = [[UIAlertView alloc] initWithTitle: msg
   124                                                message: @"Congratulations!"
   125                                               delegate:self
   126                                      cancelButtonTitle:nil 
   127                                      otherButtonTitles:nil];
   128             [alert show];
   129             [alert release];
   130         }            
   131     }
   132 }
   133 
   134 
   135 - (void)alertView:(UIAlertView *)modalView didDismissWithButtonIndex:(NSInteger)buttonIndex;
   136 {
   137     // Start new game:
   138     [self startGameNamed: nil];
   139 }
   140 
   141 
   142 @end