Doing all of the cards that way will take forever. You only had eight of the 52 cards in a deck. Besides, it's going to be incredibly difficult to do the kind of comparisons you're thinking about if every kind of card is a different class. You should probably do it like this, with just one Card class:
enum CardSuit: Int {
case Diamonds = 0, Clubs, Hearts, Spades
}
enum CardValue: Int {
case Two = 2, Three ,Four , Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King, Ace
}
class Card {
var value: CardValue
var suit: CardSuit
init(value initialValue: CardValue, suit initialSuit: CardSuit) {
value = initialValue
suit = initialSuit
}
}
//Now look at how easy it is to create individual cards
let twoOfHearts = Card(value: .Two, suit: .Hearts)
let threeOfHearts = Card(value: .Three, suit: .Hearts)
let twoOfSpades = Card(value: .Two, suit: .Spades)
let threeOfSpades = Card(value: .Three, suit: .Spades)
let twoOfClubs = Card(value: .Two, suit: .Clubs)
let threeOfClubs = Card(value: .Three, suit: .Clubs)
let twoOfDiamonds = Card(value: .Two, suit: .Diamonds)
let threeOfDiamonds = Card(value: .Three, suit: .Diamonds)
let array = [twoOfHeards, threeOfSpades, twoOfClubs, twoOfDiamonds]
Here's my earlier code:
for i in 1..<array.count {
let currentCard = array[i], previousCard = array[i - 1];
if currentCard.suit == previousCard.suit {
addPoints(6)
} else if currentCard.value == previousCard.value {
addPoints(3)
}
}
I'll start with line 1. The keyword "for" indicates a loop, and "i" is the number indicating the current iteration (pass through) the loop. The "i in 1..<array.count" means that the loop runs from i = 1 and stops just short of reaching the total number of cards in the array. Normally you'd start out with i = 0 because arrays' first objects are at index 0, but I'm starting at 1 here because you're comparing two cards in order and the first card has nothing above it to compare with. I end at the array's count minus one because, again, arrays start at 0. If you have an array with five cards, the first one will be at 0 and the last one will be at 4.
Now, for line 2. Not much going on there—all I do is grab the current card ( array[i] ) and the one right before it ( array[i - 1] ) so I can compare them more easily. I could have used two separate lines to do the same thing (which probably would have been less confusing for you—sorry 🙂)
let currentCard = array[i]
let previousCard = array[i - 1];
Does all of that make sense? It's perfectly fine to be 🙂 or 😐 or 😕 or even 😮 — these concepts are not exactly the most intuitive…