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)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…