-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcards.cpp
More file actions
203 lines (178 loc) · 3.64 KB
/
Copy pathcards.cpp
File metadata and controls
203 lines (178 loc) · 3.64 KB
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#include "cards.h"
#include "card.h"
#include <QRandomGenerator>
#include <QDebug>
Cards::Cards()
{
}
Cards::Cards(const Card &card)
{
add(card);
}
void Cards::add(const Card &card)
{
m_cards.insert(card);
}
void Cards::add(const Cards &cards)
{
m_cards.unite(cards.m_cards);
}
void Cards::add(const QVector<Cards> &cards)
{
for(int i=0; i<cards.count(); ++i)
{
add(cards.at(i));
}
}
Cards &Cards::operator <<(const Card &card)
{
add(card);
return *this;
}
Cards &Cards::operator <<(const Cards &cards)
{
add(cards);
return *this;
}
void Cards::remove(const Card &card)
{
m_cards.remove(card);
}
void Cards::remove(const Cards &cards)
{
m_cards.subtract(cards.m_cards);
}
void Cards::remove(const QVector<Cards> &cards)
{
for(int i=0; i<cards.size(); ++i)
{
remove(cards.at(i));
}
}
int Cards::cardCount()
{
return m_cards.size();
}
bool Cards::isEmpty()
{
return m_cards.isEmpty();
}
void Cards::clear()
{
m_cards.clear();
}
Card::CardPoint Cards::maxPoint()
{
Card::CardPoint max = Card::Card_Begin;
if(!m_cards.isEmpty())
{
for(auto it = m_cards.begin(); it!=m_cards.end(); ++it)
{
if(it->point() > max)
{
max = it->point();
}
}
}
return max;
}
Card::CardPoint Cards::minPoint()
{
Card::CardPoint min = Card::Card_End;
if(!m_cards.isEmpty())
{
for(auto it = m_cards.begin(); it!=m_cards.end(); ++it)
{
if(it->point() < min)
{
min = it->point();
}
}
}
return min;
}
int Cards::pointCount(Card::CardPoint point)
{
int count = 0;
for(auto it = m_cards.begin(); it!=m_cards.end(); ++it)
{
if(it->point() == point)
{
count++;
}
}
return count;
}
bool Cards::contains(const Card &card)
{
return m_cards.contains(card);
}
bool Cards::contains(const Cards &cards)
{
return m_cards.contains(cards.m_cards);
}
Card Cards::takeRandomCard()
{
// 生成一个随机数
int num = QRandomGenerator::global()->bounded(m_cards.size());
QSet<Card>::const_iterator it = m_cards.constBegin();
for(int i=0; i<num; ++i, ++it);
Card card = *it;
m_cards.erase(it);
return card;
}
CardList Cards::toCardList(SortType type)
{
CardList list;
for(auto it = m_cards.begin(); it != m_cards.end(); ++it)
{
list << *it;
}
if(type == Asc)
{
std::sort(list.begin(), list.end(), lessSort);
}
else if(type == Desc)
{
std::sort(list.begin(), list.end(), greaterSort);
}
return list;
}
QString Cards::toCardString()
{
QString st;
for(auto it = m_cards.begin(); it != m_cards.end(); ++it)
{
st += (*it).realpoint();
}
return st;
}
Cards Cards::selectValidCards(QString cards)
{
Cards res = Cards();
int curSuit = Card::Suit_Begin;
for (auto i = 0; i < cards.size(); ++i)
{
Card curCard = Card();
curCard.setPoint(Card::realpoint2point(cards.at(i)));
if (curCard.point() == Card::Card_BJ || curCard.point() == Card::Card_SJ)
{
curSuit = Card::Suit_Begin;
curCard.setSuit(Card::CardSuit(curSuit));
}
else {
do
{
curSuit = curSuit + 1;
if (curSuit == Card::Suit_End)
{
curSuit = Card::Diamond;
}
curCard.setSuit(Card::CardSuit(curSuit));
}
while (!m_cards.contains(curCard));
}
res.add(curCard);
}
return res;
}