Source/CheckersGame.m
changeset 20 7c9ecb09a612
parent 19 3b750982ff39
child 22 4cb50131788f
     1.1 --- a/Source/CheckersGame.m	Thu Jul 17 13:29:04 2008 -0700
     1.2 +++ b/Source/CheckersGame.m	Fri Jul 18 13:26:59 2008 -0700
     1.3 @@ -176,13 +176,32 @@
     1.4      [self endTurn];
     1.5  }
     1.6  
     1.7 +#pragma mark -
     1.8 +#pragma mark CHECK FOR WIN:
     1.9 +
    1.10 +static BOOL canOpponentMoveOrJump( GridCell *first, GridCell *second ) {
    1.11 +    return first.empty || (first.bit.friendly && second.empty);
    1.12 +}
    1.13 +
    1.14 +- (BOOL) canOpponentMoveFrom: (GridCell*)src
    1.15 +{
    1.16 +    if( ! src.bit.unfriendly )
    1.17 +        return NO;
    1.18 +    Square *square = (Square*)src;
    1.19 +    if( square.bit.tag )           // remember, it's opponent's piece, so directions are reversed
    1.20 +        if( canOpponentMoveOrJump(square.fl,square.fl.fl) || canOpponentMoveOrJump(square.fr,square.fr.fr) )
    1.21 +            return YES;
    1.22 +    return canOpponentMoveOrJump(square.bl,square.bl.bl) || canOpponentMoveOrJump(square.br,square.br.br);
    1.23 +}
    1.24 +
    1.25  - (Player*) checkForWinner
    1.26  {
    1.27 -    NSCountedSet *remaining = _board.countPiecesByPlayer;
    1.28 -    if( remaining.count==1 )
    1.29 -        return [remaining anyObject];
    1.30 -    else
    1.31 -        return nil;
    1.32 +    for( GridCell *cell in _board.cells )
    1.33 +        if( [self canOpponentMoveFrom: cell] ) {
    1.34 +            Log(@"Checkers: %@ can move from %@",self.currentPlayer.nextPlayer,cell);
    1.35 +            return nil;
    1.36 +        }
    1.37 +    return self.currentPlayer;
    1.38  }
    1.39  
    1.40