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.
 
     5     Redistribution and use in source and binary forms, with or without modification, are permitted
 
     6     provided that the following conditions are met:
 
     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.
 
    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.
 
    28 @property (copy) NSArray *players;
 
    29 @property (assign) Player *currentPlayer, *winner;
 
    36 + (NSString*) displayName
 
    38     NSString* name = [self description];
 
    39     if( [name hasSuffix: @"Game"] )
 
    40         name = [name substringToIndex: name.length-4];
 
    45 - (id) initWithBoard: (GGBLayer*)board
 
    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"];
 
    67 @synthesize players=_players, currentPlayer=_currentPlayer, winner=_winner;
 
    70 - (void) setNumberOfPlayers: (unsigned)n
 
    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];
 
    80     self.currentPlayer = nil;
 
    81     self.players = players;
 
    87     if( ! _currentPlayer ) {
 
    88         NSLog(@"*** The %@ Begins! ***", self.class);
 
    89         self.currentPlayer = [_players objectAtIndex: 0];
 
    91         self.currentPlayer = _currentPlayer.nextPlayer;
 
    93     NSLog(@"Current player is %@",_currentPlayer);
 
    99     NSLog(@"--- End of turn");
 
   100     Player *winner = [self checkForWinner];
 
   102         NSLog(@"*** The %@ Ends! The winner is %@ ! ***", self.class, winner);
 
   103         self.winner = winner;
 
   110 #pragma mark GAMEPLAY METHODS TO BE OVERRIDDEN:
 
   113 + (BOOL) landscapeOriented
 
   119 - (BOOL) canBit: (Bit*)bit moveFrom: (id<BitHolder>)src
 
   124 - (BOOL) canBit: (Bit*)bit moveFrom: (id<BitHolder>)src to: (id<BitHolder>)dst
 
   129 - (void) bit: (Bit*)bit movedFrom: (id<BitHolder>)src to: (id<BitHolder>)dst
 
   134 - (Bit*) bitToPlaceInHolder: (id<BitHolder>)holder
 
   140 - (BOOL) clickedBit: (Bit*)bit
 
   145 - (Player*) checkForWinner
 
   156 @implementation Player
 
   159 - (id) initWithGame: (Game*)game
 
   169 @synthesize game=_game, name=_name;
 
   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;}
 
   177     return [_game.players indexOfObjectIdenticalTo: self];
 
   180 - (Player*) nextPlayer
 
   182     return [_game.players objectAtIndex: (self.index+1) % _game.players.count];
 
   185 - (Player*) previousPlayer
 
   187     return [_game.players objectAtIndex: (self.index-1) % _game.players.count];
 
   190 - (NSString*) description
 
   192     return [NSString stringWithFormat: @"%@[%@]", self.class,self.name];
 
   200 @implementation CALayer (Game)
 
   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"];
 
   211     NSAssert1(NO,@"Couldn't look up Game from %@",self);