-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTable.cpp
86 lines (78 loc) · 1.99 KB
/
Table.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
#include "Table.h"
#include <fstream>
#include <sstream>
#include "Utils.h"
#define NORMAL_MIN 5 // Total value
#define SOFT_MIN 13 // Total value
#define PAIR_MIN 1 // Individual value
#define UPCARD_MIN 1 // Individual value
using std::vector;
using std::string;
using std::istringstream;
using std::fstream;
Table Table::basic_strategy = Table("strategy/basic.csv");
static void addToTable(vector<vector<char> > & vec, istringstream & ss) {
vector<char> row;
string val;
while(getline(ss, val, ',')) {
row.push_back(std::stoi(val));
}
vec.push_back(row);
}
Table::Table(string filename) {
fstream fin(filename);
string line, row_name;
getline(fin, line); // Ignore headers
while(getline(fin, line)) {
std::istringstream ss(line);
getline(ss, row_name, ',');
if(row_name.find('-') != string::npos) {
addToTable(soft, ss);
}
else if(row_name.find('|') != string::npos) {
addToTable(pair, ss);
}
else {
addToTable(normal, ss);
}
}
}
Decision Table::getDecision(const Hand & hand, Card upcard) {
size_t upcard_index = (upcard.isSoft() ? 0 : upcard.getValue() - UPCARD_MIN);
char decision;
if(hand.isPair()) {
if(hand.isSoft()) {
decision = pair[0][upcard_index];
}
else {
decision = pair[hand.getCards()[0].getValue() - PAIR_MIN][upcard_index];
}
}
else if(hand.isSoft()) {
decision = soft[hand.totalValue() - SOFT_MIN][upcard_index];
}
else {
decision = normal[hand.totalValue() - NORMAL_MIN][upcard_index];
}
// Convert table value to decision
switch(decision) {
case 1:
return Decision::hit;
case 2:
return Decision::stand;
case 3:
return Decision::split;
case 4:
if(hand.getCards().size() == 2) {
return Decision::double_down;
}
return Decision::hit;
case 5:
if(hand.getCards().size() == 2) {
return Decision::double_down;
}
return Decision::stand;
default:
ERROR("Invalid table value.");
}
}