Created
June 5, 2018 18:56
-
-
Save leogdion/3ad899f5663cf24c62d84aeaf4c08863 to your computer and use it in GitHub Desktop.
How to create playing cards and shuffling them in Swift 4.2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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