# C/C++ Library
## Compile and test
This library can be built using CMake. A recommended way of building it is:
```
mkdir -p build
cd build
cmake ..
make
```
This will generate a static-linked library `libpheval.a`, as well as
a unit test binary `unit_tests`.
Run the `unit_tests` to perform the unit tests:
```
./unit_tests
```
## Use the library
After building the library `libpheval.a`, you can add the `./include`
directory to your includes path, and link the library to your source
code. In addition, at least C++11 standard is required.
For example:
```bash
g++ -I include/ -std=c++11 libpheval.a your_source_code.cc -o your_binary
```
## Examples
In this example we use a scenario in the game Texas Holdem:
Community cards: 9c 4c 4s 9d 4h (both players share these cards)
Player 1: Qc 6c
Player 2: 2c 9h
Both players have full houses. But player 1 only has a four full house over
nine, while player 2 has nine full house over four, which is stronger than
player 1.
The following two sections will show us how this example hands are evaluated
using PHEvaluator. All the code and compiling can be found in the
[examples](examples) directory.
### Example Code in C++
We can construct a Card by providing a two character string, like this:
```C++
phevaluator::Card a = phevaluator::Card("Qc");
```
Then the method `EvaluateCards` will take parameters from 5 to 7 Cards and
return a Rank type. Sometimes, you can provide the strings directly to the
method and let the Cards be constructed implicitly, to make your code
cleaner.
```C++
phevaluator::Rank rank1 =
phevaluator::EvaluateCards("9c", "4c", "4s", "9d", "4h", "Qc", "6c");
phevaluator::Rank rank2 =
phevaluator::EvaluateCards("9c", "4c", "4s", "9d", "4h", "2c", "9c");
```
Now that we have the Ranks from both players' hands. What information can
we get from them?
First, we can do comparison on these Ranks. A stronger hand is greater than
a weaker hand.
```C++
assert(rank1 < rank2); // rank2 is stronger
```
We can also get its rank in all 7562 possible hands. This ranking is
identical to the return value of the Cactus Kev's evaluator:
the best rank has value 1, which is a Royal Straight Flush, while the
worst rank is 7462.
```C++
int value1 = rank1.value(); // 292
int value2 = rank2.value(); // 236
```
We can also tell the ranking category of the rank: either using the method
`category` which returns an enumerator, or the method `describeCategory`
which returns a string. In this example, both players are holding full houses.
```C++
assert(rank1.category() == FULL_HOUSE);
assert(rank1.describeCategory() == "Full House");
assert(rank2.category() == FULL_HOUSE);
assert(rank2.describeCategory() == "Full House");
```
Apart from the ranking category, we can get more detail from the rank, using
the `describeRank` or the `describeSampleHand` method.
Let's see a sample hand from the `rank2`:
```C++
assert(rank2.describeSampleHand() == "9 9 9 4 4");
```
As we can see, the best 5-card hand from player 2 is 9-9-9-4-4. Suit
information is missing from this method, because usually they don't matter,
unless all the five cards have the same suit. So we have another method that
tells us whether the sample hand is a flush. In this example, it is not.
```C++
assert(!rank2.isFlush());
```
Finally, we have another method to give us a short description of the sample
hand:
```C++
assert(rank2.describeRank() == "Nines Full over Fours");
```
### Example Code in C
In the C version, the evaluation would be tricker, since we have to convert
the card to an integer by ourselves.
The formula is `rank * 4 + suit`.
We use 0 for the clubs, 1 for the diamonds, 2 for the hearts, and 3 for the
spades.
And the rank values are:
deuce = 0, trey = 1, four = 2, five = 3, six = 4, seven = 5, eight = 6,
nine = 7, ten = 8, jack = 9, queen = 10, king = 11, ace = 12.
The mapping of a Card and its integer value can be found in [Card Id](#cardid).
```C
// Community cards
int a = 7 * 4 + 0; // 9c
int b = 2 * 4 + 0; // 4c
int c = 2 * 4 + 3; // 4s
int d = 7 * 4 + 1; // 9d
int e = 2 * 4 + 2; // 4h
// Player 1
int f = 10 * 4 + 0; // Qc
int g = 4 * 4 + 0; // 6c
// Player 2
int h = 0 * 4 + 0; // 2c
int i = 7 * 4 + 2; // 9h
```
After constructing all the Cards, we will use `evaluate_7cards` to get a
rank value. A rank value indicates its rank in the 7462 ranking table. The
best rank is 1, which is a Royal Straight Flush, and the worst rank is 7462.
```C
int rank1 = evaluate_7cards(a, b, c, d, e, f, g); // 292
int rank2 = evaluate_7cards(a, b, c, d, e, h, i); // 236
```
If we compare these two rank value, the rank with a smaller value is
stronger.
```C
assert(rank1 > rank2); // rank2 is stronger
```
Similar to the C++ interfaces, we can see the rank category:
```C
enum rank_category category = get_rank_category(rank1);
assert(category == FULL_HOUSE);
const char * categoryDesc = describe_rank_category(category); // "Full House"
```
Or get the sample hand from the rank:
```C
describe_sample_hand(rank2); // 9 9 9 4 4
```
We can see the 5 best cards from player 2 are 9 9 9 4 4.
Although suit information is missing here, indeed we only care about whether
all five cards are in the same suit or not. We can check it using the
`is_flush` method:
```C
is_flush(rank2); // false
```
Finally, we can use `rank_description` to get a short description of the rank:
```C
describe_rank(rank2); // Nines Full over Fours
```
<a name="cardid"></a>
## Card Id
We can use an integer to represent a card. The two least significant bits
represent the 4 suits, ranged from 0-3. The rest of it represent the 13
ranks, ranged from 0-12.
More specifically, the ranks are:
deuce = 0, trey = 1, four = 2, five = 3, six = 4, seven = 5, eight = 6,
nine = 7, ten = 8, jack = 9, queen = 10, king = 11, ace = 12.
And the suits are:
club = 0, diamond = 1, heart = 2, spade = 3
So that you can use `rank * 4 + suit` to get the card ID.
The complete card Id mapping can be found below. The rows are the ranks
from 2 to Ace, and the columns are the suits: club, diamond, heart and spade.
| | C | D | H | S |
|---|---|---|---|---|
| 2 | 0 | 1 | 2 | 3 |
| 3 | 4 | 5 | 6 | 7 |
| 4 | 8 | 9 | 10| 11|
| 5 | 12| 13| 14| 15|
| 6 | 16| 17| 18| 19|
| 7 | 20| 21| 22| 23|
| 8 | 24| 25| 26| 27|
| 9 | 28| 29| 30| 31|
| T | 32| 33| 34| 35|
| J | 36| 37| 38| 39|
| Q | 40| 41| 42| 43|
| K | 44| 45| 46| 47|
| A | 48| 49| 50| 51|
没有合适的资源?快使用搜索试试~ 我知道了~
PokerHandEvaluator:扑克手评估师
共90个文件
py:22个
c:20个
h:9个
需积分: 10 1 下载量 63 浏览量
2021-05-12
16:14:53
上传
评论
收藏 28.44MB ZIP 举报
温馨提示
PH评估器 基于Pefect Hash算法的扑克手牌评估器 概述 有效评估扑克手一直是一个有趣但具有挑战性的问题。 给定两只不同的扑克手,如何确定哪一只更强? 或更笼统地说,给定一只手牌,我们可以给它分配一个分数来表明其实力吗? 仙人掌凯夫(Cactus Kev)曾经给出过五张牌手牌评价。 通过智能编码,它可以将每只手排名为7462个不同的值。 尽管如此,Kev的解决方案还是针对五张牌。 要使用Kev算法评估七张牌的扑克手(由于得克萨斯州扑克而变得更加流行),一种蛮力解决方案是迭代所有7选择5的组合,运行他的五张牌评估算法21次以找到最佳答案,这显然太浪费时间了。 奥马哈扑克会更加复杂,因为它需要从四张玩家纸牌中挑选两张纸牌,并从五张社区纸牌中挑选三张纸牌。 使用蛮力,将需要Kev的5张卡片评估算法进行60次迭代(5选择3乘以4选择2)。 设计用于评估5张以上的扑克牌。 它没有遍历
资源详情
资源评论
资源推荐
收起资源包目录
PokerHandEvaluator-master.zip (90个子文件)
PokerHandEvaluator-master
.travis.yml 687B
test
five
id_input_tests.csv 47.18MB
string_input_tests.csv 49.56MB
six
id_input_tests.csv 2.08MB
string_input_tests.csv 2.19MB
seven
id_input_tests.csv 2.35MB
string_input_tests.csv 2.48MB
README.md 860B
cpp
benchmark
benchmark.cc 4KB
CMakeLists.txt.in 794B
test
rank.cc 2KB
evaluation.cc 4KB
kev
fast_eval.c 58KB
kev_eval.c 2KB
index52c7.h 499KB
arrays.h 124KB
kev_eval.h 543B
evaluation_omaha.cc 3KB
include
phevaluator
card.h 2KB
rank.h 3KB
hand.h 1KB
phevaluator.h 3KB
src
hand.cc 2KB
hashtable6.c 125KB
evaluator_omaha.c 4KB
hash.h 785B
tables.h 1KB
hashtable5.c 43KB
hash.c 884B
dptables.c 28KB
evaluator5.c 2KB
hashtable7.c 326KB
evaluator6.c 3KB
evaluator9.c 3KB
rank.c 2KB
tables_omaha.c 89.18MB
7462.c 247KB
evaluator8.c 3KB
hashtable9.c 1.64MB
hashtable8.c 769KB
evaluator7.c 3KB
evaluator_omaha.cc 947B
evaluator.cc 2KB
hashtable.c 46KB
examples
omaha_example.cc 1KB
c_example.c 3KB
cpp_example.cc 1KB
.gitignore 36B
Makefile 676B
.gitignore 180B
CMakeLists.txt 3KB
Makefile 842B
README.md 6KB
python
phevaluator
benchmark.py 1KB
tests
test_evaluator.py 2KB
cardfiles
5cards.json 25KB
7cards.json 31KB
6cards.json 28KB
examples.py 2KB
evaluator
hash.py 505B
evaluator6.py 1KB
hashtable_omaha.py 238B
card.py 523B
dptables.py 21KB
evaluator5.py 1KB
hashtable5.py 37KB
evaluator.py 919B
hashtable.py 39KB
omaha_noflush.dat 21.44MB
evaluator_omaha.py 3KB
hashtable7.py 283KB
hashtable6.py 109KB
evaluator7.py 2KB
omaha_flush.dat 7.82MB
table_tests
test_hashtable6.py 398B
test_dptables.py 3KB
utils.py 4KB
test_hashtable5.py 5KB
test_hashtable7.py 398B
test_hashtable.py 3KB
requirements.txt 6B
.gitignore 2KB
README.md 990B
LICENSE 11KB
CONTRIBUTING.md 505B
.gitignore 264B
Documentation
Algorithm.md 14KB
README.md 122B
README.md 4KB
.editorconfig 81B
共 90 条
- 1
陈崇礼
- 粉丝: 47
- 资源: 4683
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0