[Solved] I’m trying to make a card game. How would I make a deck of cards and then deal them?


You may want to redesign how you define a card:

enum Suit: String {
    case heart, diamond, club, spade
}
enum Rank: Int {
    case _1 = 1, _2 = 2, _3 = 3, _4 = 4, _5 = 5, _6 = 6, _7 = 7, _8 = 8, _9 = 9, _10 = 10
    case jack = 11, queen = 12, king = 13
}
struct Card {
    let suit: Suit
    let rank: Rank
}

(or an alternative could be enum Card {case card(suit: Suit, rank: Rank)})

Then defining your deck:

var deck = [Card(suit: .heart, rank: ._1),
            Card(suit: .heart, rank: ._2),
            Card(suit: .heart, rank: ._3),
            Card(suit: .heart, rank: ._4),
            Card(suit: .heart, rank: ._5),
            Card(suit: .heart, rank: ._6),
            // ...
]

Then picking a card from a deck:

func pickRandomCardFromDeck() -> Card {
    return deck.remove(at: Int(arc4random_uniform(UInt32(deck.count))))
}

[edit for comment]

To check for a pair:

extension Card {
    func isPair(with card: Card) -> Bool {
        return rank == card.rank
    }
}

2

solved I’m trying to make a card game. How would I make a deck of cards and then deal them?