Repository navigation
Expand file tree
/
Copy pathCardCounter.cpp
More file actions
304 lines (271 loc) · 8.91 KB
/
Copy pathCardCounter.cpp
File metadata and controls
304 lines (271 loc) · 8.91 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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#include "CardCounter.h"
using namespace std;
CardCounter::CardCounter()
{
}
void CardCounter::SortCardsByAnnounce()
{
if(m_ucAnnounce > ALL_TRUMP_ANNOUNCE )
{
cerr<< "WRONG ANNOUNCE"<< endl;
}
for(int i = 0; i <= SPADES; i++)
{
if(i == m_ucAnnounce || m_ucAnnounce == ALL_TRUMP_ANNOUNCE)
{
m_helper.sort_by_power(m_vRemainingCards.at(i), "ALL_TRUMP");
}
else
{
m_helper.sort_by_power(m_vRemainingCards.at(i), "NO_TRUMP");
}
reverse(m_vRemainingCards.at(i).begin(), m_vRemainingCards.at(i).end());
//the most powerful card should be first for simplicity
}
}
bool CardCounter::InitCounter(unsigned char announce)
{
m_vRemainingCards.clear();
m_ucPCardsCount = 0;
for(int i = 0; i < NUMBER_OF_CARDS; i++)
{
m_aPlayedCards.at(i) = NULLCARD;
}
for (int i = 0; i <= SPADES; i++ )
{
vector<Card> tempColor;
for (int j = 0; j <= Ace; j++ )
{
tempColor.push_back(Card(j, i));
// TODO : Make deck generator func
}
m_vRemainingCards.push_back(tempColor);
}
m_helper.InitHelper(announce);
m_ucAnnounce = announce;
SortCardsByAnnounce();
return true;
}
void CardCounter::Update(const array<Card, NUMBER_OF_PLAYERS>& m_aPlayedTick)
{
for(int i = 0; i < NUMBER_OF_PLAYERS; i++)
{
m_aPlayedCards.at(m_ucPCardsCount) = m_aPlayedTick.at(i);
Card tempCard = m_aPlayedCards.at(m_ucPCardsCount);
m_ucPCardsCount++;
if(m_ucPCardsCount >= 32) return;
if(tempCard.GetColor() == NULLCARD.GetColor())
{
cerr << "UPDATE WITH NULLCARD >?!?" << endl;
throw __throw_bad_function_call;
}
unsigned char remainingCardsSize = m_vRemainingCards.at(tempCard.GetColor()).size();
for(int j = 0; j < remainingCardsSize; j++)
{
if(tempCard == m_vRemainingCards.at(tempCard.GetColor()).at(j))
{
m_vRemainingCards.at(tempCard.GetColor()).erase(m_vRemainingCards.at(tempCard.GetColor()).begin() + j);
//vector:erase forses usage of iterator
break;
}
}
}
}
void CardCounter::UpdateByHands(const array<array<Card, HAND_SIZE>, NUMBER_OF_PLAYERS> &hands,
unsigned char playedCardsCount, bool updateplayedCards)
{
m_ucPCardsCount = playedCardsCount;
for (int i = 0; i <= SPADES; i++ )
{
fill(m_vRemainingCards.at(i).begin(), m_vRemainingCards.at(i).end(), NULLCARD);
}
for(int i = 0; i < NUMBER_OF_PLAYERS; i++)
{
for(int j = 0; j < HAND_SIZE; j++)
{
if(hands.at(i).at(j).GetColor() == NULLCARD.GetColor())
{
continue;
}
m_vRemainingCards.at(hands.at(i).at(j).GetColor()).at(hands.at(i).at(j).GetPower()) = hands.at(i).at(j);
}
}
if (updateplayedCards)
{
for(int i = 0; i < NUMBER_OF_PLAYERS; i++)
{
for(int j = 0; j < HAND_SIZE; j++)
{
if(playedCardsCount == 0)
{
break;
}
if(m_vRemainingCards.at(i).at(j).GetColor() == NULLCARD.GetColor())
{
Card tempCard(j, i);
m_aPlayedCards.at(playedCardsCount) = tempCard;
playedCardsCount--;
}
}
}
}
SortCardsByAnnounce();
}
void CardCounter::UpdateByCurrentDeal(const array<array<Card, NUMBER_OF_PLAYERS>, HAND_SIZE> ¤t_deal,
unsigned char playedCardsCount, bool updateplayedCards)
{
m_ucPCardsCount = playedCardsCount;
for (int i = 0; i <= SPADES; i++ )
{
fill(m_vRemainingCards.at(i).begin(), m_vRemainingCards.at(i).end(), NULLCARD);
}
for(int i = 0; i < HAND_SIZE; i++)
{
for(int j = 0; j < NUMBER_OF_PLAYERS; j++)
{
if(current_deal.at(i).at(j).GetColor() == NULLCARD.GetColor())
{
continue;
}
m_vRemainingCards.at(current_deal.at(i).at(j).GetColor()).at(current_deal.at(i).at(j).GetPower()) = current_deal.at(i).at(j);
}
}
if (updateplayedCards)
{
for(int i = 0; i <= SPADES; i++)
{
for(int j = 0; j < HAND_SIZE; j++)
{
if(playedCardsCount == 0)
{
break;
}
if(m_vRemainingCards.at(i).at(j).GetColor() == NULLCARD.GetColor())
{
Card tempCard(j, i);
m_aPlayedCards.at(playedCardsCount) = tempCard;
playedCardsCount--;
}
}
}
}
SortCardsByAnnounce();
}
vector<Card> CardCounter::GetPlayedCards()
{
vector<Card> playedCards(m_aPlayedCards.begin(), m_aPlayedCards.begin() + m_ucPCardsCount);
return playedCards;
}
vector<Card> CardCounter::GetPlayedbyTrumpCards(const unsigned char& trump)
{
vector<Card> playedCards;
for(int i = 0; i < m_ucPCardsCount; i++)
{
if(m_aPlayedCards.at(i).GetColor() == trump)
{
playedCards.push_back(m_aPlayedCards.at(i));
}
}
return playedCards;
}
vector<Card> CardCounter::GetRemainingCards()
{
vector<Card> remainingCards = m_vRemainingCards.at(0);
remainingCards.reserve(NUMBER_OF_CARDS - m_ucPCardsCount);
remainingCards.insert(remainingCards.end(), m_vRemainingCards.at(1).begin(), m_vRemainingCards.at(1).end());
remainingCards.insert(remainingCards.end(), m_vRemainingCards.at(2).begin(), m_vRemainingCards.at(2).end());
remainingCards.insert(remainingCards.end(), m_vRemainingCards.at(3).begin(), m_vRemainingCards.at(3).end());
return remainingCards;
}
vector<Card> CardCounter::GetRemainingbyTrumpCards(const unsigned char& trump)
{
return m_vRemainingCards.at(trump);
}
unsigned char CardCounter::EvaluateHand(const array<Card, HAND_SIZE> &hand)
{
unsigned char winningCards = 0;
unsigned char handSize = HAND_SIZE - m_ucPCardsCount / NUMBER_OF_PLAYERS;
if (m_ucAnnounce >= NO_TRUMP_ANNOUNCE)
{
for(unsigned char i = 0; i <= SPADES; i++)
{
unsigned char current_card = 0 ;
while(m_helper.findCard(hand, m_vRemainingCards.at(i).at(current_card), handSize, true) != ERROR)
{
if(m_vRemainingCards.at(i).at(current_card).GetColor() == NULLCARD.GetColor() )
{
break; // don't count nullcards in power
}
winningCards++;
current_card++;
}
if(winningCards == handSize)
{
return winningCards;
}
}
}
else
{
unsigned char remainingTrump = 0;
for(int i = 0; i < HAND_SIZE; i++)
{
if(m_vRemainingCards.at(m_ucAnnounce).at(i).GetColor() != NULLCARD.GetColor())
{
remainingTrump++;
}
}
unsigned char current_card = 0 ;
while(m_helper.findCard(hand, m_vRemainingCards.at(m_ucAnnounce).at(current_card), handSize, true) != ERROR)
{
if(m_vRemainingCards.at(m_ucAnnounce).at(current_card).GetColor() == NULLCARD.GetColor() )
{
break; // don't count nullcards in power
}
current_card++;
winningCards++;
if(winningCards == handSize)
{
return winningCards;
}
if(winningCards == remainingTrump)
{
break;
}
}
if(winningCards == remainingTrump)
{
for(unsigned char i = 0; i <= SPADES; i++)
{
if(i == m_ucAnnounce)
{
continue;
}
current_card = 0;
while(m_helper.findCard(hand, m_vRemainingCards.at(i).at(current_card), handSize, true) != ERROR)
{
if(m_vRemainingCards.at(i).at(current_card).GetColor() == NULLCARD.GetColor() )
{
break; // don't count nullcards in power
}
winningCards++;
current_card++;
if(current_card == HAND_SIZE)
{
break; // there are no winning cards
}
}
if(winningCards == handSize)
{
return winningCards;
}
}
}
}
return winningCards;
}
bool CardCounter::IsHandClaimable(const array<Card, HAND_SIZE>& hand)
{
unsigned char handSize = HAND_SIZE - m_ucPCardsCount / NUMBER_OF_PLAYERS;
return handSize == EvaluateHand(hand);
}