Deuces:一个纯Python扑克牌手牌评估库
项目描述
Deuces
A pure Python poker hand evaluation library
[ 2 ❤ ] , [ 2 ♠ ]
安装
pip install deuces
# OR...
git clone git@github.com:worldveil/deuces.git ./deuces
cd ./deuces
python3.10 -m venv poker_env
source poker_env/bin/activate
pip install -r requirements.txt
运行测试
这些应该更加全面,但目前为止,运行
python -m pytest
实现说明
Deuces最初是为MIT Pokerbots竞赛编写的,轻量级且速度快。所有查找都通过位运算和字典查找完成。也就是说,Deuces不会击败C实现(~250k eval/s),但它适用于Python是必需的或机器人被分配合理的思考时间(人类时间尺度)的情况。
Deuces处理5、6和7张牌的手牌查找。6和7张牌的查找是通过组合评估5张牌的选择来完成的,但后续版本可能会为这些提供专用和更快的算法。
我还为2张牌的展开有查找表,这在评估德克萨斯扑克前手牌权益特别有用,但它们也将很快提供。
请参阅我的博客以了解库的工作原理以及查找表生成方法: http://willdrevo.com/(尚未发布)
使用方法
Deuces易于设置和使用。
from deuces import Card
card = Card.new('Qh')
卡片对象以整数形式表示,以保持Deuces的性能和轻量级。
现在让我们创建底牌和一个示例德克萨斯扑克手牌
board = [
Card.new('Ah'),
Card.new('Kd'),
Card.new('Jc')
]
hand = [
Card.new('Qs'),
Card.new('Th')
]
将卡片整数以美观的方式打印到终端
Card.print_pretty_cards(board + hand)
[ A ❤ ] , [ K ♦ ] , [ J ♣ ] , [ Q ♠ ] , [ T ❤ ]
如果您已安装termcolor
,它们还将被着色。
否则直接评估您的手牌强度
>>> from deuces import Evaluator
>>> evaluator = Evaluator()
>>> print(evaluator.evaluate(board, hand))
1600
手力在1到7462的范围内评估,其中1是皇家同花顺,7462是散牌7-5-4-3-2,因为扑克牌中只有7642种不同排名的手牌。再次提醒,请参考我的博客文章,以了解更多关于为什么是这样数学上的完整解释。
如果您想要从一副牌中随机抽取牌,使用Deuces也可以做到。
from deuces import Deck
deck = Deck()
board = deck.draw(5)
player1_hand = deck.draw(2)
player2_hand = deck.draw(2)
并打印它们。
>>> Card.print_pretty_cards(board)
[ 4 ♣ ] , [ A ♠ ] , [ 5 ♦ ] , [ K ♣ ] , [ 2 ♠ ]
>>> Card.print_pretty_cards(player1_hand)
[ 6 ♣ ] , [ 7 ❤ ]
>>> Card.print_pretty_cards(player2_hand)
[ A ♣ ] , [ 3 ❤ ]
让我们评估两种手牌的强度,然后将它们分门别类,每种手牌类型(高牌、对子等)一类。
p1_score = evaluator.evaluate(board, player1_hand)
p2_score = evaluator.evaluate(board, player2_hand)
p1_class = evaluator.get_rank_class(p1_score)
p2_class = evaluator.get_rank_class(p2_score)
或者获取一个描述分数的人类友好字符串,
>>> print("Player 1 hand rank = %d (%s)\n" % (p1_score, evaluator.class_to_string(p1_class)))
Player 1 hand rank = 6330 (High Card)
>>> print("Player 2 hand rank = %d (%s)\n" % (p2_score, evaluator.class_to_string(p2_class)))
Player 2 hand rank = 1609 (Straight)
或者,最酷的是,获取与手牌强度相关的游戏阶段的详细分析,
>>> hands = [player1_hand, player2_hand]
>>> evaluator.hand_summary(board, hands)
========== FLOP ==========
Player 1 hand = High Card, percentage rank among all hands = 0.893192
Player 2 hand = Pair, percentage rank among all hands = 0.474672
Player 2 hand is currently winning.
========== TURN ==========
Player 1 hand = High Card, percentage rank among all hands = 0.848298
Player 2 hand = Pair, percentage rank among all hands = 0.452292
Player 2 hand is currently winning.
========== RIVER ==========
Player 1 hand = High Card, percentage rank among all hands = 0.848298
Player 2 hand = Straight, percentage rank among all hands = 0.215626
========== HAND OVER ==========
Player 2 is the winner with a Straight
这就是Deuces。
性能
Deuces有多快?查看performance
文件夹,比较Deuces与其他纯Python手牌评估器的几个测试。
以下是评估10,000张随机5、6和7张牌的牌桌的结果
5 card evaluation:
[*] Pokerhand-eval: Evaluations per second = 83.577580
[*] Deuces: Evaluations per second = 235722.458889
[*] SpecialK: Evaluations per second = 376833.177604
6 card evaluation:
[*] Pokerhand-eval: Evaluations per second = 55.519042
[*] Deuces: Evaluations per second = 45677.395466
[*] SpecialK: N/A
7 card evaluation:
[*] Pokerhand-eval: Evaluations per second = 51.529784
[*] Deuces: Evaluations per second = 15220.969303
[*] SpecialK: Evaluations per second = 142698.833384
与pokerhand-eval
相比,Deuces在5张牌评估中快2400倍,在7张牌评估中快300倍。
然而,SpecialKEval
在5张牌评估中处于绝对优势,其令人印象深刻的近40万次/秒(比Deuces快约1.7倍),在7张牌评估中达到140,000次/秒(快10倍)。
如果您想在Python中实现更简洁的用户界面和更易于阅读和适应的代码,我建议使用Deuces,因为如果您真的需要速度,您本就应该使用C。SpecialK在7张牌上的额外10倍提升在蒙特卡洛模拟方面不会带来太多好处,而且SpecialK的5张牌评估与Deuces的评估/s相比仅相差一个因子。
对于C/C++,我推荐pokerstove
,因为它的超优化C++ Boost例程可以做到1000万次/秒以上。
测试
您可以通过以下方式运行测试
python setup.py test
或者,安装所需的软件包
pip install -r requirements.txt
然后
bash-3.2$ pytest deuces
=== test session starts ===
platform darwin -- Python 2.7.13, pytest-3.2.1, py-1.4.34, pluggy-0.4.0
rootdir: /Users/misha/git/deuces, inifile:
plugins: hypothesis-3.19.0, cov-2.5.1
collected 1 item
deuces/test_deuces.py .
=== 1 passed in 0.05 seconds ===
以获得测试覆盖率
pytest deuces --cov-report html:gitignore/coverage --cov=deuces deuces deuces --cov-report html:gitignore/coverage --cov=deuces deuces
许可证
版权(c)2013威尔·德罗
特此授予任何获得此软件及其相关文档文件(“软件”)副本的人无限制地使用软件的权利,包括但不限于使用、复制、修改、合并、发布、分发、再许可和/或销售软件副本的权利,并允许向软件提供的人这样做,前提是以下条件
上述版权声明和本许可声明应包含在软件的所有副本或主要部分中。
软件按“原样”提供,不提供任何形式的保证,明示或暗示,包括但不限于适销性、特定用途的适用性和非侵权性。在任何情况下,作者或版权所有者不对任何索赔、损害或其他责任负责,无论这些索赔、损害或其他责任是由于合同、侵权或其他方式引起的,与软件或其使用或其它有关。
项目详情
下载文件
下载适合您平台的应用程序。如果您不确定选择哪个,请了解有关安装软件包的更多信息。
源代码分发
构建分发
deuces-0.2.1.tar.gz 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 1a22753427923b9fa17f3b9ce16f0d229aacb2e505899f34293b773e28f9ffc3 |
|
MD5 | ff554087bc19523e4b59157a7db06891 |
|
BLAKE2b-256 | 05a6bbf96fb70324d5e323c697403b101bf897288a77ead288eddce839d09182 |
deuces-0.2.1-py3-none-any.whl 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | f84fa528b6c6b0659dc2d5f60168424bd03266eb40bee85fa0233202033e70e1 |
|
MD5 | 2b96e8684330ad8d79fd91f315fce550 |
|
BLAKE2b-256 | 295a18d79a016c7ad1ad44802bfa1c2b6f959ee687d4bd236177b49553b8425f |