Skip to content

Instantly share code, notes, and snippets.

@leogdion
Created June 5, 2018 18:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leogdion/3ad899f5663cf24c62d84aeaf4c08863 to your computer and use it in GitHub Desktop.
Save leogdion/3ad899f5663cf24c62d84aeaf4c08863 to your computer and use it in GitHub Desktop.
How to create playing cards and shuffling them in Swift 4.2
// CaseIterable gives us the allCases properties
enum Suit : CaseIterable {
case clubs, hearts, diamonds, spades
}
// CaseIterable gives us the allCases properties
enum Rank : CaseIterable {
case two, three, four, five, six, seven, eight, nine, ten, jack, queen, king, ace
}
struct PlayingCard {
let suit: Suit
let rank: Rank
public static let all: [PlayingCard] = {
// flatMap to give us a "flat" array of play cards
return Suit.allCases.flatMap{ (suit) -> [PlayingCard] in
return Rank.allCases.map({ (rank) -> PlayingCard in
return PlayingCard(suit: suit, rank: rank)
})
}
}()
}
// using shuffle from Array
let shuffled = PlayingCard.all.shuffled()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment