一個純Python的撲克牌手牌評估庫
[ 3 ❤ ] , [ 3 ♠ ]
$ pip install treys
Treys 是 Deuces 的 Python 3 移植版。大部分工作都取自 msaindon 的 fork。
Treys(最初是為 Deuces)是 Will Drevo 為麻省理工學院撲克機器人大賽編寫的。它重量輕且速度快。所有查找都是透過位元算術和字典查找完成的。也就是說,Treys 不會擊敗 C 實現(~250k eval/s),但它對於需要 Python 或機器人分配合理思考時間(人類時間尺度)的情況很有用。
Treys 處理 5、6 和 7 張牌的查找。 6 和 7 卡查找是透過組合評估 5 張卡片選擇來完成的。
Treys 易於設定和使用。
>> > from treys import Card
>> > card = Card . new ( 'Qh' )
Card 物件被表示為整數,以保持 Trey 的高性能和輕量級。
現在讓我們來建立棋盤和德州撲克手牌範例:
>> > 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 ❤ ]
如果您安裝了termacolor
,它們也會被著色。
否則直接評估你的牌力:
>> > from treys import Evaluator
>> > evaluator = Evaluator ()
>> > print ( evaluator . evaluate ( board , hand ))
1600
手牌強度的評估範圍為 1 到 7462,其中 1 是皇家同花順,7462 是非同花 7-5-4-3-2,因為撲克中只有 7642 手明顯排名的手牌。再次,請參閱我的部落格文章,以從數學角度更完整地解釋為什麼會出現這種情況。
如果你想從一副牌中隨機發牌,你也可以使用 Treys 來做到這一點:
>> > from treys 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
版權所有 (c) 2013 威爾德雷沃
特此免費授予任何獲得本軟體和相關文件文件(「軟體」)副本的人不受限制地使用本軟體,包括但不限於使用、複製、修改、合併的權利、發布、分發、再授權和/或銷售軟體的副本,並允許向其提供軟體的人員這樣做,但須滿足以下條件:
上述版權聲明和本授權聲明應包含在本軟體的所有副本或主要部分中。
本軟體以「現況」提供,不提供任何明示或暗示的保證,包括但不限於適銷性、特定用途的適用性和不侵權的保證。 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE軟體.