Source/DemoBoardView.m
author Jens Alfke <jens@mooseyard.com>
Sun Mar 16 15:06:47 2008 -0700 (2008-03-16)
changeset 7 428a194e3e59
parent 1 3eb7be1dd7b6
child 8 45c82a071aca
permissions -rw-r--r--
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 /*  This code is based on Apple's "GeekGameBoard" sample code, version 1.0.
     2     http://developer.apple.com/samplecode/GeekGameBoard/
     3     Copyright © 2007 Apple Inc. Copyright © 2008 Jens Alfke. All Rights Reserved.
     4 
     5     Redistribution and use in source and binary forms, with or without modification, are permitted
     6     provided that the following conditions are met:
     7 
     8     * Redistributions of source code must retain the above copyright notice, this list of conditions
     9       and the following disclaimer.
    10     * Redistributions in binary form must reproduce the above copyright notice, this list of
    11       conditions and the following disclaimer in the documentation and/or other materials provided
    12       with the distribution.
    13 
    14     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
    15     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 
    16     FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRI-
    17     BUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
    18     (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
    19     PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
    20     CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 
    21     THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    22 */
    23 #import "DemoBoardView.h"
    24 #import "Game.h"
    25 #import "GGBTextLayer.h"
    26 #import "QuartzUtils.h"
    27 
    28 
    29 /**  WARNING: THIS CODE REQUIRES GARBAGE COLLECTION!
    30  **  This sample application uses Objective-C 2.0 garbage collection.
    31  **  Therefore, the source code in this file does NOT perform manual object memory management.
    32  **  If you reuse any of this code in a process that isn't garbage collected, you will need to
    33  **  add all necessary retain/release/autorelease calls, and implement -dealloc methods,
    34  **  otherwise unpleasant leakage will occur!
    35  **/
    36 
    37 
    38 @implementation DemoBoardView
    39 
    40 
    41 /** Class names of available games */
    42 static NSString* const kMenuGameNames[] = {@"KlondikeGame", @"CheckersGame", @"HexchequerGame",
    43                                            @"TicTacToeGame", @"GoGame"};
    44 
    45 /** Class name of the current game. */
    46 static NSString* sCurrentGameName = @"CheckersGame";
    47 
    48 
    49 - (void) startGameNamed: (NSString*)gameClassName
    50 {
    51     [super startGameNamed: gameClassName];
    52     
    53     Game *game = self.game;
    54     [game addObserver: self 
    55            forKeyPath: @"currentPlayer"
    56               options: NSKeyValueObservingOptionInitial
    57               context: NULL];
    58     [game addObserver: self
    59            forKeyPath: @"winner"
    60               options: 0 
    61               context: NULL];
    62     
    63     self.window.title = [(id)[game class] displayName];
    64 }
    65 
    66 
    67 - (CGRect) gameBoardFrame
    68 {
    69     CGRect bounds = [super gameBoardFrame];
    70     bounds.size.height -= 32;                   // Leave room for headline
    71     return CGRectInset(bounds,4,4);
    72 }
    73 
    74 
    75 - (void) awakeFromNib
    76 {
    77     srandomdev();
    78     
    79     [self registerForDraggedTypes: [NSImage imagePasteboardTypes]];
    80     [self registerForDraggedTypes: [NSArray arrayWithObject: NSFilenamesPboardType]];
    81     
    82     CGRect bounds = self.layer.bounds;
    83     self.layer.backgroundColor = GetCGPatternNamed(@"Background.png");
    84         
    85     bounds.size.height -= 32;
    86     _headline = [GGBTextLayer textLayerInSuperlayer: self.layer
    87                                            withText: nil
    88                                            fontSize: 24
    89                                           alignment: kCALayerWidthSizable | kCALayerMinYMargin];
    90     
    91     [self startGameNamed: sCurrentGameName];
    92     
    93     [_turnSlider bind: @"value"    toObject: self withKeyPath: @"game.currentTurn" options: nil];
    94 }
    95 
    96 
    97 - (void) startGameFromMenu: (id)sender
    98 {
    99     sCurrentGameName = kMenuGameNames[ [sender tag] ];
   100     [self startGameNamed: sCurrentGameName];
   101 }
   102 
   103 
   104 - (void)observeValueForKeyPath:(NSString *)keyPath 
   105                       ofObject:(id)object 
   106                         change:(NSDictionary *)change
   107                        context:(void *)context
   108 {
   109     Game *game = self.game;
   110     if( object == game ) {
   111         NSLog(@"maxTurn = %u, currentTurn=%u", self.game.maxTurn,self.game.currentTurn);
   112         _turnSlider.maxValue = self.game.maxTurn;
   113         _turnSlider.numberOfTickMarks = self.game.maxTurn+1;
   114         
   115         Player *p = game.winner;
   116         NSString *msg;
   117         if( p ) {
   118             [[NSSound soundNamed: @"Sosumi"] play];
   119             msg = @"%@ wins! Congratulations!";
   120         } else {
   121             p = game.currentPlayer;
   122             msg = @"Your turn, %@";
   123             NSLog(@"Game state = '%@'", self.game.stateString);
   124         }
   125         _headline.string = [NSString stringWithFormat: msg, p.name];
   126     }
   127 }
   128 
   129 
   130 - (IBAction) enterFullScreen: (id)sender
   131 {
   132     [super enterFullScreen: sender];
   133     [self startGameNamed: sCurrentGameName];        // restart game so it'll use the new size
   134 }
   135 
   136 
   137 #pragma mark -
   138 #pragma mark NSAPPLICATION DELEGATE:
   139 
   140 
   141 - (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)sender
   142 {
   143     return YES;
   144 }
   145 
   146 @end