Don't load Checkers sound files till a game is displayed on a board; this speeds up launch.
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"
27 #import "QuartzUtils.h"
30 #define kStackHeight 500
33 @implementation KlondikeGame
36 + (BOOL) landscapeOriented
46 [self setNumberOfPlayers: 1];
53 CGSize boardSize = _table.bounds.size;
54 CGFloat xSpacing = floor(boardSize.width/7);
56 kCardSize.width = round(xSpacing * 0.9); // 1/7th of width, with 10% gap
57 kCardSize.height = round(kCardSize.width * 1.5);
58 CGFloat gap = xSpacing-kCardSize.width;
59 [Card setCardSize: kCardSize];
61 CGPoint pos = {floor(gap/2)+kCardSize.width/2, floor(boardSize.height-kCardSize.height/2)};
62 _deck = [[[Deck alloc] initWithCardsOfClass: [PlayingCard class]] autorelease];
65 [_table addSublayer: _deck];
68 _sink = [[[Deck alloc] init] autorelease];
70 [_table addSublayer: _sink];
73 for( CardSuit suit=kSuitClubs; suit<=kSuitSpades; suit++ ) {
75 Deck *aces = [[[Deck alloc] init] autorelease];
77 [_table addSublayer: aces];
81 CGRect stackFrame = {{floor(gap/2), gap},
82 {kCardSize.width, boardSize.height-kCardSize.height-2*gap}};
83 CGPoint startPos = CGPointMake(kCardSize.width/2,kCardSize.height/2);
84 CGSize spacing = {0, floor((stackFrame.size.height-kCardSize.height)/11.0)};
85 for( int s=0; s<7; s++ ) {
86 Stack *stack = [[Stack alloc] initWithStartPos: startPos spacing: spacing];
87 stack.frame = stackFrame;
88 stackFrame.origin.x += xSpacing;
89 stack.backgroundColor = nil; //kAlmostInvisibleWhiteColor;
90 stack.dragAsStacks = YES;
91 [_table addSublayer: stack];
93 // According to the rules, one card should be added to each stack in turn, instead
94 // of populating entire stacks one at a time. However, if one trusts the Deck's
95 // -shuffle method (which uses the random() function, seeded with a high-entropy
96 // cryptographically-strong value), it shouldn't make any difference :-)
97 for( int c=0; c<=s; c++ )
98 [stack addBit: [_deck removeTopCard]];
99 ((Card*)stack.bits.lastObject).faceUp = YES;
104 - (BOOL) clickedBit: (Bit*)bit
106 if( [bit isKindOfClass: [Card class]] ) {
107 Card *card = (Card*)bit;
108 if( card.holder == _deck ) {
109 // Click on deck deals 3 cards to the sink:
110 for( int i=0; i<3; i++ ) {
111 Card *card = [_deck removeTopCard];
113 [_sink addCard: card];
119 } else if( card.holder == _sink ) {
120 // Clicking the sink when the deck is empty re-deals:
122 [_deck addCards: [_sink removeAllCards]];
128 // Click on a card elsewhere turns it face-up:
129 if( ! card.faceUp ) {
139 - (BOOL) canBit: (Bit*)bit moveFrom: (id<BitHolder>)src
141 if( [bit isKindOfClass: [DraggedStack class]] ) {
142 Card *bottomSrc = [[(DraggedStack*)bit bits] objectAtIndex: 0];
143 if( ! bottomSrc.faceUp )
150 - (BOOL) canBit: (Bit*)bit moveFrom: (id<BitHolder>)src to: (id<BitHolder>)dst
152 if( src==_deck || dst==_deck || dst==_sink )
155 // Find the bottom card being moved, and the top card it's moving onto:
156 PlayingCard *bottomSrc;
157 if( [bit isKindOfClass: [DraggedStack class]] )
158 bottomSrc = [[(DraggedStack*)bit bits] objectAtIndex: 0];
160 bottomSrc = (PlayingCard*)bit;
163 if( [dst isKindOfClass: [Deck class]] ) {
164 // Dragging to an ace pile:
165 if( ! [bit isKindOfClass: [Card class]] )
167 topDst = (PlayingCard*) ((Deck*)dst).topCard;
169 return bottomSrc.rank == kRankAce;
171 return bottomSrc.suit == topDst.suit && bottomSrc.rank == topDst.rank+1;
174 // Dragging to a card stack:
175 topDst = (PlayingCard*) ((Stack*)dst).topBit;
177 return bottomSrc.rank == kRankKing;
179 return bottomSrc.color != topDst.color && bottomSrc.rank == topDst.rank-1;
184 - (Player*) checkForWinner
186 for( CardSuit suit=kSuitClubs; suit<=kSuitSpades; suit++ )
187 if( _aces[suit].cards.count < 13 )
189 return self.currentPlayer;