Created
October 16, 2016 17:58
-
-
Save rayfix/6495f137a34b571506c78ffb396d9879 to your computer and use it in GitHub Desktop.
Playing card abstraction
This file contains hidden or 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
| //: Cards | |
| import Foundation | |
| enum CardColor { | |
| case red, black | |
| } | |
| enum CardSuit: CustomStringConvertible { | |
| case heart, diamond, club, spade | |
| static var all: [CardSuit] { | |
| return [heart, diamond, club, spade] | |
| } | |
| var description: String { | |
| switch self { | |
| case .heart: | |
| return "♥️" | |
| case .diamond: | |
| return "♦️" | |
| case .club: | |
| return "♠️" | |
| case .spade: | |
| return "♣️" | |
| } | |
| } | |
| var color: CardColor { | |
| switch self { | |
| case .heart, .diamond: | |
| return .red | |
| case .club, .spade: | |
| return .black | |
| } | |
| } | |
| } | |
| enum CardRank: Int, CustomStringConvertible { | |
| case c2=2,c3,c4,c5,c6,c7,c8,c9,c10,cJ,cQ,cK,cA | |
| static var all: [CardRank] { | |
| return [c2,c3,c4,c5,c6,c7,c8,c9,c10,cJ,cQ,cK,cA] | |
| } | |
| var scores: [Int] { | |
| switch self { | |
| case .c2,.c3,.c4,.c5,.c6,.c7,.c8,.c9,.c10: | |
| return [self.rawValue] | |
| case .cJ, .cQ, .cK: | |
| return [10] | |
| case .cA: | |
| return [1,11] | |
| } | |
| } | |
| var description: String { | |
| switch self { | |
| case .c2,.c3,.c4,.c5,.c6,.c7,.c8,.c9,.c10: | |
| return String(self.rawValue) | |
| case .cJ: | |
| return "J" | |
| case .cQ: | |
| return "Q" | |
| case .cK: | |
| return "K" | |
| case .cA: | |
| return "A" | |
| } | |
| } | |
| } | |
| struct Card: CustomStringConvertible { | |
| var suit: CardSuit | |
| var rank: CardRank | |
| var color: CardColor { | |
| return suit.color | |
| } | |
| var scores: [Int] { | |
| return rank.scores | |
| } | |
| var description: String { | |
| return "\(rank)\(suit)" | |
| } | |
| } | |
| Card(suit: .club, rank: .c3) | |
| struct Deck { | |
| var cards: [Card] | |
| } | |
| extension Deck { | |
| init() { | |
| cards = | |
| CardSuit.all.flatMap { suit in | |
| CardRank.all.map { rank in | |
| return Card(suit: suit, rank: rank) | |
| } | |
| }.shuffled() | |
| } | |
| mutating func draw() -> Card? { | |
| return cards.popLast() | |
| } | |
| mutating func shuffle() { | |
| cards.shuffle() | |
| } | |
| func shuffled() -> Deck { | |
| var copy = self | |
| copy.shuffle() | |
| return copy | |
| } | |
| var isEmpty: Bool { | |
| return cards.isEmpty | |
| } | |
| } | |
| func random(_ input: Int) -> Int { | |
| return Int(arc4random_uniform(UInt32(input))) | |
| } | |
| func random(_ range: CountableRange<Int>) -> Int { | |
| let spread = range.endIndex - range.startIndex | |
| return random(spread) + range.startIndex | |
| } | |
| extension Array { | |
| mutating func shuffle() { | |
| let n = count | |
| guard n > 1 else { return } | |
| for index in 0..<(n-2) { | |
| let randomIndex = random(index..<count) | |
| guard index != randomIndex else { continue } | |
| swap(&self[index],&self[randomIndex]) | |
| } | |
| } | |
| func shuffled() -> Array { | |
| var copy = self | |
| copy.shuffle() | |
| return copy | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment