Checkers and Hexchequer now detect victory when the other player can't move.
authorJens Alfke <jens@mooseyard.com>
Fri Jul 18 13:26:59 2008 -0700 (2008-07-18)
changeset 207c9ecb09a612
parent 19 3b750982ff39
child 21 2eb229411d73
Checkers and Hexchequer now detect victory when the other player can't move.
Source/CheckersGame.h
Source/CheckersGame.m
Source/HexchequerGame.m
     1.1 --- a/Source/CheckersGame.h	Thu Jul 17 13:29:04 2008 -0700
     1.2 +++ b/Source/CheckersGame.h	Fri Jul 18 13:26:59 2008 -0700
     1.3 @@ -21,7 +21,7 @@
     1.4      THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     1.5  */
     1.6  #import "Game+Protected.h"
     1.7 -@class Grid;
     1.8 +@class Grid, GridCell;
     1.9  
    1.10  
    1.11  /** US Checkers, known as Draughts in the UK. (Other countries use similar but not identical rules.)
    1.12 @@ -31,4 +31,7 @@
    1.13      Grid *_board;
    1.14  }
    1.15  
    1.16 +//protected
    1.17 +- (BOOL) canOpponentMoveFrom: (GridCell*)src;
    1.18 +
    1.19  @end
     2.1 --- a/Source/CheckersGame.m	Thu Jul 17 13:29:04 2008 -0700
     2.2 +++ b/Source/CheckersGame.m	Fri Jul 18 13:26:59 2008 -0700
     2.3 @@ -176,13 +176,32 @@
     2.4      [self endTurn];
     2.5  }
     2.6  
     2.7 +#pragma mark -
     2.8 +#pragma mark CHECK FOR WIN:
     2.9 +
    2.10 +static BOOL canOpponentMoveOrJump( GridCell *first, GridCell *second ) {
    2.11 +    return first.empty || (first.bit.friendly && second.empty);
    2.12 +}
    2.13 +
    2.14 +- (BOOL) canOpponentMoveFrom: (GridCell*)src
    2.15 +{
    2.16 +    if( ! src.bit.unfriendly )
    2.17 +        return NO;
    2.18 +    Square *square = (Square*)src;
    2.19 +    if( square.bit.tag )           // remember, it's opponent's piece, so directions are reversed
    2.20 +        if( canOpponentMoveOrJump(square.fl,square.fl.fl) || canOpponentMoveOrJump(square.fr,square.fr.fr) )
    2.21 +            return YES;
    2.22 +    return canOpponentMoveOrJump(square.bl,square.bl.bl) || canOpponentMoveOrJump(square.br,square.br.br);
    2.23 +}
    2.24 +
    2.25  - (Player*) checkForWinner
    2.26  {
    2.27 -    NSCountedSet *remaining = _board.countPiecesByPlayer;
    2.28 -    if( remaining.count==1 )
    2.29 -        return [remaining anyObject];
    2.30 -    else
    2.31 -        return nil;
    2.32 +    for( GridCell *cell in _board.cells )
    2.33 +        if( [self canOpponentMoveFrom: cell] ) {
    2.34 +            Log(@"Checkers: %@ can move from %@",self.currentPlayer.nextPlayer,cell);
    2.35 +            return nil;
    2.36 +        }
    2.37 +    return self.currentPlayer;
    2.38  }
    2.39  
    2.40  
     3.1 --- a/Source/HexchequerGame.m	Thu Jul 17 13:29:04 2008 -0700
     3.2 +++ b/Source/HexchequerGame.m	Fri Jul 18 13:26:59 2008 -0700
     3.3 @@ -127,5 +127,17 @@
     3.4      [self endTurn];
     3.5  }
     3.6  
     3.7 +- (BOOL) canOpponentMoveFrom: (GridCell*)src
     3.8 +{
     3.9 +    if( ! src.bit.unfriendly )
    3.10 +        return NO;
    3.11 +    if( [super canOpponentMoveFrom: src] )
    3.12 +        return YES;
    3.13 +    Hex *hex = (Hex*)src;
    3.14 +    if( hex.bit.tag )           // remember, it's opponent's piece, so directions are reversed
    3.15 +        if( hex.r.empty || (hex.r.bit.friendly && hex.r.r.empty) )
    3.16 +            return YES;
    3.17 +    return hex.l.empty || (hex.l.bit.friendly && hex.l.l.empty);
    3.18 +}
    3.19  
    3.20  @end