-
Notifications
You must be signed in to change notification settings - Fork 1
/
AllHands.cpp
136 lines (124 loc) · 4.18 KB
/
AllHands.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
#include "pch.h"
#include "AllHands.h"
#include "Hand.h"
#include "Deck.h"
const auto fileName = "handRanksByHash.bin";
AllHands::AllHands()
{
if (!std::experimental::filesystem::is_regular_file(fileName))
{
cout << "Computing best hands...";
Compute();
cout << "done.\n";
cout << "Saving to disk...";
Save();
cout << "done.\n";
}
else
{
cout << "Loading best hands from disk...";
Load();
cout << "done.\n";
}
}
void AllHands::Compute()
{
handRanksByHash.resize(HandHashCount);
for (auto c1 = 0; c1 < DeckSize; ++c1)
for (auto c2 = c1 + 1; c2 < DeckSize; ++c2)
for (auto c3 = c2 + 1; c3 < DeckSize; ++c3)
for (auto c4 = c3 + 1; c4 < DeckSize; ++c4)
for (auto c5 = c4 + 1; c5 < DeckSize; ++c5)
handRanksByHash[Hand::GetHash(c1, c2, c3, c4, c5)] = Hand{ c1, c2, c3, c4, c5 }.GetValue().GetRank();
}
void AllHands::Load()
{
ifstream in{ fileName, ios::binary };
handRanksByHash.resize(HandHashCount);
in.read(reinterpret_cast<char*>(handRanksByHash.data()), HandHashCount * sizeof(int));
}
void AllHands::Save()
{
ofstream out{ fileName, ios::binary };
out.write(reinterpret_cast<const char*>(handRanksByHash.data()), HandHashCount * sizeof(int));
}
int AllHands::GetRankByHash(int hash) const
{
return handRanksByHash[hash];
}
int AllHands::GetBestHandRank(const GamePlayer& player, const Community& community) const
{
return GetBestHandRank(
player.GetHole().GetCard1(),
player.GetHole().GetCard2(),
community[0],
community[1],
community[2],
community[3],
community[4]);
}
int AllHands::GetBestHandRank(int c1, int c2, int m1, int m2, int m3, int m4, int m5) const
{
auto rank = GetRank(m1, m2, m3, m4, m5);
rank = max(rank, GetRank(c1, m1, m2, m3, m4));
rank = max(rank, GetRank(c1, m1, m2, m3, m5));
rank = max(rank, GetRank(c1, m1, m2, m4, m5));
rank = max(rank, GetRank(c1, m1, m3, m4, m5));
rank = max(rank, GetRank(c1, m2, m3, m4, m5));
rank = max(rank, GetRank(c2, m1, m2, m3, m4));
rank = max(rank, GetRank(c2, m1, m2, m3, m5));
rank = max(rank, GetRank(c2, m1, m2, m4, m5));
rank = max(rank, GetRank(c2, m1, m3, m4, m5));
rank = max(rank, GetRank(c2, m2, m3, m4, m5));
rank = max(rank, GetRank(c1, c2, m1, m2, m3));
rank = max(rank, GetRank(c1, c2, m1, m2, m4));
rank = max(rank, GetRank(c1, c2, m1, m2, m5));
rank = max(rank, GetRank(c1, c2, m1, m3, m4));
rank = max(rank, GetRank(c1, c2, m1, m3, m5));
rank = max(rank, GetRank(c1, c2, m1, m4, m5));
rank = max(rank, GetRank(c1, c2, m2, m3, m4));
rank = max(rank, GetRank(c1, c2, m2, m3, m5));
rank = max(rank, GetRank(c1, c2, m2, m4, m5));
rank = max(rank, GetRank(c1, c2, m3, m4, m5));
return rank;
}
bool AllHands::BeatenByOneCard(int bestHand, int c, int m1, int m2, int m3, int m4, int m5) const
{
return GetRank(c, m1, m2, m3, m4) > bestHand ||
GetRank(c, m1, m2, m3, m5) > bestHand ||
GetRank(c, m1, m2, m4, m5) > bestHand ||
GetRank(c, m1, m3, m4, m5) > bestHand ||
GetRank(c, m2, m3, m4, m5) > bestHand;
}
bool AllHands::BeatenByTwoCards(int bestHand, int c1, int c2, int m1, int m2, int m3, int m4, int m5) const
{
return GetRank(c1, c2, m1, m2, m3) > bestHand ||
GetRank(c1, c2, m1, m2, m4) > bestHand ||
GetRank(c1, c2, m1, m2, m5) > bestHand ||
GetRank(c1, c2, m1, m3, m4) > bestHand ||
GetRank(c1, c2, m1, m3, m5) > bestHand ||
GetRank(c1, c2, m1, m4, m5) > bestHand ||
GetRank(c1, c2, m2, m3, m4) > bestHand ||
GetRank(c1, c2, m2, m3, m5) > bestHand ||
GetRank(c1, c2, m2, m4, m5) > bestHand ||
GetRank(c1, c2, m3, m4, m5) > bestHand;
}
void AllHands::DumpHistogram() const
{
map<HandType, int> histogram;
for (auto c1 = 0; c1 < DeckSize; ++c1)
for (auto c2 = c1 + 1; c2 < DeckSize; ++c2)
for (auto c3 = c2 + 1; c3 < DeckSize; ++c3)
for (auto c4 = c3 + 1; c4 < DeckSize; ++c4)
for (auto c5 = c4 + 1; c5 < DeckSize; ++c5)
++histogram[HandValue{ handRanksByHash[Hand::GetHash(c1, c2, c3, c4, c5)] }.GetType()];
for (auto p : histogram)
cout << ToString(p.first) << ": " << p.second << '\n';
}
int AllHands::GetRank(int c1, int c2, int c3, int c4, int c5) const
{
array<int, HandSize> cards{ c1, c2, c3, c4, c5 };
sort(cards.begin(), cards.end());
auto hash = Hand::GetHash(cards[0], cards[1], cards[2], cards[3], cards[4]);
return GetRankByHash(hash);
}