jens@0: /* This code is based on Apple's "GeekGameBoard" sample code, version 1.0. jens@0: http://developer.apple.com/samplecode/GeekGameBoard/ jens@0: Copyright © 2007 Apple Inc. Copyright © 2008 Jens Alfke. All Rights Reserved. jens@0: jens@0: Redistribution and use in source and binary forms, with or without modification, are permitted jens@0: provided that the following conditions are met: jens@0: jens@0: * Redistributions of source code must retain the above copyright notice, this list of conditions jens@0: and the following disclaimer. jens@0: * Redistributions in binary form must reproduce the above copyright notice, this list of jens@0: conditions and the following disclaimer in the documentation and/or other materials provided jens@0: with the distribution. jens@0: jens@0: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR jens@0: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND jens@0: FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRI- jens@0: BUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES jens@0: (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR jens@0: PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN jens@0: CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF jens@0: THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. jens@0: */ jens@0: #import "Deck.h" jens@0: #import "Card.h" jens@0: #import "Stack.h" jens@0: #import "QuartzUtils.h" jens@0: #import "GGBUtils.h" jens@0: jens@0: jens@0: @interface Deck () jens@0: - (void) setCards: (NSMutableArray*)cards; jens@0: - (void) x_showTopCard; jens@0: @end jens@0: jens@0: jens@0: @implementation Deck jens@0: jens@0: jens@0: - (id) init jens@0: { jens@0: self = [super init]; jens@0: if (self != nil) { jens@4: self.bounds = (CGRect){{0,0},[Card cardSize]}; jens@0: self.cornerRadius = 8; jens@0: self.backgroundColor = kAlmostInvisibleWhiteColor; jens@0: self.borderColor = kHighlightColor; jens@0: self.cards = [NSMutableArray array]; jens@0: } jens@0: return self; jens@0: } jens@0: jens@0: - (id) initWithCardsOfClass: (Class)klass jens@0: { jens@0: self = [self init]; jens@0: if (self != nil) { jens@0: // Create a full deck of cards: jens@0: NSRange serials = [klass serialNumberRange]; jens@0: for( int i=serials.location; i 0; n-- ) { jens@0: int i = random() % n; jens@0: Card *card = [_cards objectAtIndex: i]; jens@0: [shuffled addObject: card]; jens@0: [_cards removeObjectAtIndex: i]; jens@0: } jens@0: self.cards = shuffled; jens@0: [self x_showTopCard]; jens@0: } jens@0: jens@0: jens@0: - (void) flip jens@0: { jens@0: int n = _cards.count; jens@0: NSMutableArray *flipped = [NSMutableArray arrayWithCapacity: n]; jens@0: while( --n >= 0 ) { jens@0: Card *card = [_cards objectAtIndex: n]; jens@0: card.faceUp = ! card.faceUp; jens@0: [flipped addObject: card]; jens@0: } jens@0: self.cards = flipped; jens@0: [self x_showTopCard]; jens@0: } jens@0: jens@0: jens@0: - (void) addCard: (Card*)card jens@0: { jens@0: [_cards addObject: card]; jens@0: [self x_showTopCard]; jens@0: } jens@0: jens@0: - (void) addCardAtBottom: (Card*)card jens@0: { jens@0: [_cards insertObject: card atIndex: 0]; jens@0: if( _cards.count==1 ) jens@0: [self x_showTopCard]; jens@0: } jens@0: jens@0: - (void) addCardAtRandom: (Card*)card jens@0: { jens@0: // Put the card at some random location, but _not_ on top (unless the deck is empty.) jens@0: int n = _cards.count; jens@0: if( n==0 ) jens@0: [self addCard: card]; jens@0: else jens@0: [_cards insertObject: card atIndex: (random() % (n-1))]; jens@0: } jens@0: jens@0: jens@0: - (void) addCards: (NSArray*)cards jens@0: { jens@0: [_cards addObjectsFromArray: cards]; jens@0: [self x_showTopCard]; jens@0: } jens@0: jens@0: jens@0: - (BOOL) addBit: (Bit*)bit jens@0: { jens@0: if( [bit isKindOfClass: [DraggedStack class]] ) { jens@0: // Convert a DraggedStack back to a group of Cards: jens@0: for( Bit *subBit in [(DraggedStack*)bit bits] ) jens@0: if( ! [self addBit: subBit] ) jens@0: return NO; jens@0: return YES; jens@0: } else if( [bit isKindOfClass: [Card class]] ) { jens@0: [self addCard: (Card*)bit]; jens@0: return YES; jens@0: } else jens@0: return NO; jens@0: } jens@0: jens@0: jens@0: - (Card*) removeTopCard jens@0: { jens@0: Card *card = [_cards lastObject]; jens@0: if( card ) { jens@0: [[card retain] autorelease]; jens@0: [_cards removeLastObject]; jens@0: _bit = nil; // keep it from being removed from superlayer by _showTopCard jens@0: [self x_showTopCard]; jens@0: } jens@0: return card; jens@0: } jens@0: jens@0: jens@0: - (NSArray*) removeAllCards jens@0: { jens@0: NSArray *removedCards = [[_cards retain] autorelease]; jens@0: self.cards = [NSMutableArray array]; jens@0: [removedCards makeObjectsPerformSelector: @selector(removeFromSuperlayer)]; jens@0: [self x_showTopCard]; jens@0: return removedCards; jens@0: } jens@0: jens@0: jens@0: #pragma mark - jens@0: #pragma mark BITHOLDER INTERFACE: jens@0: jens@0: jens@0: - (Bit*) canDragBit: (Bit*)bit jens@0: { jens@0: if( bit == _bit ) { jens@0: [bit retain]; jens@0: [_cards removeObjectIdenticalTo: bit]; jens@0: _bit = nil; // prevent the card from being removed from my layer jens@0: [self x_showTopCard]; jens@0: return [bit autorelease]; jens@0: } else jens@0: return nil; jens@0: } jens@0: jens@0: - (void) cancelDragBit: (Bit*)bit jens@0: { jens@0: [self addCard: (Card*)bit]; jens@0: } jens@0: jens@0: - (void) draggedBit: (Bit*)bit to: (id)dst {} jens@0: jens@0: jens@0: - (void) setHighlighted: (BOOL)h jens@0: { jens@0: [super setHighlighted: h]; jens@0: self.borderWidth = h ?6 :0; jens@0: } jens@0: jens@0: - (BOOL) canDropBit: (Bit*)bit atPoint: (CGPoint)point jens@0: { jens@0: return [bit isKindOfClass: [Card class]] || [bit isKindOfClass: [DraggedStack class]]; jens@0: } jens@0: jens@0: - (BOOL) dropBit: (Bit*)bit atPoint: (CGPoint)point jens@0: { jens@0: return [self addBit: bit]; jens@0: } jens@0: jens@0: jens@0: jens@0: @end