Source/TicTacToeGame.m
changeset 7 428a194e3e59
parent 3 40d225cf9c43
child 8 45c82a071aca
     1.1 --- a/Source/TicTacToeGame.m	Tue Mar 11 09:21:53 2008 -0700
     1.2 +++ b/Source/TicTacToeGame.m	Sun Mar 16 15:06:47 2008 -0700
     1.3 @@ -29,21 +29,13 @@
     1.4  
     1.5  @implementation TicTacToeGame
     1.6  
     1.7 -- (void) x_createDispenser: (NSString*)imageName forPlayer: (int)playerNumber
     1.8 +- (Piece*) pieceForPlayer: (int)playerNumber
     1.9  {
    1.10 -    Piece *p = [[Piece alloc] initWithImageNamed: imageName scale: 80];
    1.11 +    Piece *p = [[Piece alloc] initWithImageNamed: (playerNumber ? @"O.tiff" :@"X.tiff")
    1.12 +                                           scale: 80];
    1.13      p.owner = [self.players objectAtIndex: playerNumber];
    1.14 -    CGFloat x = floor(CGRectGetMidX(_board.bounds));
    1.15 -#if TARGET_OS_ASPEN
    1.16 -    x = x - 80 + 160*playerNumber;
    1.17 -    CGFloat y = 360;
    1.18 -#else
    1.19 -    x += (playerNumber==0 ?-230 :230);
    1.20 -    CGFloat y = 175;
    1.21 -#endif
    1.22 -    _dispenser[playerNumber] = [[Dispenser alloc] initWithPrototype: p quantity: 0
    1.23 -                                                        frame: CGRectMake(x-45,y-45, 90,90)];
    1.24 -    [_board addSublayer: _dispenser[playerNumber]];
    1.25 +    p.name = (playerNumber ?@"O" :@"X");
    1.26 +    return [p autorelease];
    1.27  }
    1.28  
    1.29  - (id) initWithBoard: (GGBLayer*)board
    1.30 @@ -62,8 +54,20 @@
    1.31          [board addSublayer: _grid];
    1.32          
    1.33          // Create piece dispensers for the two players:
    1.34 -        [self x_createDispenser: @"X.tiff" forPlayer: 0];
    1.35 -        [self x_createDispenser: @"O.tiff" forPlayer: 1];
    1.36 +        for( int playerNumber=0; playerNumber<=1; playerNumber++ ) {
    1.37 +            Piece *p = [self pieceForPlayer: playerNumber];
    1.38 +            CGFloat x = floor(CGRectGetMidX(_board.bounds));
    1.39 +#if TARGET_OS_ASPEN
    1.40 +            x = x - 80 + 160*playerNumber;
    1.41 +            CGFloat y = 360;
    1.42 +#else
    1.43 +            x += (playerNumber==0 ?-230 :230);
    1.44 +            CGFloat y = 175;
    1.45 +#endif
    1.46 +            _dispenser[playerNumber] = [[Dispenser alloc] initWithPrototype: p quantity: 0
    1.47 +                                                                      frame: CGRectMake(x-45,y-45, 90,90)];
    1.48 +            [_board addSublayer: _dispenser[playerNumber]];
    1.49 +        }            
    1.50          
    1.51          // And they're off!
    1.52          [self nextPlayer];
    1.53 @@ -71,6 +75,34 @@
    1.54      return self;
    1.55  }
    1.56  
    1.57 +
    1.58 +- (NSString*) stateString
    1.59 +{
    1.60 +    unichar str[10];
    1.61 +    for( int i=0; i<9; i++ ) {
    1.62 +        NSString *ident = [_grid cellAtRow: i/3 column: i%3].bit.name;
    1.63 +        if( ident==nil )
    1.64 +            str[i] = '-';
    1.65 +        else 
    1.66 +            str[i] = [ident characterAtIndex: 0];
    1.67 +    }
    1.68 +    return [NSString stringWithCharacters: str length: 9];
    1.69 +}
    1.70 +
    1.71 +- (void) setStateString: (NSString*)stateString
    1.72 +{
    1.73 +    for( int i=0; i<9; i++ ) {
    1.74 +        Piece *piece;
    1.75 +        switch( [stateString characterAtIndex: i] ) {
    1.76 +            case 'X': case 'x': piece = [self pieceForPlayer: 0]; break;
    1.77 +            case 'O': case 'o': piece = [self pieceForPlayer: 1]; break;
    1.78 +            default:            piece = nil; break;
    1.79 +        }
    1.80 +        [_grid cellAtRow: i/3 column: i%3].bit = piece;
    1.81 +    }
    1.82 +}
    1.83 +
    1.84 +
    1.85  - (Bit*) bitToPlaceInHolder: (id<BitHolder>)holder
    1.86  {
    1.87      if( holder.bit==nil && [holder isKindOfClass: [Square class]] )
    1.88 @@ -79,6 +111,15 @@
    1.89          return nil;
    1.90  }
    1.91  
    1.92 +
    1.93 +- (void) bit: (Bit*)bit movedFrom: (id<BitHolder>)src to: (id<BitHolder>)dst
    1.94 +{
    1.95 +    Square *square = (Square*)dst;
    1.96 +    int squareIndex = 3*square.row + square.column;
    1.97 +    [self.currentMove appendFormat: @"%@%i", bit.name, squareIndex];
    1.98 +    [super bit: bit movedFrom: src to: dst];
    1.99 +}
   1.100 +
   1.101  - (void) nextPlayer
   1.102  {
   1.103      [super nextPlayer];