forked from lean-poker/poker-player-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.cpp
512 lines (432 loc) · 18.6 KB
/
player.cpp
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
#include "player.h"
#include "strategy.h"
#include <initializer_list>
#include <vector>
#include <algorithm>
#include <iostream>
//Continous delivery poker
const char* Player::VERSION = "Rounders poker strategy";
//////////////////////////////////
//START of Code section for Laja
//PUBLIC interface
bool HandEvaluator::selfTest(){
std::cout << "Running HandEvaluator self Test" << std::endl;
bool testResult=true;
//create Device Under Test
HandEvaluator DUT;
//init test input
//TC 0
{
GameState testState;
testState.hand_cards.push_back({CardType::A, CardColor::HEARTS});
testState.hand_cards.push_back({CardType::A, CardColor::SPADES});
std::vector<CardType> testRange;
testRange.push_back(CardType::A);
testRange.push_back(CardType::_2);
testRange.push_back(CardType::SUITED);
if( !DUT.isHigherOrEqual( testState, testRange ) ) {
std::cout << "Range Test 0 failed " << std::endl;
testResult = false;
}
}
//TC 1
{
GameState testState;
testState.hand_cards.push_back({CardType::A, CardColor::HEARTS});
testState.hand_cards.push_back({CardType::J, CardColor::SPADES});
std::vector<CardType> testRange;
testRange.push_back(CardType::A);
testRange.push_back(CardType::_2);
testRange.push_back(CardType::SUITED);
if( DUT.isHigherOrEqual( testState, testRange ) ) {
std::cout << "Range Test 1 failed " << std::endl;
testResult = false;
}
}
//TC 2
{
GameState testState;
testState.hand_cards.push_back({CardType::A, CardColor::HEARTS});
testState.hand_cards.push_back({CardType::J, CardColor::HEARTS});
std::vector<CardType> testRange;
testRange.push_back(CardType::A);
testRange.push_back(CardType::_2);
testRange.push_back(CardType::SUITED);
if( !DUT.isHigherOrEqual( testState, testRange ) ) {
std::cout << "Range Test 2 failed " << std::endl;
testResult = false;
}
}
//TC 3
{
GameState testState;
testState.hand_cards.push_back({CardType::K, CardColor::HEARTS});
testState.hand_cards.push_back({CardType::_2, CardColor::SPADES});
std::vector<CardType> testRange;
testRange.push_back(CardType::K);
testRange.push_back(CardType::_10);
if( DUT.isHigherOrEqual( testState, testRange ) ) {
std::cout << "Range Test 3 failed " << std::endl;
testResult = false;
}
}
//TC 4
{
GameState testState;
testState.hand_cards.push_back({CardType::K, CardColor::HEARTS});
testState.hand_cards.push_back({CardType::K, CardColor::SPADES});
std::vector<CardType> testRange;
testRange.push_back(CardType::_10);
testRange.push_back(CardType::_10);
if( !DUT.isHigherOrEqual( testState, testRange ) ) {
std::cout << "Range Test 4 failed " << std::endl;
testResult = false;
}
}
//TC 5
{
GameState testState;
testState.hand_cards.push_back({CardType::A, CardColor::HEARTS});
testState.hand_cards.push_back({CardType::Q, CardColor::SPADES});
std::vector<CardType> testRange;
testRange.push_back(CardType::_8);
testRange.push_back(CardType::_8);
if( DUT.isHigherOrEqual( testState, testRange ) ) {
std::cout << "Range Test 4 failed " << std::endl;
testResult = false;
}
}
if(testResult){
std::cout << "Self Test OK" << std::endl;
} else {
std::cout << "Self Test FAILED" << std::endl;
}
return testResult;
};
bool HandEvaluator::isHigherOrEqual(const GameState ¤tState, std::vector<CardType> rangeVec){
//init a vector for the current hand
std::vector<CardType> handVec = {currentState.hand_cards[0].type, currentState.hand_cards[1].type};
std::vector<CardType> commCardVec;
for(const auto& c : currentState.comm_cards)
commCardVec.push_back(c.type);
//sorting the input vectors
std::sort(rangeVec.begin(), rangeVec.end());
std::sort(handVec.begin(), handVec.end());
std::sort(commCardVec.begin(), commCardVec.end());
switch (rangeVec.size()) {
case 1:
//Determine hand
switch (rangeVec[0]) {
case CardType::FLUSH :
{
std::vector<Card> tmpVec;
tmpVec.insert(tmpVec.end(), currentState.hand_cards.begin(), currentState.hand_cards.end());
tmpVec.insert(tmpVec.end(), currentState.comm_cards.begin(), currentState.comm_cards.end());
return std::all_of(tmpVec.begin(), tmpVec.end(), [&tmpVec](const Card & c){return c.color==tmpVec.front().color;});
}
case CardType::TOP_PAIR:
{
if(commCardVec.size()<3){
return false;
} else{
return (commCardVec.back()==handVec[0] || commCardVec.back()==handVec[1]) || ((handVec[0]==handVec[1]) && (handVec[0]>commCardVec.back()));
}
}
case CardType::MID_PAIR:
{
if(commCardVec.size()<3){
return false;
} else{
CardType topCard = commCardVec.back();
commCardVec.pop_back();
return (commCardVec.back()==handVec[0] || commCardVec.back()==handVec[1]) || ((handVec[0]==handVec[1]) && (handVec[0]>commCardVec.front()) && (handVec[0]<topCard));
}
}
case CardType::LOW_PAIR:
{
if(commCardVec.size()<3){
return false;
} else{
return (commCardVec.front()==handVec[0] || commCardVec.front()==handVec[1]) || ((handVec[0]==handVec[1]) && (handVec[0]< commCardVec.front()));
}
}
default:
return false;
}
break;
case 2:
//if the range to be checked is unsuited
return HandEvaluator::checkInRange(handVec, rangeVec);
break;
case 3:
//if the range to be checked is suited
if ((currentState.hand_cards[0].color == currentState.hand_cards[1].color) || (currentState.hand_cards[0].type==currentState.hand_cards[1].type)){
return HandEvaluator::checkInRange(handVec, rangeVec);
} else {
return false;
}
default:
//by default return false
return false;
break;
}
}
//PRIVATE interface
bool HandEvaluator::checkInRange(std::vector<CardType> handVec, std::vector<CardType> rangeVec){
if(rangeVec[0]==rangeVec[1]){
if(handVec[0]==handVec[1]){
return ((handVec[0]>=rangeVec[0]));
} else {
return false;
}
} else {
return ((rangeVec[1]==handVec[1]) && (handVec[0]>=rangeVec[0]));
}
};
//END of Code section for Laja
//////////////////////////////////
//////////////////////////////////
//START of Code section for Gabor
//END of Code section for Gabor
//////////////////////////////////
//////////////////////////////////
//START of Code section for Andris
//END of Code section for Andris
//////////////////////////////////
//////////////////////////////////
//START of Code section for Tibi
CardType decodeCardType(std::string s)
{
if (s == "2")
return CardType::_2;
if (s == "3")
return CardType::_3;
if (s == "4")
return CardType::_4;
if (s == "5")
return CardType::_5;
if (s == "6")
return CardType::_6;
if (s == "7")
return CardType::_7;
if (s == "8")
return CardType::_8;
if (s == "9")
return CardType::_9;
if (s == "10")
return CardType::_10;
if (s == "J")
return CardType::J;
if (s == "Q")
return CardType::Q;
if (s == "K")
return CardType::K;
if (s == "A")
return CardType::A;
}
// Suit of the card. Possible values are: clubs,spades,hearts,diamonds
CardColor decodeCardColor(std::string s)
{
if (s == "clubs")
return CardColor::CLUBS;
if (s == "spades")
return CardColor::SPADES;
if (s == "hearts")
return CardColor::HEARTS;
if (s == "diamonds")
return CardColor::DIAMONDS;
}
bool Player::fillState(GameState& gs, json::Value game_state)
{
gs.small_blind = game_state["small_blind"].ToInt();
gs.call_value = game_state["current_buy_in"].ToInt();
const int players_length = game_state["players"].ToArray().size();
const int dealer_index = game_state["dealer"].ToInt();
const int me_index = game_state["in_action"].ToInt();
json::Value me = game_state["players"].ToArray()[me_index];
gs.stack = me["stack"];
gs.call_value -= me["bet"].ToInt();
gs.half_pot = std::max(game_state["pot"], game_state["minimum_raise"]);
gs.has_A = me["hole_cards"].ToArray()[0]["rank"].ToString() == "A" || me["hole_cards"].ToArray()[1]["rank"].ToString() == "A";
gs.has_pair = me["hole_cards"].ToArray()[0]["rank"].ToString() == me["hole_cards"].ToArray()[1]["rank"].ToString();
gs.hand_cards.push_back({decodeCardType(me["hole_cards"].ToArray()[0]["rank"].ToString()), decodeCardColor(me["hole_cards"].ToArray()[0]["suit"].ToString())});
gs.hand_cards.push_back({decodeCardType(me["hole_cards"].ToArray()[1]["rank"].ToString()), decodeCardColor(me["hole_cards"].ToArray()[1]["suit"].ToString())});
const int comm_card_num = game_state["community_cards"].size();
for (int i = 0; i < comm_card_num; ++i)
gs.comm_cards.push_back({decodeCardType(game_state["community_cards"].ToArray()[i]["rank"].ToString()), decodeCardColor(game_state["community_cards"].ToArray()[1]["suit"].ToString())});
gs.round = RoundType::PREFLOP;
switch (gs.comm_cards.size()) {
case 3: gs.round = RoundType::FLOP; break;
case 4: gs.round = RoundType::TURN; break;
case 5: gs.round = RoundType::RIVER; break;
}
// Action
{
gs.action = ActionType::FOLD;
int index = dealer_index;
int counter = players_length;
int raise = 0;
int prev_bet = gs.small_blind * 2;
for (;counter--;index=(index+1)%players_length) {
json::Value p = game_state["players"].ToArray()[index];
if (p["status"] != "out") {
if (p["bet"].ToInt() > prev_bet) {
raise++;
prev_bet = p["bet"].ToInt();
}
}
}
switch (raise) {
case 1: gs.action = ActionType::RAISE_1;
case 2: gs.action = ActionType::RAISE_2;
case 3: gs.action = ActionType::RAISE_3;
case 4: gs.action = ActionType::RAISE_4;
}
}
// Position
{
gs.player_num = 0;
int index = dealer_index;
int counter = players_length;
int pos = static_cast<int>(PositionType::D);
for (;counter--;index=(index+1)%players_length) {
json::Value p = game_state["players"].ToArray()[index];
if (p["status"] != "out") {
gs.player_num++;
pos++;
if (index == me_index) {
gs.position = static_cast<PositionType>(pos);
}
}
}
if (gs.player_num == 2) {
if (gs.position == PositionType::D) {
gs.position = PositionType::SB;
} else {
gs.position = PositionType::BB;
}
}
}
return true;
}
int Player::betRequest(json::Value game_state)
{
GameState gs;
fillState(gs, game_state);
StrategyManager manager;
initStrategies(manager);
Action action = manager.execute(gs);
switch (action.mType) {
case ActionType::ALL_IN:
return gs.stack;
case ActionType::CALL:
return gs.call_value;
case ActionType::FOLD:
return 0;
case ActionType::BET_HALF_POT:
return gs.half_pot;
default:
break;
}
//return (gs.has_A || gs.has_pair )? 1000 : 0;
if (gs.player_num == 5 && gs.action >= ActionType::RAISE_1 &&
(
(gs.hand_cards[0].type == CardType::A && gs.hand_cards[1].type >= CardType::Q) ||
(gs.hand_cards[1].type == CardType::A && gs.hand_cards[0].type >= CardType::Q) ||
(gs.has_pair && gs.hand_cards[0].type >= CardType::J)
)
)
{
return gs.stack;
}
if (gs.player_num == 3 && gs.action >= ActionType::RAISE_1 &&
(
(gs.hand_cards[0].type == CardType::A && gs.hand_cards[1].type >= CardType::_9) ||
(gs.hand_cards[1].type == CardType::A && gs.hand_cards[0].type >= CardType::_9) ||
(gs.hand_cards[0].type == CardType::K && gs.hand_cards[1].type >= CardType::J) ||
(gs.hand_cards[1].type == CardType::K && gs.hand_cards[0].type >= CardType::J) ||
(gs.has_pair && gs.hand_cards[0].type >= CardType::_2)
)
)
{
return gs.stack;
}
if (gs.player_num < 3 && gs.action >= ActionType::RAISE_1 &&
(
(gs.hand_cards[0].type == CardType::A && gs.hand_cards[1].type >= CardType::_2) ||
(gs.hand_cards[1].type == CardType::A && gs.hand_cards[0].type >= CardType::_2) ||
(gs.hand_cards[0].type == CardType::K && gs.hand_cards[1].type >= CardType::_6) ||
(gs.hand_cards[1].type == CardType::K && gs.hand_cards[0].type >= CardType::_6) ||
(gs.hand_cards[0].type == CardType::Q && gs.hand_cards[1].type >= CardType::_8) ||
(gs.hand_cards[1].type == CardType::Q && gs.hand_cards[0].type >= CardType::_8) ||
(gs.hand_cards[0].type == CardType::J && gs.hand_cards[1].type >= CardType::_9) ||
(gs.hand_cards[1].type == CardType::J && gs.hand_cards[0].type >= CardType::_9) ||
(gs.hand_cards[0].type == CardType::_10 && gs.hand_cards[1].type >= CardType::_9) ||
(gs.hand_cards[1].type == CardType::_10 && gs.hand_cards[0].type >= CardType::_9) ||
(gs.has_pair && gs.hand_cards[0].type >= CardType::_2)
)
)
{
return gs.stack;
}
if (gs.player_num < 3 &&
(
(gs.hand_cards[0].type == CardType::A && gs.hand_cards[1].type >= CardType::_2) ||
(gs.hand_cards[1].type == CardType::A && gs.hand_cards[0].type >= CardType::_2) ||
(gs.hand_cards[0].type == CardType::K && gs.hand_cards[1].type >= CardType::_6) ||
(gs.hand_cards[1].type == CardType::K && gs.hand_cards[0].type >= CardType::_6) ||
(gs.hand_cards[0].type == CardType::Q && gs.hand_cards[1].type >= CardType::_8) ||
(gs.hand_cards[1].type == CardType::Q && gs.hand_cards[0].type >= CardType::_8) ||
(gs.hand_cards[0].type == CardType::J && gs.hand_cards[1].type >= CardType::_9) ||
(gs.hand_cards[1].type == CardType::J && gs.hand_cards[0].type >= CardType::_9) ||
(gs.hand_cards[0].type == CardType::_10 && gs.hand_cards[1].type >= CardType::_9) ||
(gs.hand_cards[1].type == CardType::_10 && gs.hand_cards[0].type >= CardType::_9) ||
(gs.hand_cards[0].type == CardType::_9 && gs.hand_cards[1].type >= CardType::_8) ||
(gs.hand_cards[1].type == CardType::_9 && gs.hand_cards[0].type >= CardType::_8) ||
(gs.hand_cards[0].type == CardType::_8 && gs.hand_cards[1].type >= CardType::_7) ||
(gs.hand_cards[1].type == CardType::_8 && gs.hand_cards[0].type >= CardType::_7) ||
(gs.hand_cards[0].type == CardType::_7 && gs.hand_cards[1].type >= CardType::_6) ||
(gs.hand_cards[1].type == CardType::_7 && gs.hand_cards[0].type >= CardType::_6) ||
(gs.has_pair && gs.hand_cards[0].type >= CardType::_2)
)
)
{
return gs.stack;
}
return ((gs.hand_cards[0].type == CardType::A && gs.hand_cards[1].type >= CardType::J) ||
(gs.hand_cards[1].type == CardType::A && gs.hand_cards[0].type >= CardType::J) ||
(gs.hand_cards[0].type == CardType::K && gs.hand_cards[1].type >= CardType::Q && gs.hand_cards[0].color == gs.hand_cards[1].color) ||
(gs.hand_cards[1].type == CardType::K && gs.hand_cards[0].type >= CardType::Q && gs.hand_cards[0].color == gs.hand_cards[1].color) ||
//(gs.hand_cards[0].type == CardType::Q && gs.hand_cards[1].type > CardType::J && gs.hand_cards[0].color == gs.hand_cards[1].color) ||
//(gs.hand_cards[1].type == CardType::Q && gs.hand_cards[0].type > CardType::J && gs.hand_cards[0].color == gs.hand_cards[1].color) ||
(gs.has_pair && gs.hand_cards[0].type >= CardType::_9)) ? 1000 :
(
(
(gs.position == PositionType::D) &&
(gs.action == ActionType::FOLD || gs.action == ActionType::CALL) &&
(
(gs.hand_cards[0].type == CardType::A && gs.hand_cards[1].type >= CardType::_6) ||
(gs.hand_cards[1].type == CardType::A && gs.hand_cards[0].type >= CardType::_6) ||
(gs.hand_cards[0].type == CardType::K && gs.hand_cards[1].type >= CardType::_10 && gs.hand_cards[0].color == gs.hand_cards[1].color) ||
(gs.hand_cards[1].type == CardType::K && gs.hand_cards[0].type >= CardType::_10 && gs.hand_cards[0].color == gs.hand_cards[1].color) ||
(gs.has_pair && gs.hand_cards[0].type >= CardType::_5)
)
) ||
(
(gs.position == PositionType::SB || gs.position == PositionType::BB) &&
(gs.action == ActionType::FOLD || gs.action == ActionType::CALL) &&
(
(gs.hand_cards[0].type == CardType::A && gs.hand_cards[1].type >= CardType::_2) ||
(gs.hand_cards[1].type == CardType::A && gs.hand_cards[0].type >= CardType::_2) ||
(gs.hand_cards[0].type == CardType::K && gs.hand_cards[1].type >= CardType::_9) ||
(gs.hand_cards[1].type == CardType::K && gs.hand_cards[0].type >= CardType::_9) ||
(gs.has_pair && gs.hand_cards[0].type >= CardType::_2)
)
)? gs.stack : 0
);
}
void Player::showdown(json::Value game_state)
{
}
//END of Code section for Tibi
//////////////////////////////////