Source/TicTacToeGame.m
changeset 12 4e567e11f45f
parent 8 45c82a071aca
child 16 28392c9a969f
     1.1 --- a/Source/TicTacToeGame.m	Wed May 28 12:47:10 2008 -0700
     1.2 +++ b/Source/TicTacToeGame.m	Mon Jul 07 15:47:42 2008 -0700
     1.3 @@ -38,42 +38,44 @@
     1.4      return [p autorelease];
     1.5  }
     1.6  
     1.7 -- (id) initWithBoard: (GGBLayer*)board
     1.8 +- (id) init
     1.9  {
    1.10 -    self = [super initWithBoard: board];
    1.11 +    self = [super init];
    1.12      if (self != nil) {
    1.13          [self setNumberOfPlayers: 2];
    1.14 -        
    1.15 -        // Create a 3x3 grid:
    1.16 -        CGFloat center = floor(CGRectGetMidX(board.bounds));
    1.17 -        _grid = [[RectGrid alloc] initWithRows: 3 columns: 3 frame: CGRectMake(center-150,0, 300,300)];
    1.18 -        [_grid addAllCells];
    1.19 -        _grid.allowsMoves = _grid.allowsCaptures = NO;
    1.20 -        _grid.cellColor = CreateGray(1.0, 0.25);
    1.21 -        _grid.lineColor = kTranslucentLightGrayColor;
    1.22 -        [board addSublayer: _grid];
    1.23 -        
    1.24 -        // Create piece dispensers for the two players:
    1.25 -        for( int playerNumber=0; playerNumber<=1; playerNumber++ ) {
    1.26 -            Piece *p = [self pieceForPlayer: playerNumber];
    1.27 -            CGFloat x = floor(CGRectGetMidX(_board.bounds));
    1.28 -#if TARGET_OS_IPHONE
    1.29 -            x = x - 80 + 160*playerNumber;
    1.30 -            CGFloat y = 360;
    1.31 -#else
    1.32 -            x += (playerNumber==0 ?-230 :230);
    1.33 -            CGFloat y = 175;
    1.34 -#endif
    1.35 -            _dispenser[playerNumber] = [[Dispenser alloc] initWithPrototype: p quantity: 0
    1.36 -                                                                      frame: CGRectMake(x-45,y-45, 90,90)];
    1.37 -            [_board addSublayer: _dispenser[playerNumber]];
    1.38 -        }            
    1.39 -        
    1.40 -        // And they're off!
    1.41 -        [self nextPlayer];
    1.42      }
    1.43      return self;
    1.44  }
    1.45 +        
    1.46 +- (void) setUpBoard
    1.47 +{
    1.48 +    // Create a 3x3 grid:
    1.49 +    CGFloat center = floor(CGRectGetMidX(_board.bounds));
    1.50 +    [_grid release];
    1.51 +    _grid = [[RectGrid alloc] initWithRows: 3 columns: 3 frame: CGRectMake(center-150,0, 300,300)];
    1.52 +    [_grid addAllCells];
    1.53 +    _grid.allowsMoves = _grid.allowsCaptures = NO;
    1.54 +    _grid.cellColor = CreateGray(1.0, 0.25);
    1.55 +    _grid.lineColor = kTranslucentLightGrayColor;
    1.56 +    [_board addSublayer: _grid];
    1.57 +    
    1.58 +    // Create piece dispensers for the two players:
    1.59 +    for( int playerNumber=0; playerNumber<=1; playerNumber++ ) {
    1.60 +        Piece *p = [self pieceForPlayer: playerNumber];
    1.61 +        CGFloat x = floor(CGRectGetMidX(_board.bounds));
    1.62 +#if TARGET_OS_IPHONE
    1.63 +        x = x - 80 + 160*playerNumber;
    1.64 +        CGFloat y = 360;
    1.65 +#else
    1.66 +        x += (playerNumber==0 ?-230 :230);
    1.67 +        CGFloat y = 175;
    1.68 +#endif
    1.69 +        [_dispenser[playerNumber] release];
    1.70 +        _dispenser[playerNumber] = [[Dispenser alloc] initWithPrototype: p quantity: 0
    1.71 +                                                                  frame: CGRectMake(x-45,y-45, 90,90)];
    1.72 +        [_board addSublayer: _dispenser[playerNumber]];
    1.73 +    }            
    1.74 +}
    1.75  
    1.76  
    1.77  - (NSString*) stateString
    1.78 @@ -92,12 +94,13 @@
    1.79  - (void) setStateString: (NSString*)stateString
    1.80  {
    1.81      for( int i=0; i<9; i++ ) {
    1.82 -        Piece *piece;
    1.83 -        switch( [stateString characterAtIndex: i] ) {
    1.84 -            case 'X': case 'x': piece = [self pieceForPlayer: 0]; break;
    1.85 -            case 'O': case 'o': piece = [self pieceForPlayer: 1]; break;
    1.86 -            default:            piece = nil; break;
    1.87 -        }
    1.88 +        Piece *piece = nil;
    1.89 +        if( i < stateString.length )
    1.90 +            switch( [stateString characterAtIndex: i] ) {
    1.91 +                case 'X': case 'x': piece = [self pieceForPlayer: 0]; break;
    1.92 +                case 'O': case 'o': piece = [self pieceForPlayer: 1]; break;
    1.93 +                default:            break;
    1.94 +            }
    1.95          [_grid cellAtRow: i/3 column: i%3].bit = piece;
    1.96      }
    1.97  }
    1.98 @@ -116,16 +119,18 @@
    1.99  {
   1.100      Square *square = (Square*)dst;
   1.101      int squareIndex = 3*square.row + square.column;
   1.102 -    [self.currentMove appendFormat: @"%@%i", bit.name, squareIndex];
   1.103 +    [self.currentTurn addToMove: [NSString stringWithFormat: @"%@%i", bit.name, squareIndex]];
   1.104      [super bit: bit movedFrom: src to: dst];
   1.105  }
   1.106  
   1.107 +/* FIX: Need to restore this somehow, now that -nextPlayer is gone
   1.108  - (void) nextPlayer
   1.109  {
   1.110      [super nextPlayer];
   1.111      // Give the next player another piece to put down:
   1.112      _dispenser[self.currentPlayer.index].quantity = 1;
   1.113  }
   1.114 + */
   1.115  
   1.116  static Player* ownerAt( Grid *grid, int index )
   1.117  {