Source/CheckersGame.m
changeset 8 45c82a071aca
parent 1 3eb7be1dd7b6
child 9 a59acc683080
     1.1 --- a/Source/CheckersGame.m	Mon Mar 10 17:30:57 2008 -0700
     1.2 +++ b/Source/CheckersGame.m	Wed May 28 12:47:10 2008 -0700
     1.3 @@ -30,46 +30,34 @@
     1.4  @implementation CheckersGame
     1.5  
     1.6  
     1.7 -- (void) addPieces: (NSString*)imageName
     1.8 -            toGrid: (Grid*)grid
     1.9 -         forPlayer: (int)playerNum
    1.10 -              rows: (NSRange)rows
    1.11 -       alternating: (BOOL)alternating
    1.12 +- (Piece*) pieceForPlayer: (int)playerNum
    1.13  {
    1.14 -    Piece *prototype = [[Piece alloc] initWithImageNamed: imageName scale: floor(grid.spacing.width * 0.8)];
    1.15 -    prototype.owner = [self.players objectAtIndex: playerNum];
    1.16 -    unsigned cols=grid.columns;
    1.17 -    for( unsigned row=rows.location; row<NSMaxRange(rows); row++ )
    1.18 -        for( unsigned col=0; col<cols; col++ ) {
    1.19 -            if( !alternating || ((row+col) & 1) == 0 ) {
    1.20 -                GridCell *cell = [grid cellAtRow: row column: col];
    1.21 -                if( cell ) {
    1.22 -                    Piece *piece = [prototype copy];
    1.23 -                    cell.bit = piece;
    1.24 -                    [piece release];
    1.25 -                    //cell.bit.rotation = random() % 360; // keeps pieces from looking too samey
    1.26 -                    _numPieces[playerNum]++;
    1.27 -                }
    1.28 -            }
    1.29 -        }
    1.30 -    [prototype release];
    1.31 +    Piece *p = [[Piece alloc] initWithImageNamed: (playerNum==0 ?@"Green Ball.png" :@"Red Ball.png") 
    1.32 +                                           scale: floor(_grid.spacing.width * 0.8)];
    1.33 +    p.owner = [self.players objectAtIndex: playerNum];
    1.34 +    p.name = playerNum ?@"2" :@"1";
    1.35 +    return [p autorelease];
    1.36  }
    1.37  
    1.38 -
    1.39  - (Grid*) x_makeGrid
    1.40  {
    1.41      RectGrid *grid = [[[RectGrid alloc] initWithRows: 8 columns: 8 frame: _board.bounds] autorelease];
    1.42 -    CGPoint pos = grid.position;
    1.43 +    _grid = grid;
    1.44 +    CGPoint pos = _grid.position;
    1.45      pos.x = floor((_board.bounds.size.width-grid.frame.size.width)/2);
    1.46 -    [grid addAllCells];
    1.47      grid.position = pos;
    1.48      grid.allowsMoves = YES;
    1.49      grid.allowsCaptures = NO;
    1.50      grid.cellColor = CreateGray(0.0, 0.25);
    1.51      grid.altCellColor = CreateGray(1.0, 0.25);
    1.52      grid.lineColor = nil;
    1.53 -    [self addPieces: @"Green Ball.png" toGrid: grid forPlayer: 0 rows: NSMakeRange(0,3) alternating: YES];
    1.54 -    [self addPieces: @"Red Ball.png"   toGrid: grid forPlayer: 1 rows: NSMakeRange(5,3) alternating: YES];
    1.55 +
    1.56 +    [grid addAllCells];
    1.57 +    for( int i=0; i<32; i++ ) {
    1.58 +        int row = i/4;
    1.59 +        [_cells addObject: [_grid cellAtRow: row column: 2*(i%4) + (row&1)]];
    1.60 +    }
    1.61 +    self.stateString = @"111111111111--------222222222222";
    1.62      return grid;
    1.63  }
    1.64  
    1.65 @@ -79,12 +67,56 @@
    1.66      self = [super initWithBoard: board];
    1.67      if (self != nil) {
    1.68          [self setNumberOfPlayers: 2];
    1.69 -        [board addSublayer: [self x_makeGrid]];
    1.70 +        _cells = [[NSMutableArray alloc] init];
    1.71 +        [self x_makeGrid];
    1.72 +        [board addSublayer: _grid];
    1.73          [self nextPlayer];
    1.74 +        
    1.75 +        PreloadSound(@"Tink");
    1.76 +        PreloadSound(@"Funk");
    1.77 +        PreloadSound(@"Blow");
    1.78 +        PreloadSound(@"Pop");
    1.79      }
    1.80      return self;
    1.81  }
    1.82  
    1.83 +- (void) dealloc
    1.84 +{
    1.85 +    [_cells release];
    1.86 +    [_grid release];
    1.87 +    [super dealloc];
    1.88 +}
    1.89 +
    1.90 +
    1.91 +- (NSString*) stateString
    1.92 +{
    1.93 +    unichar state[_cells.count];
    1.94 +    int i = 0;
    1.95 +    for( GridCell *cell in _cells ) {
    1.96 +        NSString *ident = cell.bit.name;
    1.97 +        if( ident )
    1.98 +            state[i++] = [ident characterAtIndex: 0];
    1.99 +        else
   1.100 +            state[i++] = '-';
   1.101 +    }
   1.102 +    return [NSString stringWithCharacters: state length: i];
   1.103 +}
   1.104 +
   1.105 +- (void) setStateString: (NSString*)state
   1.106 +{
   1.107 +    _numPieces[0] = _numPieces[1] = 0;
   1.108 +    int i = 0;
   1.109 +    for( GridCell *cell in _cells ) {
   1.110 +        Piece *piece;
   1.111 +        switch( [state characterAtIndex: i++] ) {
   1.112 +            case '1': piece = [self pieceForPlayer: 0]; _numPieces[0]++; break;
   1.113 +            case '2': piece = [self pieceForPlayer: 1]; _numPieces[1]++; break;
   1.114 +            default:  piece = nil; break;
   1.115 +        }
   1.116 +        cell.bit = piece;
   1.117 +    }    
   1.118 +}
   1.119 +
   1.120  
   1.121  - (BOOL) canBit: (Bit*)bit moveFrom: (id<BitHolder>)srcHolder to: (id<BitHolder>)dstHolder
   1.122  {
   1.123 @@ -101,8 +133,12 @@
   1.124  {
   1.125      Square *src=(Square*)srcHolder, *dst=(Square*)dstHolder;
   1.126      int playerIndex = self.currentPlayer.index;
   1.127 +    
   1.128 +    if( self.currentMove.length==0 )
   1.129 +        [self.currentMove appendString: src.name];
   1.130 +    [self.currentMove appendString: dst.name];
   1.131 +    
   1.132      BOOL isKing = ([bit valueForKey: @"King"] != nil);
   1.133 -    
   1.134      PlaySound(isKing ?@"Funk" :@"Tink");
   1.135  
   1.136      // "King" a piece that made it to the last row:
   1.137 @@ -154,4 +190,22 @@
   1.138  }
   1.139  
   1.140  
   1.141 +- (BOOL) applyMoveString: (NSString*)move
   1.142 +{
   1.143 +    int length = move.length;
   1.144 +    if( length<4 || (length&1) )
   1.145 +        return NO;
   1.146 +    GridCell *src = nil;
   1.147 +    for( int i=0; i<length; i+=2 ) {
   1.148 +        NSString *ident = [move substringWithRange: NSMakeRange(i,2)];
   1.149 +        GridCell *dst = [_grid cellWithName: ident];
   1.150 +        if( i > 0 )
   1.151 +            if( ! [self animateMoveFrom: src to: dst] )
   1.152 +                return NO;
   1.153 +        src = dst;
   1.154 +    }
   1.155 +    return YES;
   1.156 +}
   1.157 +
   1.158 +
   1.159  @end