Source/KlondikeGame.m
changeset 10 6c78cc6bd7a6
parent 4 d781b00f3ed4
child 13 db7bb080c3d5
     1.1 --- a/Source/KlondikeGame.m	Tue Mar 11 17:09:50 2008 -0700
     1.2 +++ b/Source/KlondikeGame.m	Thu Jul 03 17:44:30 2008 -0700
     1.3 @@ -39,64 +39,68 @@
     1.4  }
     1.5  
     1.6  
     1.7 -- (id) initWithBoard: (GGBLayer*)board
     1.8 +- (id) init
     1.9  {
    1.10 -    self = [super initWithBoard: board];
    1.11 -    if (self != nil) {
    1.12 +    self = [super init];
    1.13 +    if (self != nil)
    1.14          [self setNumberOfPlayers: 1];
    1.15 +    return self;
    1.16 +}
    1.17 +
    1.18          
    1.19 -        CGSize boardSize = board.bounds.size;
    1.20 -        CGFloat xSpacing = floor(boardSize.width/7);
    1.21 -        CGSize kCardSize;
    1.22 -        kCardSize.width  = round(xSpacing * 0.9);  // 1/7th of width, with 10% gap
    1.23 -        kCardSize.height = round(kCardSize.width * 1.5);
    1.24 -        CGFloat gap = xSpacing-kCardSize.width;
    1.25 -        [Card setCardSize: kCardSize];
    1.26 +- (void) setUpBoard
    1.27 +{
    1.28 +    CGSize boardSize = _board.bounds.size;
    1.29 +    CGFloat xSpacing = floor(boardSize.width/7);
    1.30 +    CGSize kCardSize;
    1.31 +    kCardSize.width  = round(xSpacing * 0.9);  // 1/7th of width, with 10% gap
    1.32 +    kCardSize.height = round(kCardSize.width * 1.5);
    1.33 +    CGFloat gap = xSpacing-kCardSize.width;
    1.34 +    [Card setCardSize: kCardSize];
    1.35 +    
    1.36 +    CGPoint pos = {floor(gap/2)+kCardSize.width/2, floor(boardSize.height-kCardSize.height/2)};
    1.37 +    [_deck release];
    1.38 +    _deck = [[Deck alloc] initWithCardsOfClass: [PlayingCard class]];
    1.39 +    [_deck shuffle];
    1.40 +    _deck.position = pos;
    1.41 +    [_board addSublayer: _deck];
    1.42 +    
    1.43 +    pos.x += xSpacing;
    1.44 +    [_sink release];
    1.45 +    _sink = [[Deck alloc] init];
    1.46 +    _sink.position = pos;
    1.47 +    [_board addSublayer: _sink];
    1.48 +    
    1.49 +    pos.x += xSpacing;
    1.50 +    for( CardSuit suit=kSuitClubs; suit<=kSuitSpades; suit++ ) {
    1.51 +        pos.x += xSpacing;
    1.52 +        Deck *aces = [[Deck alloc] init];
    1.53 +        aces.position = pos;
    1.54 +        [_board addSublayer: aces];
    1.55 +        [_aces[suit] release];
    1.56 +        _aces[suit] = aces;
    1.57 +    }
    1.58 +    
    1.59 +    CGRect stackFrame = {{floor(gap/2), gap}, 
    1.60 +                         {kCardSize.width, boardSize.height-kCardSize.height-2*gap}};
    1.61 +    CGPoint startPos = CGPointMake(kCardSize.width/2,kCardSize.height/2);
    1.62 +    CGSize spacing = {0, floor((stackFrame.size.height-kCardSize.height)/11.0)};
    1.63 +    for( int s=0; s<7; s++ ) {
    1.64 +        Stack *stack = [[Stack alloc] initWithStartPos: startPos spacing: spacing];
    1.65 +        stack.frame = stackFrame;
    1.66 +        stackFrame.origin.x += xSpacing;
    1.67 +        stack.backgroundColor = nil; //kAlmostInvisibleWhiteColor;
    1.68 +        stack.dragAsStacks = YES;
    1.69 +        [_board addSublayer: stack];
    1.70          
    1.71 -        CGPoint pos = {floor(gap/2)+kCardSize.width/2, floor(boardSize.height-kCardSize.height/2)};
    1.72 -        _deck = [[Deck alloc] initWithCardsOfClass: [PlayingCard class]];
    1.73 -        [_deck shuffle];
    1.74 -        _deck.position = pos;
    1.75 -        [board addSublayer: _deck];
    1.76 -        
    1.77 -        pos.x += xSpacing;
    1.78 -        _sink = [[Deck alloc] init];
    1.79 -        _sink.position = pos;
    1.80 -        [board addSublayer: _sink];
    1.81 -        
    1.82 -        pos.x += xSpacing;
    1.83 -        for( CardSuit suit=kSuitClubs; suit<=kSuitSpades; suit++ ) {
    1.84 -            pos.x += xSpacing;
    1.85 -            Deck *aces = [[Deck alloc] init];
    1.86 -            aces.position = pos;
    1.87 -            [board addSublayer: aces];
    1.88 -            _aces[suit] = aces;
    1.89 -        }
    1.90 -        
    1.91 -        CGRect stackFrame = {{floor(gap/2), gap}, 
    1.92 -                             {kCardSize.width, boardSize.height-kCardSize.height-2*gap}};
    1.93 -        CGPoint startPos = CGPointMake(kCardSize.width/2,kCardSize.height/2);
    1.94 -        CGSize spacing = {0, floor((stackFrame.size.height-kCardSize.height)/11.0)};
    1.95 -        for( int s=0; s<7; s++ ) {
    1.96 -            Stack *stack = [[Stack alloc] initWithStartPos: startPos spacing: spacing];
    1.97 -            stack.frame = stackFrame;
    1.98 -            stackFrame.origin.x += xSpacing;
    1.99 -            stack.backgroundColor = nil; //kAlmostInvisibleWhiteColor;
   1.100 -            stack.dragAsStacks = YES;
   1.101 -            [board addSublayer: stack];
   1.102 -            
   1.103 -            // According to the rules, one card should be added to each stack in turn, instead
   1.104 -            // of populating entire stacks one at a time. However, if one trusts the Deck's
   1.105 -            // -shuffle method (which uses the random() function, seeded with a high-entropy
   1.106 -            // cryptographically-strong value), it shouldn't make any difference :-)
   1.107 -            for( int c=0; c<=s; c++ )
   1.108 -                [stack addBit: [_deck removeTopCard]];
   1.109 -            ((Card*)stack.bits.lastObject).faceUp = YES;
   1.110 -        }
   1.111 -        
   1.112 -        [self nextPlayer];
   1.113 +        // According to the rules, one card should be added to each stack in turn, instead
   1.114 +        // of populating entire stacks one at a time. However, if one trusts the Deck's
   1.115 +        // -shuffle method (which uses the random() function, seeded with a high-entropy
   1.116 +        // cryptographically-strong value), it shouldn't make any difference :-)
   1.117 +        for( int c=0; c<=s; c++ )
   1.118 +            [stack addBit: [_deck removeTopCard]];
   1.119 +        ((Card*)stack.bits.lastObject).faceUp = YES;
   1.120      }
   1.121 -    return self;
   1.122  }
   1.123  
   1.124  
   1.125 @@ -185,7 +189,7 @@
   1.126      for( CardSuit suit=kSuitClubs; suit<=kSuitSpades; suit++ )
   1.127          if( _aces[suit].cards.count < 13 )
   1.128              return nil;
   1.129 -    return _currentPlayer;
   1.130 +    return self.currentPlayer;
   1.131  }
   1.132  
   1.133