In this lab, you will experiment with Objective C fundamentals in a Mac terminal application that does not display any user interface. Everything you do in this lab is applicable to iOS as well – we will not use any API that is not available in iOS.
Begin by creating an Xcode project of type OS X > Application > Command Line Tool. (In the Type combo box, select “Foundation” if it isn’t selected.) Add classes as necessary per the instructions below using File > New > File, OS X > Cocoa > Objective C Class.
Implement a PlayingCard class that has the following methods, initializers, and properties:
NSString* suit
propertyNSUInteger rank
property (valid values are 1-13, where 1 = “Ace”, …, 13 = “King”)NSString* contents
read-only property that returns the card name (e.g. “Jack of Spades”)(NSArray *)validSuits
class method that returns an array of valid suits (“Diamonds”, “Hearts”, “Spades”, “Clubs”)(NSArray *)validRanks
method that returns an array of valid ranks (1-13) – you might need the[NSNumber numberWithUnsignedInteger:]
class method here, or use the @ literal syntax- Initializer:
(id)initWithRank:(NSUInteger)rank suit:(NSString *)suit
which checks that the rank and suit are valid
Implement a Deck class that has the following methods:
(void)addCard:(PlayingCard *)card
– stores the card in an NSMutableArray(PlayingCard *)drawRandomCard
– draws a random card from the array; use the arc4random() method to obtain a random number. Remove the card before returning it.
Implement a PlayingDeck class that derives from Deck.
- In its init method, add all 52 playing cards to the deck.
- Make sure you use the
[Card validSuits]
and[Card validRanks]
methods.
Test your code by creating a PlayingDeck instance in the main.m file, drawing a few random cards, and using the NSLog function to print out the cards’ contents.