Source/Game.m
author Jens Alfke <jens@mooseyard.com>
Mon Mar 10 17:32:04 2008 -0700 (2008-03-10)
changeset 2 7b0441db81e5
parent 0 e9f7ba4718e1
child 3 40d225cf9c43
permissions -rw-r--r--
Oops, needed to fix an #include
     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 "Game.h"
    24 #import "Bit.h"
    25 
    26 
    27 @interface Game ()
    28 @property (copy) NSArray *players;
    29 @property (assign) Player *currentPlayer, *winner;
    30 @end
    31 
    32 
    33 /**  WARNING: THIS CODE REQUIRES GARBAGE COLLECTION!
    34  **  This sample application uses Objective-C 2.0 garbage collection.
    35  **  Therefore, the source code in this file does NOT perform manual object memory management.
    36  **  If you reuse any of this code in a process that isn't garbage collected, you will need to
    37  **  add all necessary retain/release/autorelease calls, and implement -dealloc methods,
    38  **  otherwise unpleasant leakage will occur!
    39  **/
    40 
    41 
    42 @implementation Game
    43 
    44 
    45 + (NSString*) displayName
    46 {
    47     NSString* name = [self description];
    48     if( [name hasSuffix: @"Game"] )
    49         name = [name substringToIndex: name.length-4];
    50     return name;
    51 }
    52 
    53 
    54 - (id) initWithBoard: (GGBLayer*)board
    55 {
    56     self = [super init];
    57     if (self != nil) {
    58         _board = [board retain];
    59         // Store a pointer to myself as the value of the "Game" property
    60         // of my root layer. (CALayers can have arbitrary KV properties stored into them.)
    61         // This is used by the -[CALayer game] category method defined below, to find the Game.
    62         [board setValue: self forKey: @"Game"];
    63     }
    64     return self;
    65 }
    66 
    67 
    68 - (void) dealloc
    69 {
    70     [_board release];
    71     [_players release];
    72     [super dealloc];
    73 }
    74 
    75 
    76 @synthesize players=_players, currentPlayer=_currentPlayer, winner=_winner;
    77 
    78 
    79 - (void) setNumberOfPlayers: (unsigned)n
    80 {
    81     NSMutableArray *players = [NSMutableArray arrayWithCapacity: n];
    82     for( int i=1; i<=n; i++ ) {
    83         Player *player = [[Player alloc] initWithGame: self];
    84         player.name = [NSString stringWithFormat: @"Player %i",i];
    85         [players addObject: player];
    86         [player release];
    87     }
    88     self.winner = nil;
    89     self.currentPlayer = nil;
    90     self.players = players;
    91 }
    92 
    93 
    94 - (void) nextPlayer
    95 {
    96     if( ! _currentPlayer ) {
    97         NSLog(@"*** The %@ Begins! ***", self.class);
    98         self.currentPlayer = [_players objectAtIndex: 0];
    99     } else {
   100         self.currentPlayer = _currentPlayer.nextPlayer;
   101     }
   102     NSLog(@"Current player is %@",_currentPlayer);
   103 }
   104 
   105 
   106 - (void) endTurn
   107 {
   108     NSLog(@"--- End of turn");
   109     Player *winner = [self checkForWinner];
   110     if( winner ) {
   111         NSLog(@"*** The %@ Ends! The winner is %@ ! ***", self.class, winner);
   112         self.winner = winner;
   113     } else
   114         [self nextPlayer];
   115 }
   116 
   117 
   118 #pragma mark -
   119 #pragma mark GAMEPLAY METHODS TO BE OVERRIDDEN:
   120 
   121 
   122 - (BOOL) canBit: (Bit*)bit moveFrom: (id<BitHolder>)src
   123 {
   124     return YES;
   125 }
   126 
   127 - (BOOL) canBit: (Bit*)bit moveFrom: (id<BitHolder>)src to: (id<BitHolder>)dst
   128 {
   129     return YES;
   130 }
   131 
   132 - (void) bit: (Bit*)bit movedFrom: (id<BitHolder>)src to: (id<BitHolder>)dst
   133 {
   134     [self endTurn];
   135 }
   136 
   137 - (BOOL) clickedBit: (Bit*)bit
   138 {
   139     return YES;
   140 }
   141 
   142 - (Player*) checkForWinner
   143 {
   144     return nil;
   145 }
   146 
   147 
   148 @end
   149 
   150 
   151 
   152 
   153 @implementation Player
   154 
   155 
   156 - (id) initWithGame: (Game*)game
   157 {
   158     self = [super init];
   159     if (self != nil) {
   160         _game = game;
   161     }
   162     return self;
   163 }
   164 
   165 
   166 @synthesize game=_game, name=_name;
   167 
   168 - (BOOL) isCurrent      {return self == _game.currentPlayer;}
   169 - (BOOL) isFriendly     {return self == _game.currentPlayer;}   // could be overridden for games with partners
   170 - (BOOL) isUnfriendly   {return ! self.friendly;}
   171 
   172 - (int) index
   173 {
   174     return [_game.players indexOfObjectIdenticalTo: self];
   175 }
   176 
   177 - (Player*) nextPlayer
   178 {
   179     return [_game.players objectAtIndex: (self.index+1) % _game.players.count];
   180 }
   181 
   182 - (Player*) previousPlayer
   183 {
   184     return [_game.players objectAtIndex: (self.index-1) % _game.players.count];
   185 }
   186 
   187 - (NSString*) description
   188 {
   189     return [NSString stringWithFormat: @"%@[%@]", self.class,self.name];
   190 }
   191 
   192 @end
   193 
   194 
   195 
   196 
   197 @implementation CALayer (Game)
   198 
   199 - (Game*) game
   200 {
   201     // The Game object stores a pointer to itself as the value of the "Game" property
   202     // of its root layer. (CALayers can have arbitrary KV properties stored into them.)
   203     for( CALayer *layer = self; layer; layer=layer.superlayer ) {
   204         Game *game = [layer valueForKey: @"Game"];
   205         if( game )
   206             return game;
   207     }
   208     NSAssert1(NO,@"Couldn't look up Game from %@",self);
   209     return nil;
   210 }
   211 
   212 @end