Source/CheckersGame.m
changeset 13 db7bb080c3d5
parent 11 436cbdf56810
child 15 73f8c889f053
     1.1 --- a/Source/CheckersGame.m	Sat Jul 05 17:46:43 2008 -0700
     1.2 +++ b/Source/CheckersGame.m	Tue Jul 08 13:12:01 2008 -0700
     1.3 @@ -53,7 +53,6 @@
     1.4  {
     1.5      self = [super init];
     1.6      if (self != nil) {
     1.7 -        _cells = [[NSMutableArray alloc] init];
     1.8          [self setNumberOfPlayers: 2];
     1.9          
    1.10          PreloadSound(@"Tink");
    1.11 @@ -82,7 +81,7 @@
    1.12  - (void) makeKing: (Piece*)piece
    1.13  {
    1.14      piece.scale = 1.4;
    1.15 -    [piece setValue: @"King" forKey: @"King"];
    1.16 +    piece.tag = YES;        // tag property stores the 'king' flag
    1.17      piece.name = piece.owner.index ?@"4" :@"3";
    1.18  }
    1.19  
    1.20 @@ -100,65 +99,34 @@
    1.21      grid.altCellColor = CreateGray(1.0, 0.25);
    1.22      grid.lineColor = nil;
    1.23  
    1.24 -    [grid addAllCells];
    1.25 -    [_cells removeAllObjects];
    1.26      for( int i=0; i<32; i++ ) {
    1.27          int row = i/4;
    1.28 -        [_cells addObject: [_grid cellAtRow: row column: 2*(i%4) + (row&1)]];
    1.29 +        [_grid addCellAtRow: row column: 2*(i%4) + (row&1)];
    1.30      }
    1.31 +    [_grid release]; // its superlayer still retains it
    1.32  }
    1.33  
    1.34 -- (void) dealloc
    1.35 +- (NSString*) initialStateString            {return @"111111111111--------222222222222";}
    1.36 +- (NSString*) stateString                   {return _grid.stateString;}
    1.37 +- (void) setStateString: (NSString*)state   {_grid.stateString = state;}
    1.38 +
    1.39 +- (Piece*) makePieceNamed: (NSString*)name
    1.40  {
    1.41 -    [_cells release];
    1.42 -    [_grid release];
    1.43 -    [super dealloc];
    1.44 -}
    1.45 -
    1.46 -
    1.47 -- (NSString*) initialStateString
    1.48 -{
    1.49 -    return @"111111111111--------222222222222";
    1.50 -}
    1.51 -
    1.52 -- (NSString*) stateString
    1.53 -{
    1.54 -    unichar state[_cells.count];
    1.55 -    int i = 0;
    1.56 -    for( GridCell *cell in _cells ) {
    1.57 -        NSString *ident = cell.bit.name;
    1.58 -        if( ident )
    1.59 -            state[i++] = [ident characterAtIndex: 0];
    1.60 -        else
    1.61 -            state[i++] = '-';
    1.62 -    }
    1.63 -    return [NSString stringWithCharacters: state length: i];
    1.64 -}
    1.65 -
    1.66 -- (void) setStateString: (NSString*)state
    1.67 -{
    1.68 -    _numPieces[0] = _numPieces[1] = 0;
    1.69 -    int i = 0;
    1.70 -    for( GridCell *cell in _cells ) {
    1.71 -        Piece *piece;
    1.72 -        int which = [state characterAtIndex: i++] - '1';
    1.73 -        if( which >=0 && which < 4 ) {
    1.74 -            int player = (which & 1);
    1.75 -            piece = [self pieceForPlayer: player];
    1.76 -            _numPieces[player]++;
    1.77 -            if( which & 2 ) 
    1.78 -                [self makeKing: piece];
    1.79 -        } else
    1.80 -            piece = nil;
    1.81 -        cell.bit = piece;
    1.82 -    }    
    1.83 +    int which = [name characterAtIndex: 0] - '1';
    1.84 +    if( which >=0 && which < 4 ) {
    1.85 +        Piece *piece = [self pieceForPlayer: (which & 1)];
    1.86 +        if( which & 2 ) 
    1.87 +            [self makeKing: piece];
    1.88 +        return piece;
    1.89 +    } else
    1.90 +        return nil;
    1.91  }
    1.92  
    1.93  
    1.94  - (BOOL) canBit: (Bit*)bit moveFrom: (id<BitHolder>)srcHolder to: (id<BitHolder>)dstHolder
    1.95  {
    1.96      Square *src=(Square*)srcHolder, *dst=(Square*)dstHolder;
    1.97 -    if( [bit valueForKey: @"King"] )
    1.98 +    if( bit.tag )
    1.99          if( dst==src.bl || dst==src.br || dst==src.l || dst==src.r
   1.100             || (src.bl.bit.unfriendly && dst==src.bl.bl) || (src.br.bit.unfriendly && dst==src.br.br) )
   1.101              return YES;    
   1.102 @@ -177,34 +145,24 @@
   1.103      [turn addToMove: @"-"];
   1.104      [turn addToMove: dst.name];
   1.105      
   1.106 -    BOOL isKing = ([bit valueForKey: @"King"] != nil);
   1.107 +    BOOL isKing = bit.tag;
   1.108      PlaySound(isKing ?@"Funk" :@"Tink");
   1.109  
   1.110      // "King" a piece that made it to the last row:
   1.111 -    if( dst.row == (playerIndex ?0 :7) )
   1.112 -        if( ! isKing ) {
   1.113 -            PlaySound(@"Blow");
   1.114 -            [self makeKing: (Piece*)bit];
   1.115 -            [turn addToMove: @"*"];
   1.116 -            // don't set isKing flag - piece can't jump again after being kinged.
   1.117 -        }
   1.118 +    if( !isKing && (dst.row == (playerIndex ?0 :7)) ) {
   1.119 +        PlaySound(@"Blow");
   1.120 +        [self makeKing: (Piece*)bit];
   1.121 +        [turn addToMove: @"*"];
   1.122 +        // don't set isKing flag - piece can't jump again after being kinged.
   1.123 +    }
   1.124  
   1.125      // Check for a capture:
   1.126 -    Square *capture = nil;
   1.127 -    if(dst==src.fl.fl)
   1.128 -        capture = src.fl;
   1.129 -    else if(dst==src.fr.fr)
   1.130 -        capture = src.fr;
   1.131 -    else if(dst==src.bl.bl)
   1.132 -        capture = src.bl;
   1.133 -    else if(dst==src.br.br)
   1.134 -        capture = src.br;
   1.135 -    
   1.136 -    if( capture ) {
   1.137 -        PlaySound(@"Pop");
   1.138 -        _numPieces[capture.bit.owner.index]--;
   1.139 +    NSArray *line = [src lineToCell: dst inclusive: NO];
   1.140 +    if( line.count==1 ) {
   1.141 +        Square *capture = [line objectAtIndex: 0];
   1.142          [capture destroyBit];
   1.143          [turn addToMove: @"!"];
   1.144 +        PlaySound(@"Pop");
   1.145          
   1.146          // Now check if another capture is possible. If so, don't end the turn:
   1.147          if( (dst.fl.bit.unfriendly && dst.fl.fl.empty) || (dst.fr.bit.unfriendly && dst.fr.fr.empty) )
   1.148 @@ -219,11 +177,9 @@
   1.149  
   1.150  - (Player*) checkForWinner
   1.151  {
   1.152 -    // Whoever runs out of pieces loses:
   1.153 -    if( _numPieces[0]==0 )
   1.154 -        return [self.players objectAtIndex: 1];
   1.155 -    else if( _numPieces[1]==0 )
   1.156 -        return [self.players objectAtIndex: 0];
   1.157 +    NSCountedSet *remaining = _grid.countPiecesByPlayer;
   1.158 +    if( remaining.count==1 )
   1.159 +        return [remaining anyObject];
   1.160      else
   1.161          return nil;
   1.162  }