Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Object oriented programming is a beautiful thing. Coded this real fast to show you how it works...</p> <pre><code>// // PlayingCard.h // #import &lt;Foundation/Foundation.h&gt; @interface PlayingCard : NSObject - (id)initWithSuit:(NSInteger)suit value:(NSInteger)value; - (BOOL)isAce; - (BOOL)isFace; @end // // PlayingCard.m // #import "PlayingCard.h" @interface PlayingCard () @property(nonatomic, assign) NSInteger suit; @property(nonatomic, assign) NSInteger value; @end @implementation PlayingCard - (id)initWithSuit:(NSInteger)suit value:(NSInteger)value { self = [self init]; if (self) { _suit = suit; _value = value; } return self; } - (BOOL)isAce { return self.value == 0; } - (BOOL)isFace { return self.value &gt; 10; } - (NSString *)suitName { switch (self.suit) { case 0: return @"spades"; break; case 1: return @"diamonds"; break; case 2: return @"clubs"; break; case 3: return @"hearts"; break; default: return @""; break; } } // so it prints nicely. you should consider making the suits enums - (NSString *)description { return [NSString stringWithFormat:@"%@: suit:%@, value:%d", [super description], [self suitName], self.value]; } @end // // CardDeck.h // #import &lt;Foundation/Foundation.h&gt; @class PlayingCard; @interface CardDeck : NSObject + (id)cardDeck; - (void)shuffle; - (NSUInteger)count; - (PlayingCard *)dealOne; @end // // CardDeck.m // #import "CardDeck.h" #import "PlayingCard.h" @interface CardDeck () @property(nonatomic,strong) NSMutableArray *cards; @end @implementation CardDeck + (id)cardDeck { CardDeck *deck = [[self alloc] init]; for (NSInteger suit = 0; suit &lt; 4; suit++) { for (NSInteger value = 0; value &lt; 13; value++) { PlayingCard *card = [[PlayingCard alloc] initWithSuit:suit value:value]; [deck.cards addObject:card]; } } return deck; } - (id)init { self = [super init]; if (self) { _cards = [NSMutableArray array]; } return self; } - (void)shuffle { NSMutableArray *shuffledCards = [NSMutableArray array]; for (int i=0; i&lt;52; i++) { int randomIndex = arc4random_uniform(52-i); [shuffledCards addObject:[self.cards objectAtIndex:randomIndex]]; [self.cards removeObjectAtIndex:randomIndex]; } self.cards = shuffledCards; } - (NSUInteger)count { return self.cards.count; } - (PlayingCard *)dealOne { PlayingCard *card = [self.cards lastObject]; [self.cards removeLastObject]; return card; } @end </code></pre> <p>Call it like this ...</p> <pre><code>CardDeck *deck = [CardDeck cardDeck]; // try it with and without shuffle [deck shuffle]; while (deck.count) { PlayingCard *card = [deck dealOne]; NSLog(@"%@", card); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload