Source/iPhoneAppDelegate.m
author Jens Alfke <jens@mooseyard.com>
Sun Mar 16 15:06:47 2008 -0700 (2008-03-16)
changeset 7 428a194e3e59
parent 3 40d225cf9c43
child 8 45c82a071aca
permissions -rwxr-xr-x
Game class now tracks board state and moves, as strings, and can step through its history.
Fixed another bug in Go (you could drag your captured stones back to the board!)
     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 "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     for( NSString *family in [UIFont familyNames] )
    27         NSLog(@"%@: (%@)", family, [[UIFont fontNamesForFamilyName: family] componentsJoinedByString: @", "]);
    28         
    29     // Create window
    30     self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    31     _window.layer.backgroundColor = GetCGPatternNamed(@"Background.png");
    32     
    33     // Set up content view
    34     CGRect rHeadline,rContent;
    35     CGRectDivide([[UIScreen mainScreen] applicationFrame],
    36                  &rHeadline, &rContent, 35, CGRectMinYEdge);
    37     
    38     self.contentView = [[[BoardUIView alloc] initWithFrame: rContent] autorelease];
    39     [_window addSubview: _contentView];
    40     
    41     self.headline = [[[UILabel alloc] initWithFrame: rHeadline] autorelease];
    42     _headline.backgroundColor = nil;
    43     _headline.opaque = NO;
    44     _headline.textAlignment = UITextAlignmentCenter;
    45     _headline.font = [UIFont boldSystemFontOfSize: 20];
    46     _headline.minimumFontSize = 14;
    47     _headline.adjustsFontSizeToFit = YES;
    48     [_window addSubview: _headline];
    49     
    50     // Start game:
    51     [self startGameNamed: @"KlondikeGame"];
    52     
    53     // Show window
    54     [_window makeKeyAndVisible];
    55 }
    56 
    57 
    58 - (void)dealloc 
    59 {
    60     [_contentView release];
    61     [_headline release];
    62     [_window release];
    63     [super dealloc];
    64 }
    65 
    66 
    67 - (void) startGameNamed: (NSString*)gameClassName
    68 {
    69     Game *game = _contentView.game;
    70     [game removeObserver: self  forKeyPath: @"currentPlayer"];
    71     [game removeObserver: self forKeyPath: @"winner"];
    72     
    73     if( gameClassName == nil )
    74         gameClassName = [[game class] className];
    75     
    76     [_contentView startGameNamed: gameClassName];
    77     
    78     game = _contentView.game;
    79     [game addObserver: self 
    80            forKeyPath: @"currentPlayer"
    81               options: NSKeyValueObservingOptionInitial
    82               context: NULL];
    83     [game addObserver: self
    84            forKeyPath: @"winner"
    85               options: 0 
    86               context: NULL];
    87 }
    88 
    89 
    90 - (void)observeValueForKeyPath:(NSString *)keyPath 
    91                       ofObject:(id)object 
    92                         change:(NSDictionary *)change
    93                        context:(void *)context
    94 {
    95     Game *game = _contentView.game;
    96     if( object == game ) {
    97         Player *p = game.winner;
    98         NSString *msg;
    99         if( p ) {
   100             PlaySound(@"Sosumi");
   101             msg = @"%@ wins!";
   102         } else {
   103             p = game.currentPlayer;
   104             msg = @"Your turn, %@";
   105         }
   106         _headline.text = [NSString stringWithFormat: msg, p.name];
   107         
   108         if( game.winner ) {
   109             UIAlertView *alert;
   110             alert = [[UIAlertView alloc] initWithTitle: msg
   111                                                message: @"Congratulations!"
   112                                               delegate:self
   113                                          defaultButton:@"OK" 
   114                                           cancelButton:nil otherButtons:nil];
   115             [alert show];
   116             [alert release];
   117         }            
   118     }
   119 }
   120 
   121 
   122 - (void)modalView:(UIModalView *)modalView didDismissWithButtonIndex:(NSInteger)buttonIndex;
   123 {
   124     // Start new game:
   125     [self startGameNamed: nil];
   126 }
   127 
   128 
   129 @end