/
CardDeck.swift
126 lines (99 loc) · 3.25 KB
/
CardDeck.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
//
// CardDeck.swift
// CardGame
//
// Created by YOUTH on 2018. 1. 9..
// Copyright © 2018년 JK. All rights reserved.
//
import Foundation
class CardDeck: CustomStringConvertible {
private var cards = [Card]()
var description: String {
return "총 \(self.cards.count)장의 카드가 남아있습니다.\n"
}
init() {
var cards = [Card]()
for shape in Suit.allValues {
for number in Denomination.allValues {
cards.append(Card(suit: shape, denomination: number))
}
}
self.cards = cards
}
func reset() -> CardDeck {
return CardDeck()
}
func shuffle() {
var tempCards = self.cards
var shuffledCards = [Card]()
while 0 < tempCards.count {
let randomIndex = Int(arc4random_uniform(UInt32(tempCards.count)))
let pickedCard = tempCards.remove(at: randomIndex)
shuffledCards.append(pickedCard)
}
self.cards = shuffledCards
}
func removeOne() -> Card {
let randomIndex = Int(arc4random_uniform(UInt32(cards.count)))
let pickedCard = self.cards.remove(at: randomIndex)
return pickedCard
}
func count() -> Int {
return self.cards.count
}
func makeShuffleMessage() -> String {
return "전체 \(self.cards.count)장의 카드를 섞었습니다.\n"
}
func makeCards(_ countOfCards: Int) -> [Card] {
var cards = [Card]()
for _ in 0..<countOfCards {
let picked = removeOne()
cards.append(picked)
}
return cards
}
func makeStack(cards: [Card]) -> CardStack {
return CardStack(cards)
}
func hasEnoughCards(numberOfNeeded: Int) -> Bool {
if numberOfNeeded < self.cards.count {
return true
}
return false
}
}
extension CardDeck {
enum Suit: String, EnumCollection, Comparable {
case heart = "♥️"
case diamond = "♦️"
case clover = "♣️"
case spade = "♠️"
static func <(lhs: CardDeck.Suit, rhs: CardDeck.Suit) -> Bool {
return lhs.hashValue < rhs.hashValue
}
static func ==(lhs: CardDeck.Suit, rhs: CardDeck.Suit) -> Bool {
return lhs.hashValue == rhs.hashValue
}
}
enum Denomination: Int, CustomStringConvertible, EnumCollection, Comparable {
case two = 2, three, four, five, six, seven, eight, nine, ten, eleven, twelve, thirteen, ace
var description: String {
switch self {
case .ace: return "A"
case .eleven: return "J"
case .twelve: return "Q"
case .thirteen: return "K"
default: return String(self.rawValue)
}
}
static func <(lhs: CardDeck.Denomination, rhs: CardDeck.Denomination) -> Bool {
return lhs.rawValue < rhs.rawValue
}
static func ==(lhs: CardDeck.Denomination, rhs: CardDeck.Denomination) -> Bool {
return lhs.rawValue == rhs.rawValue
}
func isContinuous(next: CardDeck.Denomination) -> Bool {
return self.rawValue == (next.rawValue + 1)
}
}
}