Add a method to Card that creates a full deck of cards, with one card of each combination of rank and suit
Here's another way of doing it, this time only using techniques you would have learned up to that point*
First we define the possible ranks and suits, using the respective Rank
and Suit
enums defined previously.
Next we have the function iterate over each rank within each suit, creating a card for each, and finally returning an array of the cards.
struct Card {
var rank: Rank
var suit: Suit
func simpleDescription() -> String {
return "The \(rank.simpleDescription()) of \(suit.simpleDescription())"
}
func createDeck() -> [Card] {
let ranks = [Rank.ace, Rank.two, Rank.three, Rank.four, Rank.five, Rank.six, Rank.seven, Rank.eight, Rank.nine, Rank.ten, Rank.jack, Rank.queen, Rank.king]
let suits = [Suit.spades, Suit.hearts, Suit.diamonds, Suit.clubs]
var deck = [Card]()
for suit in suits {
for rank in ranks {
deck.append(Card(rank: rank, suit: suit))
}
}
return deck
}
}
(* with the notable exception that the tour hadn't explicitly explained how to append to arrays at that point)
Making a playing card in objective C using enums?
Here one way to do it
typedef NS_ENUM(NSInteger, Suit) {
Clubs,
Hearts,
Diamonds,
Spades
};
typedef NS_ENUM(NSInteger, Rank) {
King,
Queen,
Jack,
// ...
Three,
Two,
One
};
@interface Card : NSObject
@property (nonatomic, assign) Suit suit;
@property (nonatomic, assign) Rank rank;
@end
@implementation Card
- (NSString *)stringForSuit
{
static NSDictionary *SuitToString = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
SuitToString = @{
@(Clubs): @"♣︎",
@(Hearts): @"♥︎",
@(Diamonds): @"♦︎",
@(Spades): @"♠︎",
};
});
return SuitToString[@(self.suit)];
}
- (NSString *)stringForRank
{
static NSDictionary *RanksToString = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
RanksToString = @{
@(King): @"King",
@(Queen): @"Queen",
@(Jack): @"Jack",
// ...
@(Three): @"3",
@(Two): @"2",
@(One): @"Ace",
};
});
return RanksToString[@(self.rank)];
}
- (NSString *)description
{
return [NSString stringWithFormat:@"%@ %@",[self stringForRank],[self stringForSuit]];
}
@end
Advantages of using NS_ENUM
explained on NSHipster (mainly, better compiler help)
Although, this would all be fun to write in Swift too... (see this StackOverflow question for example...)
Edit
Other advantage of using ´NS_ENUM´, it generates real Swift enums on the swift side
(source)
Deck of Cards Objects Java
I try your code and it's okay. It seems that you have a bug in another part
class Main {
public static void main(String[] args) {
String[] ranks = { "2", "3", "4", "5", "6", "7", "8", "9", "10",
"jack", "queen", "king", "ace" };
String[] suits = { "spades", "diamonds", "clubs", "hearts" };
int[] pointValues = { 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11 };
deck(ranks, suits, pointValues);
}
public static void deck(String[] ranks, String[] suits, int[] values) {
List<Card> cards = new ArrayList<Card>();
for (int j = 0; j < ranks.length; j++) {
for (String suitString : suits) {
cards.add(new Card(ranks[j], suitString, values[j]));
}
}
System.out.println(cards.size());
}
}
class Card {
String rank;
String suit;
int value;
public Card(String rank, String suit, int value) {
this.rank = rank;
this.suit = suit;
this.value = value;
}
public String getRank() {
return rank;
}
public void setRank(String rank) {
this.rank = rank;
}
public String getSuit() {
return suit;
}
public void setSuit(String suit) {
this.suit = suit;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
}
It prints 52, that it's correct.
How are you shuffling the Deck ? You use Collections.shuffle(cards) or a custom method ?
Having trouble understanding order of evaluation in Python method call
It's pretty simple.
Any time you call a class in Python, you create a new instance of the class. So if you call the Card
class, you create a new instance of class Card
... in other words, you create a new card.
Let's create the ace of spades:
ace_of_spades = Card('A', 's')
But I'm cheating. I looked and saw what strings to pass to Card
when I should be using the pre-defined constants. Let's do it more properly:
ace_of_spades = Card(Card.RANKS[0], Card.SUITS[3])
Hmm, I'm not sure that Card.RANKS[0]
is that easy to understand. Personally I would probably do something like
class Card(object):
RANK_ACE = 'A'
RANK_2 = '2'
# and so on
and then:
ace_of_spades = Card(Card.RANK_ACE, Card.SUIT_SPADES)
Now that we have used the variable name ace_of_spades
to hold a reference to the card, we can use the card:
hand = Hand()
hand.add(ace_of_spades)
But if we just want to get the card into the hand, we don't need that variable name ace_of_spades
. We could remove the name:
del(ace_of_spades)
But there is no need to use the name in the first place. We can simply do:
hand.add(Card(Card.RANK_ACE, Card.SUIT_SPADES))
This creates the card, then immediately passes the newly created card to Hand.add()
to add it to a hand.
You don't, strictly speaking, need to store the ranks and suits inside the Card
class. In Python, it might be more common to put these things outside of the classes, in "module" scope. I would most likely do:
RANK_ACE = 'A'
RANK_2 = '2'
# and so on
SUIT_CLUBS = 'c'
# and so on
class Card(object):
# and so on
But if you are taking a class and your teacher wants you to put constants inside your programs' classes, then do that.
EDIT: You are trying to figure out what self.add()
does in the program.
Here's the source code for it:
# inside class Hand
def add(self, card):
self.cards.append(card)
self
refers to the object. To see how this works, let's imagine you have an instance of class Hand
, saved in the variable h
. If you call h.add(ace_of_spades)
then the self
in this function will be set to the same instance of Hand
that h
refers to.
In fact, the call h.add(ace_of_spades)
is exactly the same as doing this call:
Hand.add(h, ace_of_spades)
When you use the form h.add()
, Python automatically does the same thing as the above call. Python looks at h
and figures out that it is an instance of class Hand
. Then Python looks in class Hand
for a function called add()
. Then Python builds a call similar to the one shown above.
self.cards
refers to a list
object stored inside the Hand
instance. Python lists have a method function, .append()
, that appends an item to the list.
You can read more about classes and method functions here: http://docs.python.org/2/tutorial/classes.html
Related Topics
Concatenate Two Audio Files in Swift and Play Them
Swift & Firebase | Checking If a User Exists with a Username
What's the How to Use an Objective-C Category Within Swift
Build Error When Trying to Override an Initializer in Xcode 6.3 Beta 3
Download File from Server Using Swift
How We Can Get and Read Size of a Text with Geometryreader in Swiftui
How to Detect a Swiftui Touchdown Event with No Movement or Duration
Why Does Filter(_:)'s Predicate Get Called So Many Times When Evaluating It Lazily
Error When Trying to Save Image in Nsuserdefaults Using Swift
Manage Ifaddrs to Return MAC Addresses as Well in Swift
Swift: Decode Imprecise Decimal Correctly
Uitableviewcell: Rounded Corners and Shadow
Extension May Not Contain Stored Property But Why Is Static Allowed
Swift: Setting an Optional Property of a Protocol
How to Get User Home Directory Path (Users/"User Name") Without Knowing the Username in Swift3