Source/KlondikeGame.m
author Jens Alfke <jens@mooseyard.com>
Mon Mar 10 17:32:04 2008 -0700 (2008-03-10)
changeset 2 7b0441db81e5
parent 0 e9f7ba4718e1
child 4 d781b00f3ed4
permissions -rw-r--r--
Oops, needed to fix an #include
     1 /*  This code is based on Apple's "GeekGameBoard" sample code, version 1.0.
     2     http://developer.apple.com/samplecode/GeekGameBoard/
     3     Copyright © 2007 Apple Inc. Copyright © 2008 Jens Alfke. All Rights Reserved.
     4 
     5     Redistribution and use in source and binary forms, with or without modification, are permitted
     6     provided that the following conditions are met:
     7 
     8     * Redistributions of source code must retain the above copyright notice, this list of conditions
     9       and the following disclaimer.
    10     * Redistributions in binary form must reproduce the above copyright notice, this list of
    11       conditions and the following disclaimer in the documentation and/or other materials provided
    12       with the distribution.
    13 
    14     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
    15     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 
    16     FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRI-
    17     BUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
    18     (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
    19     PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
    20     CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 
    21     THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    22 */
    23 #import "KlondikeGame.h"
    24 #import "Deck.h"
    25 #import "PlayingCard.h"
    26 #import "Stack.h"
    27 
    28 
    29 #define kStackHeight 500
    30 
    31 
    32 /**  WARNING: THIS CODE REQUIRES GARBAGE COLLECTION!
    33  **  This sample application uses Objective-C 2.0 garbage collection.
    34  **  Therefore, the source code in this file does NOT perform manual object memory management.
    35  **  If you reuse any of this code in a process that isn't garbage collected, you will need to
    36  **  add all necessary retain/release/autorelease calls, and implement -dealloc methods,
    37  **  otherwise unpleasant leakage will occur!
    38  **/
    39 
    40 
    41 @implementation KlondikeGame
    42 
    43 
    44 - (id) initWithBoard: (GGBLayer*)board
    45 {
    46     self = [super initWithBoard: board];
    47     if (self != nil) {
    48         [self setNumberOfPlayers: 1];
    49         
    50         _deck = [[Deck alloc] initWithCardsOfClass: [PlayingCard class]];
    51         [_deck shuffle];
    52         _deck.position = CGPointMake(kCardWidth/2+16,kCardHeight/2+16);
    53         [board addSublayer: _deck];
    54         
    55         _sink = [[Deck alloc] init];
    56         _sink.position = CGPointMake(3*kCardWidth/2+32,kCardHeight/2+16);
    57         [board addSublayer: _sink];
    58         
    59         for( CardSuit suit=kSuitClubs; suit<=kSuitSpades; suit++ ) {
    60             Deck *aces = [[Deck alloc] init];
    61             aces.position = CGPointMake(kCardWidth/2+16+(kCardWidth+16)*(suit%2),
    62                                         120+kCardHeight+(kCardHeight+16)*(suit/2));
    63             [board addSublayer: aces];
    64             _aces[suit] = aces;
    65         }
    66         
    67         for( int s=0; s<7; s++ ) {
    68             Stack *stack = [[Stack alloc] initWithStartPos: CGPointMake(kCardWidth/2,
    69                                                                         kStackHeight-kCardHeight/2.0)
    70                                                    spacing: CGSizeMake(0,-22)];
    71             stack.frame = CGRectMake(260+s*(kCardWidth+16),16, kCardWidth,kStackHeight);
    72             stack.backgroundColor = nil;
    73             stack.dragAsStacks = YES;
    74             [board addSublayer: stack];
    75             
    76             // According to the rules, one card should be added to each stack in turn, instead
    77             // of populating entire stacks one at a time. However, if one trusts the Deck's
    78             // -shuffle method (which uses the random() function, seeded with a high-entropy
    79             // cryptographically-strong value), it shouldn't make any difference :-)
    80             for( int c=0; c<=s; c++ )
    81                 [stack addBit: [_deck removeTopCard]];
    82             ((Card*)stack.bits.lastObject).faceUp = YES;
    83         }
    84         
    85         [self nextPlayer];
    86     }
    87     return self;
    88 }
    89 
    90 
    91 - (BOOL) clickedBit: (Bit*)bit
    92 {
    93     if( [bit isKindOfClass: [Card class]] ) {
    94         Card *card = (Card*)bit;
    95         if( card.holder == _deck ) {
    96             // Click on deck deals 3 cards to the sink:
    97             for( int i=0; i<3; i++ ) {
    98                 Card *card = [_deck removeTopCard];
    99                 if( card ) {
   100                     [_sink addCard: card];
   101                     card.faceUp = YES;
   102                 }
   103             }
   104             [self endTurn];
   105             return YES;
   106         } else if( card.holder == _sink ) {
   107             // Clicking the sink when the deck is empty re-deals:
   108             if( _deck.empty ) {
   109                 [_deck addCards: [_sink removeAllCards]];
   110                 [_deck flip];
   111                 [self endTurn];
   112                 return YES;
   113             }
   114         } else {
   115             // Click on a card elsewhere turns it face-up:
   116             if( ! card.faceUp ) {
   117                 card.faceUp = YES;
   118                 return YES;
   119             }
   120         }
   121     }
   122     return NO;
   123 }
   124 
   125 
   126 - (BOOL) canBit: (Bit*)bit moveFrom: (id<BitHolder>)src
   127 {
   128     if( [bit isKindOfClass: [DraggedStack class]] ) {
   129         Card *bottomSrc = [[(DraggedStack*)bit bits] objectAtIndex: 0];
   130         if( ! bottomSrc.faceUp )
   131             return NO;
   132     }
   133     return YES;
   134 }
   135 
   136 
   137 - (BOOL) canBit: (Bit*)bit moveFrom: (id<BitHolder>)src to: (id<BitHolder>)dst
   138 {
   139     if( src==_deck || dst==_deck || dst==_sink )
   140         return NO;
   141     
   142     // Find the bottom card being moved, and the top card it's moving onto:
   143     PlayingCard *bottomSrc;
   144     if( [bit isKindOfClass: [DraggedStack class]] )
   145         bottomSrc = [[(DraggedStack*)bit bits] objectAtIndex: 0];
   146     else
   147         bottomSrc = (PlayingCard*)bit;
   148     
   149     PlayingCard *topDst;
   150     if( [dst isKindOfClass: [Deck class]] ) {
   151         // Dragging to an ace pile:
   152         if( ! [bit isKindOfClass: [Card class]] )
   153             return NO;
   154         topDst = (PlayingCard*) ((Deck*)dst).topCard;
   155         if( topDst == nil )
   156             return bottomSrc.rank == kRankAce;
   157         else
   158             return bottomSrc.suit == topDst.suit && bottomSrc.rank == topDst.rank+1;
   159         
   160     } else {
   161         // Dragging to a card stack:
   162         topDst = (PlayingCard*) ((Stack*)dst).topBit;
   163         if( topDst == nil )
   164             return bottomSrc.rank == kRankKing;
   165         else
   166             return bottomSrc.color != topDst.color && bottomSrc.rank == topDst.rank-1;
   167     }
   168 }
   169 
   170 
   171 - (Player*) checkForWinner
   172 {
   173     for( CardSuit suit=kSuitClubs; suit<=kSuitSpades; suit++ )
   174         if( _aces[suit].cards.count < 13 )
   175             return nil;
   176     return _currentPlayer;
   177 }
   178 
   179 
   180 
   181 @end