Source/Game.m
author Jens Alfke <jens@mooseyard.com>
Wed Mar 12 15:51:32 2008 -0700 (2008-03-12)
changeset 6 af9b2b929b03
parent 3 40d225cf9c43
child 7 428a194e3e59
permissions -rw-r--r--
Fixed: An exception in the Go game if you mouse down on the board but then drag to the captured-pieces area and release the mouse.
     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 @implementation Game
    34 
    35 
    36 + (NSString*) displayName
    37 {
    38     NSString* name = [self description];
    39     if( [name hasSuffix: @"Game"] )
    40         name = [name substringToIndex: name.length-4];
    41     return name;
    42 }
    43 
    44 
    45 - (id) initWithBoard: (GGBLayer*)board
    46 {
    47     self = [super init];
    48     if (self != nil) {
    49         _board = [board retain];
    50         // Store a pointer to myself as the value of the "Game" property
    51         // of my root layer. (CALayers can have arbitrary KV properties stored into them.)
    52         // This is used by the -[CALayer game] category method defined below, to find the Game.
    53         [board setValue: self forKey: @"Game"];
    54     }
    55     return self;
    56 }
    57 
    58 
    59 - (void) dealloc
    60 {
    61     [_board release];
    62     [_players release];
    63     [super dealloc];
    64 }
    65 
    66 
    67 @synthesize players=_players, currentPlayer=_currentPlayer, winner=_winner;
    68 
    69 
    70 - (void) setNumberOfPlayers: (unsigned)n
    71 {
    72     NSMutableArray *players = [NSMutableArray arrayWithCapacity: n];
    73     for( int i=1; i<=n; i++ ) {
    74         Player *player = [[Player alloc] initWithGame: self];
    75         player.name = [NSString stringWithFormat: @"Player %i",i];
    76         [players addObject: player];
    77         [player release];
    78     }
    79     self.winner = nil;
    80     self.currentPlayer = nil;
    81     self.players = players;
    82 }
    83 
    84 
    85 - (void) nextPlayer
    86 {
    87     if( ! _currentPlayer ) {
    88         NSLog(@"*** The %@ Begins! ***", self.class);
    89         self.currentPlayer = [_players objectAtIndex: 0];
    90     } else {
    91         self.currentPlayer = _currentPlayer.nextPlayer;
    92     }
    93     NSLog(@"Current player is %@",_currentPlayer);
    94 }
    95 
    96 
    97 - (void) endTurn
    98 {
    99     NSLog(@"--- End of turn");
   100     Player *winner = [self checkForWinner];
   101     if( winner ) {
   102         NSLog(@"*** The %@ Ends! The winner is %@ ! ***", self.class, winner);
   103         self.winner = winner;
   104     } else
   105         [self nextPlayer];
   106 }
   107 
   108 
   109 #pragma mark -
   110 #pragma mark GAMEPLAY METHODS TO BE OVERRIDDEN:
   111 
   112 
   113 + (BOOL) landscapeOriented
   114 {
   115     return NO;
   116 }
   117 
   118 
   119 - (BOOL) canBit: (Bit*)bit moveFrom: (id<BitHolder>)src
   120 {
   121     return YES;
   122 }
   123 
   124 - (BOOL) canBit: (Bit*)bit moveFrom: (id<BitHolder>)src to: (id<BitHolder>)dst
   125 {
   126     return YES;
   127 }
   128 
   129 - (void) bit: (Bit*)bit movedFrom: (id<BitHolder>)src to: (id<BitHolder>)dst
   130 {
   131     [self endTurn];
   132 }
   133 
   134 - (Bit*) bitToPlaceInHolder: (id<BitHolder>)holder
   135 {
   136     return nil;
   137 }
   138 
   139 
   140 - (BOOL) clickedBit: (Bit*)bit
   141 {
   142     return YES;
   143 }
   144 
   145 - (Player*) checkForWinner
   146 {
   147     return nil;
   148 }
   149 
   150 
   151 @end
   152 
   153 
   154 
   155 
   156 @implementation Player
   157 
   158 
   159 - (id) initWithGame: (Game*)game
   160 {
   161     self = [super init];
   162     if (self != nil) {
   163         _game = game;
   164     }
   165     return self;
   166 }
   167 
   168 
   169 @synthesize game=_game, name=_name;
   170 
   171 - (BOOL) isCurrent      {return self == _game.currentPlayer;}
   172 - (BOOL) isFriendly     {return self == _game.currentPlayer;}   // could be overridden for games with partners
   173 - (BOOL) isUnfriendly   {return ! self.friendly;}
   174 
   175 - (int) index
   176 {
   177     return [_game.players indexOfObjectIdenticalTo: self];
   178 }
   179 
   180 - (Player*) nextPlayer
   181 {
   182     return [_game.players objectAtIndex: (self.index+1) % _game.players.count];
   183 }
   184 
   185 - (Player*) previousPlayer
   186 {
   187     return [_game.players objectAtIndex: (self.index-1) % _game.players.count];
   188 }
   189 
   190 - (NSString*) description
   191 {
   192     return [NSString stringWithFormat: @"%@[%@]", self.class,self.name];
   193 }
   194 
   195 @end
   196 
   197 
   198 
   199 
   200 @implementation CALayer (Game)
   201 
   202 - (Game*) game
   203 {
   204     // The Game object stores a pointer to itself as the value of the "Game" property
   205     // of its root layer. (CALayers can have arbitrary KV properties stored into them.)
   206     for( CALayer *layer = self; layer; layer=layer.superlayer ) {
   207         Game *game = [layer valueForKey: @"Game"];
   208         if( game )
   209             return game;
   210     }
   211     NSAssert1(NO,@"Couldn't look up Game from %@",self);
   212     return nil;
   213 }
   214 
   215 @end