enum Number: Int, CaseIterable, Comparable {
case two = 2, three, four, five, six, seven, eight, nine, ten, jack, queen, king, ace
}
enum Suit: String, CaseIterable {
case spades, hearts, diamonds, clubs
}
extension CaseIterable {
static var random: Self.AllCases.Element {
return Self.allCases.randomElement()!
}
}
extension Card {
static var randomCard: Card {
return .init(number: .random, suit: .random)
}
}
struct Card: CustomStringConvertible, Equatable, Comparable {
let number: Number
let suit: Suit
var description: String {
return "\(number) of \(suit)"
}
}
extension Number {
static func == (left: Number, right: Number) -> Bool {
return left.rawValue == right.rawValue
}
static func < (left: Number, right: Number) -> Bool {
return left.rawValue < right.rawValue
}
}
extension Card {
static func == (left: Card, right: Card) -> Bool {
return left.number == right.number
}
static func < (left: Card, right: Card) -> Bool {
return left.number < right.number
}
static let deck: [Card] = Suit.allCases.map { suit -> [Card] in
Number.allCases.map { number -> Card in
.init(number: number, suit: suit)
}
}.flatMap({$0})
}
let shuffledDeck = Card.deck.shuffled()
let firstNine = shuffledDeck.prefix(9)
for card in firstNine {
print(card)
}
for card in shuffledDeck {
print(card)
}