Initial check-in into Mercurial. Branched from 1.0 release of Apple's sample code. No longer requires garbage collection. Fixed some memory leaks of CG objects. Fixed a bug when advancing to the 8th row in the Checkers game.
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.
5 Redistribution and use in source and binary forms, with or without modification, are permitted
6 provided that the following conditions are met:
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.
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.
23 #import "KlondikeGame.h"
25 #import "PlayingCard.h"
29 #define kStackHeight 500
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!
41 @implementation KlondikeGame
44 - (id) initWithBoard: (CALayer*)board
46 self = [super initWithBoard: board];
48 [self setNumberOfPlayers: 1];
50 _deck = [[Deck alloc] initWithCardsOfClass: [PlayingCard class]];
52 _deck.position = CGPointMake(kCardWidth/2+16,kCardHeight/2+16);
53 [board addSublayer: _deck];
55 _sink = [[Deck alloc] init];
56 _sink.position = CGPointMake(3*kCardWidth/2+32,kCardHeight/2+16);
57 [board addSublayer: _sink];
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];
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];
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;
91 - (BOOL) clickedBit: (Bit*)bit
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];
100 [_sink addCard: card];
106 } else if( card.holder == _sink ) {
107 // Clicking the sink when the deck is empty re-deals:
109 [_deck addCards: [_sink removeAllCards]];
115 // Click on a card elsewhere turns it face-up:
116 if( ! card.faceUp ) {
126 - (BOOL) canBit: (Bit*)bit moveFrom: (id<BitHolder>)src
128 if( [bit isKindOfClass: [DraggedStack class]] ) {
129 Card *bottomSrc = [[(DraggedStack*)bit bits] objectAtIndex: 0];
130 if( ! bottomSrc.faceUp )
137 - (BOOL) canBit: (Bit*)bit moveFrom: (id<BitHolder>)src to: (id<BitHolder>)dst
139 if( src==_deck || dst==_deck || dst==_sink )
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];
147 bottomSrc = (PlayingCard*)bit;
150 if( [dst isKindOfClass: [Deck class]] ) {
151 // Dragging to an ace pile:
152 if( ! [bit isKindOfClass: [Card class]] )
154 topDst = (PlayingCard*) ((Deck*)dst).topCard;
156 return bottomSrc.rank == kRankAce;
158 return bottomSrc.suit == topDst.suit && bottomSrc.rank == topDst.rank+1;
161 // Dragging to a card stack:
162 topDst = (PlayingCard*) ((Stack*)dst).topBit;
164 return bottomSrc.rank == kRankKing;
166 return bottomSrc.color != topDst.color && bottomSrc.rank == topDst.rank-1;
171 - (Player*) checkForWinner
173 for( CardSuit suit=kSuitClubs; suit<=kSuitSpades; suit++ )
174 if( _aces[suit].cards.count < 13 )
176 return _currentPlayer;