-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayer.cpp
More file actions
408 lines (336 loc) · 12.6 KB
/
Player.cpp
File metadata and controls
408 lines (336 loc) · 12.6 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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
//
// Created by 차승준 on 25. 5. 25.
//
#include "Player.h"
#include <random>
Player::Player() {
this->hand.clear();
this->prob.clear();
}
void Player::setAllShown() {
for (auto & i : hand) {
i.shown = true;
}
}
bool Player::contains(const std::vector<int>& vec, int target){
return std::find(vec.begin(), vec.end(), target) != vec.end();
}
std::vector<Card>& Player::getHand() {
return hand;
}
void Player::drawCard(const Card& card) {
hand.push_back(card);
std::sort(hand.begin(), hand.end(), [](const Card& a, const Card& b) {
return a.seq < b.seq;
});
}
int Player::cardAmount() {
return hand.size();
}
int Player::shownCards() {
int count = 0;
for (auto & i : hand) {
if (i.shown) count++;
}
return count;
}
void Player::initializeProb(const std::vector<Card>& humanHand){
// seqVec -> Vector of cards(seq) that the computer know about
seqVec.clear();
for (const auto& card: hand){
seqVec.push_back(card.seq);
}
std::sort(seqVec.begin(), seqVec.end());
blackValVec.clear();
whiteValVec.clear();
for (int seq : seqVec){
if (seq%2==0) blackValVec.push_back(seq/2);
else whiteValVec.push_back(seq/2);
}
// count and index black/white cards in humanHand
int blackCount = 0;
int blackIdx = 0;
int whiteCount = 0;
int whiteIdx = 0;
for (const auto& card : humanHand) {
if (card.color == "black") blackCount++;
else whiteCount++;
}
// initialize prob vector - color, values (black, white)
prob.clear();
for (const auto & i : humanHand) {
probData newProb;
newProb.color = i.color;
if (newProb.color == "black") {
for (int j = blackIdx; j < 13 - (blackCount-blackIdx); ++j) {
if (!contains(blackValVec,j)) newProb.values.push_back(j);
}
blackIdx++;
} else {
for (int j = whiteIdx; j < 13 - (whiteCount-whiteIdx); ++j) {
if (!contains(whiteValVec,j)) newProb.values.push_back(j);
}
whiteIdx++;
}
prob.push_back(newProb);
}
adjustProb(humanHand);
}
void Player::adjustProb(const std::vector<Card> &humanHand) {
// count and index black/white cards in humanHand
std::vector<int> whiteIdxes;
std::vector<int> blackIdxes;
for (int i = 0; i < humanHand.size(); i++) {
if (humanHand[i].color == "black") blackIdxes.push_back(i);
else whiteIdxes.push_back(i);
}
/// BLACK + BLACK / WHITE + WHITE CARD RELATIONSHIPS
int lowerBound;
int upperBound;
// Black card relationship
for (int i = 0; i < blackIdxes.size(); i++){
if (humanHand[blackIdxes[i]].shown) continue;
if (i==0) lowerBound = -1;
else lowerBound = *std::min_element(
prob[blackIdxes[i - 1]].values.begin(),
prob[blackIdxes[i - 1]].values.end()
);
if (i==blackIdxes.size()-1) upperBound = 12;
else upperBound = *std::max_element(
prob[blackIdxes[i + 1]].values.begin(),
prob[blackIdxes[i + 1]].values.end()
);
// remove values leq/geq than lowerBound/upperBound
auto& vals = prob[blackIdxes[i]].values;
vals.erase(std::remove_if(vals.begin(), vals.end(), [&](int val) {
return val <= lowerBound;
}), vals.end());
vals.erase(std::remove_if(vals.begin(), vals.end(), [&](int val) {
return val >= upperBound;
}), vals.end());
}
// White card relationship - same with black
for (int i = 0; i < whiteIdxes.size(); i++){
if (humanHand[whiteIdxes[i]].shown) continue;
if (i==0) lowerBound = -1;
else lowerBound = *std::min_element(
prob[whiteIdxes[i - 1]].values.begin(),
prob[whiteIdxes[i - 1]].values.end()
);
if (i==whiteIdxes.size()-1) upperBound = 12;
else upperBound = *std::max_element(
prob[whiteIdxes[i + 1]].values.begin(),
prob[whiteIdxes[i + 1]].values.end()
);
// remove values leq/geq than lowerBound/upperBound
auto& vals = prob[whiteIdxes[i]].values;
vals.erase(std::remove_if(vals.begin(), vals.end(), [&](int val) {
return val <= lowerBound;
}), vals.end());
vals.erase(std::remove_if(vals.begin(), vals.end(), [&](int val) {
return val >= upperBound;
}), vals.end());
}
/// BLACK + WHITE CARD RELATIONSHIPS
// when there are at least one white card
if (!whiteIdxes.empty()){
int left_closest_black_idx = -1;
int right_closest_black_idx = -1;
// get the right/left closest black card and fix the value vector
for (int whiteIdx : whiteIdxes){
if (humanHand[whiteIdx].shown) continue;
left_closest_black_idx = -1;
right_closest_black_idx = -1;
for (int j = 0; j < whiteIdx; j++){
if (humanHand[j].color == "black") left_closest_black_idx = j;
}
for (int j = humanHand.size()-1; j > whiteIdx; j--){
if (humanHand[j].color == "black") right_closest_black_idx = j;
}
// Delete values that are smaller(larger) than min(max) lcb(rcb)
if (left_closest_black_idx != -1) {
lowerBound = *std::min_element(
prob[left_closest_black_idx].values.begin(),
prob[left_closest_black_idx].values.end()
);
auto& vals = prob[whiteIdx].values;
vals.erase(std::remove_if(vals.begin(), vals.end(), [&](int val) {
return val < lowerBound;
}), vals.end());
}
if (right_closest_black_idx != -1) {
upperBound = *std::max_element(
prob[right_closest_black_idx].values.begin(),
prob[right_closest_black_idx].values.end()
);
auto& vals = prob[whiteIdx].values;
vals.erase(std::remove_if(vals.begin(), vals.end(), [&](int val) {
return val >= upperBound;
}), vals.end());
}
}
}
// when there are at least one black card
if (!blackIdxes.empty()){
int left_closest_white_idx = -1;
int right_closest_white_idx = -1;
// get the right/left closest white card and fix the value vector
for (int blackIdx : blackIdxes){
if (humanHand[blackIdx].shown) continue;
left_closest_white_idx = -1;
right_closest_white_idx = -1;
for (int j = 0; j < blackIdx; j++){
if (humanHand[j].color == "white") left_closest_white_idx = j;
}
for (int j = humanHand.size()-1; j > blackIdx; j--){
if (humanHand[j].color == "white") right_closest_white_idx = j;
}
// Delete values that are smaller(larger) than min(max) lcw(rcw)
if (left_closest_white_idx != -1) {
lowerBound = *std::min_element(
prob[left_closest_white_idx].values.begin(),
prob[left_closest_white_idx].values.end()
);
auto& vals = prob[blackIdx].values;
vals.erase(std::remove_if(vals.begin(), vals.end(), [&](int val) {
return val <= lowerBound;
}), vals.end());
}
if (right_closest_white_idx != -1) {
upperBound = *std::max_element(
prob[right_closest_white_idx].values.begin(),
prob[right_closest_white_idx].values.end()
);
auto& vals = prob[blackIdx].values;
vals.erase(std::remove_if(vals.begin(), vals.end(), [&](int val) {
return val > upperBound;
}), vals.end());
}
}
}
// FOR DEBUGGING
if (debugMode) debugScreen(humanHand);
}
void Player::updateProb(std::vector<Card>& humanHand){
// Update seqVec - include human's shown cards from seqVec
for (const auto& card : humanHand){
if (card.shown && !contains(seqVec, card.seq)) seqVec.push_back(card.seq);
}
// Update seqVec - include new cards that the computer owns
for (const auto& card : hand){
if (!contains(seqVec, card.seq)) seqVec.push_back(card.seq);
}
std::sort(seqVec.begin(), seqVec.end());
blackValVec.clear();
whiteValVec.clear();
for (int seq : seqVec){
if (seq%2==0) blackValVec.push_back(seq/2);
else whiteValVec.push_back(seq/2);
}
// Add newly added card to prob
while (prob.size() < humanHand.size()) {
probData newData;
int newlyDrawnIdx;
for (int i = 0; i < humanHand.size(); i++){
if (humanHand[i].newlyDrawn) {
newData.color = humanHand[i].color;
newlyDrawnIdx = i;
break;
}
}
// add values that are not in ValVec
if (newData.color=="black"){
for (int i = 0; i < 12; i++){
if (!contains(blackValVec,i)) newData.values.push_back(i);
}
}
else {
for (int i = 0; i < 12; i++){
if (!contains(whiteValVec,i)) newData.values.push_back(i);
}
}
prob.insert(prob.begin() + newlyDrawnIdx, newData);
}
// If shown, collapse into known value
for (int i = 0; i < humanHand.size(); i++) {
if (humanHand[i].shown) prob[i].values = {humanHand[i].number};
}
// For hidden cards, get rid of numbers within probs that is in ValVec
for (int i = 0; i < prob.size(); i++) {
if (humanHand[i].shown) continue;
if (prob[i].color=="black"){
prob[i].values.erase(
std::remove_if(prob[i].values.begin(), prob[i].values.end(),
[&](int x) {
return std::find(blackValVec.begin(), blackValVec.end(), x) != blackValVec.end();
}),
prob[i].values.end());
}
else{
prob[i].values.erase(
std::remove_if(prob[i].values.begin(), prob[i].values.end(),
[&](int x) {
return std::find(whiteValVec.begin(), whiteValVec.end(), x) != whiteValVec.end();
}),
prob[i].values.end());
}
}
// adjust neighboring cards values
adjustProb(humanHand);
}
void Player::deleteFromProb(int probIdx, int target) {
auto& vec = prob[probIdx].values;
vec.erase(std::remove(vec.begin(), vec.end(), target), vec.end());
}
std::tuple<int, int> Player::guessingAlgorithm(const std::vector<Card>& humanHand, bool additional) {
int minSize = 12;
int targetIdx = -1;
// find the card that has the smallest value vector
for (int i = 0; i < prob.size(); i++) {
if (!humanHand[i].shown && prob[i].values.size() < minSize) {
minSize = prob[i].values.size();
targetIdx = i;
}
}
if (additional){
if (minSize > 2) return {-1, -1};
else if (minSize == 1) return {targetIdx, prob[targetIdx].values[0]};
}
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> dist(0, prob[targetIdx].values.size() - 1);
int guessNum = prob[targetIdx].values[dist(gen)];
return {targetIdx, guessNum};
}
void Player::debugScreen(const std::vector<Card>& humanHand) {
std::cout << "\n **COMPUTER CARDS** \n";
for (auto & i : hand){
std::cout << i.color << i.number << " ";
}
std::cout << std::endl;
std::cout << "\n **seqVec** \n";
for (int i : seqVec){
std::cout << i << " ";
}
std::cout << std::endl;
std::cout << "\n **blackValVec** \n";
for (int i : blackValVec){
std::cout << i << " ";
}
std::cout << std::endl;
std::cout << "\n **whiteValVec** \n";
for (int i : whiteValVec){
std::cout << i << " ";
}
std::cout << std::endl;
std::cout << "\n **PROB VECTOR VALUES** \n";
for (int i = 0; i<prob.size(); i++){
auto& vec = prob[i].values;
if (humanHand[i].shown) std::cout << prob[i].color << "(SHOWN) -> ";
else std::cout << prob[i].color << ": ";
for (int j : vec){
std::cout << j << ' ';
}
std::cout << std::endl;
}
}