Source/PlayingCard.m
author Jens Alfke <jens@mooseyard.com>
Fri Mar 07 11:43:02 2008 -0800 (2008-03-07)
changeset 0 e9f7ba4718e1
child 1 3eb7be1dd7b6
permissions -rw-r--r--
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.
     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 "PlayingCard.h"
    24 #import "QuartzUtils.h"
    25 
    26 
    27 @implementation PlayingCard
    28 
    29 
    30 + (NSRange) serialNumberRange;
    31 {
    32     return NSMakeRange(1,52);
    33 }
    34 
    35 
    36 - (CALayer*) createFront
    37 {
    38     CALayer *front = [super createFront];
    39     NSString *name = [NSString stringWithFormat: @"%@%@",
    40                       self.rankString, self.suitString];
    41     
    42     CGColorRef suitColor = self.suitColor;
    43     CATextLayer *label;
    44     label = AddTextLayer(front, name, [NSFont systemFontOfSize: 18],
    45                          kCALayerMaxXMargin | kCALayerMinYMargin);
    46     label.foregroundColor = suitColor;
    47     label = AddTextLayer(front, name, [NSFont systemFontOfSize: 18],
    48                          kCALayerMaxXMargin | kCALayerMaxYMargin);
    49     label.foregroundColor = suitColor;
    50     label.anchorPoint = CGPointMake(1,1);
    51     [label setValue: [NSNumber numberWithFloat: M_PI] forKeyPath: @"transform.rotation"];
    52     
    53     label = AddTextLayer(front, self.faceSymbol, [NSFont systemFontOfSize: 80],
    54                          kCALayerWidthSizable | kCALayerHeightSizable);
    55     label.foregroundColor = suitColor;
    56     return front;
    57 }
    58 
    59 
    60 - (CardRank) rank       {return (self.serialNumber-1)%13 + 1;}
    61 - (CardSuit) suit       {return (self.serialNumber-1)/13;}
    62 
    63 - (CardColor) color
    64 {
    65     CardSuit suit = self.suit;
    66     return suit==kSuitDiamonds || suit==kSuitHearts ?kColorRed :kColorBlack;
    67 }
    68 
    69 
    70 - (NSString*) suitString
    71 {
    72     return [@"\u2663\u2666\u2665\u2660" substringWithRange: NSMakeRange(self.suit,1)];
    73 }
    74 
    75 - (NSString*) rankString
    76 {
    77     CardRank rank = self.rank;
    78     if( rank == 10 )
    79         return @"10";
    80     else
    81         return [@"A234567890JQK" substringWithRange: NSMakeRange(rank-1,1)];
    82 }
    83 
    84 - (CGColorRef) suitColor
    85 {
    86     static CGColorRef kSuitColor[4];
    87     if( ! kSuitColor[0] ) {
    88         kSuitColor[0] = kSuitColor[3] = kBlackColor;
    89         kSuitColor[1] = kSuitColor[2] = CGColorCreateGenericRGB(1, 0, 0, 1);
    90     }
    91     return kSuitColor[self.suit];
    92 }
    93 
    94 
    95 - (NSString*) faceSymbol
    96 {
    97     int rank = self.rank;
    98     if( rank < kRankJack )
    99         return self.suitString;
   100     else
   101         return [@"\u265E\u265B\u265A" substringWithRange: NSMakeRange(rank-kRankJack,1)]; // actually chess symbols
   102 }
   103 
   104 
   105 - (NSString*) description
   106 {
   107     return [NSString stringWithFormat: @"%@[%@%@]",self.class,self.rankString,self.suitString];
   108 }
   109 
   110 
   111 @end