def __init__(self, protocol, avatar, table_info): self.protocol = protocol self.id = table_info.get('id', 0) self.seat = table_info.get('player_seated', -1) self.seats = [0] * table_info.get('seats', 10) if self.seat != -1: self.seats[self.seat] = avatar.serial assert avatar.seat == self.seat, "as %s, ss %s" % (avatar.seat, self.seat) self.name = table_info.get('name', 'unnamed') self.betting_structure = table_info['betting_structure'] blinds, buy_ins, limit = table_info['betting_structure'].split('_') min_buy_in, max_buy_in = buy_ins.split('-') small, big = blinds.split('-') self.max_buy_in = int(max_buy_in) * 100 self.min_buy_in = int(min_buy_in) * 100 self.big_blind = int(big) * 100 self.small_blind = int(small) * 100 self.players = {avatar.serial: avatar} self.avatar = avatar self._serial_and_game_id = dict(serial=avatar.serial, game_id=self.id) self._eval = PokerEval() self.reset() self._game_state = GAME_STATE_NULL
def getValue(self, card): value = None if type(card) is str: poker_eval = PokerEval() try: value = poker_eval.string2card(card) except RuntimeError: raise UserWarning, "Invalid card %s" % (card) else: if card != PokerCards.NOCARD: value = card & PokerCards.VALUE_CARD_MASK if (value < 0) or (value >= PokerCards.NB_CARD): raise UserWarning, "Invalid card %s" % (card) value = card return value
def __init__(self): self.hand_groups = { "A":["AA", "AKs", "KK"], "B":["AK", "QQ"], "C":["JJ", "TT"], "D":["AQs", "AQ", "AJs", "99", "88"], "E":["AJ","ATs","KQs", "77","66","55"], "F":["AT","KQ","KJs","QJs","44","33","22"], "G":["A9s","A8s","A7s","A6s","A5s","A4s","A3s","A2s","KTs","QTs","JTs","J9s","T9s","98s"], "H":["KJ","KT","QJ","J8s","T8s","87s","76s"] } self.hand_values = {"A":13,"K":12,"Q":11,"J":10, "T":9,"9":8,"8":7,"7":6,"6":5,"5":4,"4":3,"3":2,"2":1} self.odds_map = { "flop":{1:0.045,2:0.088,3:0.13,4:0.172,5:0.212,6:0.252,7:0.29,8:0.327,9:0.364,10:0.399,11:0.433,12:0.467,13:0.499,14:0.53,15:0.561,16:0.59,17:0.618}, "turn":{1:0.023,2:0.045,3:0.68,4:0.091,5:0.114,6:0.136,7:0.159,8:0.182,9:0.205,10:0.227,11:0.25,12:0.273,13:0.295,14:0.318,15:0.341,16:0.364,17:0.386} } self.eval = PokerEval()
def __init__(self, had_values, hand, board): self.eval = PokerEval() self.hand_values = { "A": 13, "K": 12, "Q": 11, "J": 10, "T": 9, "9": 8, "8": 7, "7": 6, "6": 5, "5": 4, "4": 3, "3": 2, "2": 1 } self.hand = hand self.board = board
def calculate_hand_strength(current_hole_cards, board_cards, full_deck): """ Implemented as described in the page 21 of the thesis in: http://poker.cs.ualberta.ca/publications/davidson.msc.pdf """ our_cards = current_hole_cards + board_cards our_cards_set = frozenset(our_cards) if len(our_cards_set) == 2: return STRENGTH_TABLE_FOR_2_CARDS[our_cards_set] else: pokereval = PokerEval() ahead = 0.0 tied = 0.0 behind = 0.0 our_rank = pokereval.evaln(our_cards) # considers all two card combinations of the remaining cards deck = list(full_deck) for card in our_cards: if card not in deck: print "Warning! Card not in deck: " + str( card) + ", current_hole_cards: " + str( current_hole_cards) + ", board_cards: " + str( board_cards) deck.remove(card) opponent_cards_combinations = itertools.combinations(deck, 2) for opponent_card1, opponent_card2 in opponent_cards_combinations: opponent_rank = pokereval.evaln([opponent_card1] + [opponent_card2] + board_cards) if our_rank > opponent_rank: ahead += 1.0 elif our_rank == opponent_rank: tied += 1.0 else: behind += 1.0 hand_strength = (ahead + tied / 2.0) / (ahead + tied + behind) return hand_strength
def GET(self): input = web.input() web.header("Content-Type", "text/plain") defaultParams = { "game": "holdem", "pockets": [], "board": [], "dead": [], "iterations": 10000 } kwargs = defaultParams jsonp = None for key in input: value = input[key] if key == "jsoncallback": jsonp = value elif key in ["game", "pockets", "board", "dead", "iterations"]: kwargs[key] = json.read(value) cards = [] for pocket in kwargs["pockets"]: cards.extend(pocket) cards.extend(kwargs["board"]) cards.extend(kwargs["dead"]) duplicate = set( filter(lambda card: (card != "__" and cards.count(card) > 1), cards)) if len(duplicate) > 0: poker_eval_result = { "error": "duplicate cards: %s" % string.join(duplicate) } else: params = apply_game_params(**kwargs) try: poker_eval_result = PokerEval().poker_eval(**params) except Exception, e: poker_eval_result = {"error": str(e)}
# Loic Dachary <*****@*****.**> # Henry Precheur <*****@*****.**> (2004) # import unittest, sys from os import path TESTS_PATH = path.dirname(path.realpath(__file__)) sys.path.insert(0, path.join(TESTS_PATH, "..")) from pokereval import PokerEval from pokerengine import pokergame from pokerengine.pokergame import PokerGameServer from pokerengine.pokercards import PokerCards poker_eval = PokerEval() _initial_money = 1000 class TestAllIn(unittest.TestCase): def setUp(self, variant, betting): self.game = PokerGameServer("poker.%s.xml", [path.join(TESTS_PATH, '../conf')]) self.game.setVariant(variant) self.game.setBettingStructure(betting) def bestWithStrings(self, side, serial): (value, cards) = self.game.bestHand(side, serial) return (cards[0], self.game.eval.card2string(cards[1:])) def tearDown(self): del self.game
class HandEvaluator: evaluator = PokerEval() preflop_win_percentages_suited = [ [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [ 0, 0, 0, 0.3598, 0.3682, 0.3784, 0.3766, 0.3814, 0.4026, 0.4241, 0.4484, 0.4738, 0.5017, 0.532, 0.5737 ], [ 0, 0, 0.3598, 0, 0.3863, 0.3968, 0.3953, 0.4003, 0.4087, 0.4325, 0.4568, 0.4823, 0.5101, 0.5405, 0.5821 ], [ 0, 0, 0.3682, 0.3863, 0, 0.4145, 0.4133, 0.4184, 0.427, 0.4385, 0.4653, 0.4906, 0.5185, 0.5488, 0.5903 ], [ 0, 0, 0.3784, 0.3968, 0.4145, 0, 0.4313, 0.4367, 0.4454, 0.4572, 0.4721, 0.4999, 0.5276, 0.5579, 0.5992 ], [ 0, 0, 0.3766, 0.3953, 0.4133, 0.4313, 0, 0.4536, 0.4623, 0.4742, 0.4894, 0.506, 0.536, 0.5664, 0.599 ], [ 0, 0, 0.3814, 0.4003, 0.4184, 0.4367, 0.4536, 0, 0.4793, 0.4912, 0.5064, 0.5232, 0.543, 0.5753, 0.6098 ], [ 0, 0, 0.4026, 0.4087, 0.427, 0.4454, 0.4623, 0.4793, 0, 0.5079, 0.5233, 0.5401, 0.5601, 0.5831, 0.6194 ], [ 0, 0, 0.4241, 0.4325, 0.4385, 0.4572, 0.4742, 0.4912, 0.5079, 0, 0.5402, 0.5566, 0.5766, 0.5998, 0.6277 ], [ 0, 0, 0.4484, 0.4568, 0.4653, 0.4721, 0.4894, 0.5064, 0.5233, 0.5402, 0, 0.5752, 0.5947, 0.6178, 0.6459 ], [ 0, 0, 0.4738, 0.4823, 0.4906, 0.4999, 0.506, 0.5232, 0.5401, 0.5566, 0.5752, 0, 0.6026, 0.6256, 0.6539 ], [ 0, 0, 0.5017, 0.5101, 0.5185, 0.5276, 0.536, 0.543, 0.5601, 0.5766, 0.5947, 0.6026, 0, 0.6339, 0.6621 ], [ 0, 0, 0.532, 0.5405, 0.5488, 0.5579, 0.5664, 0.5753, 0.5831, 0.5998, 0.6178, 0.6256, 0.6339, 0, 0.6704 ], [ 0, 0, 0.5737, 0.5821, 0.5903, 0.5992, 0.599, 0.6098, 0.6194, 0.6277, 0.6459, 0.6539, 0.6621, 0.6704, 0 ] ] preflop_win_percentages_unsuited = [ [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [ 0, 0, 0.5033, 0.3229, 0.3319, 0.3428, 0.3406, 0.3458, 0.3682, 0.3909, 0.4165, 0.4434, 0.4729, 0.5051, 0.5492 ], [ 0, 0, 0.3229, 0.5368, 0.3514, 0.3625, 0.3607, 0.3659, 0.3747, 0.4001, 0.4259, 0.4527, 0.4821, 0.5142, 0.5584 ], [ 0, 0, 0.3319, 0.3514, 0.5702, 0.3815, 0.3801, 0.3854, 0.3944, 0.4067, 0.4349, 0.4618, 0.4912, 0.5232, 0.5672 ], [ 0, 0, 0.3428, 0.3625, 0.3815, 0.6032, 0.3994, 0.4051, 0.4143, 0.4266, 0.4424, 0.4717, 0.5011, 0.5331, 0.5769 ], [ 0, 0, 0.3406, 0.3607, 0.3801, 0.3994, 0.6328, 0.4232, 0.4323, 0.4449, 0.4608, 0.4784, 0.5102, 0.5421, 0.5768 ], [ 0, 0, 0.3458, 0.3659, 0.3854, 0.4051, 0.4232, 0.6623, 0.4505, 0.463, 0.479, 0.4968, 0.5176, 0.5518, 0.5883 ], [ 0, 0, 0.3682, 0.3747, 0.3944, 0.4143, 0.4323, 0.4505, 0.6915, 0.4809, 0.4971, 0.5149, 0.536, 0.5602, 0.5986 ], [ 0, 0, 0.3909, 0.4001, 0.4067, 0.4266, 0.4449, 0.463, 0.4809, 0.7205, 0.5153, 0.5324, 0.5536, 0.578, 0.6076 ], [ 0, 0, 0.4165, 0.4259, 0.4349, 0.4424, 0.4608, 0.479, 0.4971, 0.5153, 0.7501, 0.5524, 0.5728, 0.5973, 0.6271 ], [ 0, 0, 0.4434, 0.4527, 0.4618, 0.4717, 0.4784, 0.4968, 0.5149, 0.5324, 0.5524, 0.7747, 0.5813, 0.6057, 0.6355 ], [ 0, 0, 0.4729, 0.4821, 0.4912, 0.5011, 0.5102, 0.5176, 0.536, 0.5536, 0.5728, 0.5813, 0.7992, 0.6145, 0.6442 ], [ 0, 0, 0.5051, 0.5142, 0.5232, 0.5331, 0.5421, 0.5518, 0.5602, 0.578, 0.5973, 0.6057, 0.6145, 0.8239, 0.6531 ], [ 0, 0, 0.5492, 0.5584, 0.5672, 0.5769, 0.5768, 0.5883, 0.5986, 0.6076, 0.6271, 0.6355, 0.6442, 0.6531, 0.852 ] ] def card_to_str(card): """ Convert this card to a string or number for pypoker-eval. Note that I don't check whether you passed a Card or the right string format! """ # NOT str! Could be unicode if isinstance(card, basestring): return card return card.__str__() def str_to_card(card_string): """ Convert this string to a pokerbots.engine.game.Card instance. Note that I don't check whether or not you passed the right format! """ if isinstance(card_string, Card): return card rank_str = card_string[0].lower() suit_str = card_string[1].lower() rank = 2 suit = 1 if rank_str == "t": rank = 10 elif rank_str == "j": rank = 11 elif rank_str == "q": rank = 12 elif rank_str == "k": rank = 13 elif rank_str == "a": rank = 14 if suit_str == "s": suit = 1 elif suit_str == "h": suit = 2 elif suit_str == "d": suit = 3 elif suit_str == "c": suit = 4 return Card(rank, suit) def evaluate_hand(hand, board=[], iterations=1000): """ Return winning percentage of your hand, with ties counted as 0.5 Includes Monte-Carlo simulation of running the board. Includes trying all possible opponent hands. Arguments: hand: your hand board: the board if any iterations: number of times to simulate """ # If the board is determined, there's only 990 hands to run, # so run them all if len(board) == 5: # convert to pypoker-eval format hand = map(HandEvaluator.card_to_str, hand) board = map(HandEvaluator.card_to_str, board) poker_eval_result = HandEvaluator.evaluator.poker_eval( game="holdem", pockets=[hand, [255, 255]], dead=[], board=board) elif len(board) == 0: # Use a lookup table, because this has been done before if hand[0].suit == hand[1].suit: return HandEvaluator.preflop_win_percentages_suited[ hand[0].rank][hand[1].rank] else: return HandEvaluator.preflop_win_percentages_unsuited[ hand[0].rank][hand[1].rank] else: hand = map(HandEvaluator.card_to_str, hand) board = map(HandEvaluator.card_to_str, board) # Fill the rest of the board with 255s (unknown card) for i in xrange(5 - len(board)): board.append(255) poker_eval_result = HandEvaluator.evaluator.poker_eval( game="holdem", pockets=[hand, [255, 255]], dead=[], board=board, iterations=iterations) # Ok, we have stats. Calculate win pct, with ties as 0.5 weight return (poker_eval_result['eval'][0]['winhi'] + \ 0.5 * poker_eval_result['eval'][0]['tiehi']) / \ float(poker_eval_result['info'][0]) card_to_str = staticmethod(card_to_str) str_to_card = staticmethod(str_to_card) evaluate_hand = staticmethod(evaluate_hand)
# # Authors: # Loic Dachary <*****@*****.**> # # import sys sys.path.insert(0, ".") sys.path.insert(0, ".libs") from pokereval import PokerEval iterations_low = 100000 iterations_high = 200000 pokereval = PokerEval() if pokereval.best_hand_value("hi", ["Ah", "Ad", "As", "Kh", "Ks"]) != 101494784: sys.exit(1) if pokereval.string2card("2h") != 0: sys.exit(1) print "" pockets = [["As", "Ad", "Ac", "Tc", "Ts", "2d", "5c"], ["Js", "Jc", "7s", "8c", "8d", "3c", "3h"], [255, 255]] print "stud7 (1) result = %s\n" % pokereval.winners( game="7stud", pockets=pockets, dead=[], board=[]) pockets = [[22, 18, 21, 3, 41, 1, 30], [39, 255, 255, 15, 13, 17, 255]]
from flask import Flask, request, jsonify from pokereval import PokerEval GAME = 'holdem' ITERATIONS = 20000 app = Flask(__name__) pe = PokerEval() @app.route('/', methods=['POST', 'GET']) def index(): app.logger.info('index route') if request.method == 'POST': data = request.get_json() app.logger.info('request {}'.format(data)) equities = pe.poker_eval(iterations=ITERATIONS, game=GAME, **data) app.logger.info('equities {}'.format(equities)) return jsonify(equities) return 'https://github.com/minmax/pypoker-eval' app.run(host='0.0.0.0', port=5000, debug=True)
#!/usr/bin/python from pokereval import PokerEval p = PokerEval() contador = 0 with open('./p054_poker.txt') as archivo: for juego in archivo: cartas = juego.rstrip().lower().split(' ') mano_a, mano_b = cartas[:5], cartas[5:] if p.evaln(mano_a) > p.evaln(mano_b): contador += 1 print contador
def calculate_hand_potential_without_heuristics(current_hole_cards, board_cards, round_id, full_deck): """ Implemented as described in the page 23 of the thesis in: http://poker.cs.ualberta.ca/publications/davidson.msc.pdf """ our_cards = current_hole_cards + board_cards out_cards_set = frozenset(our_cards) # hand potential array, each index represents ahead, tied, and behind ahead = 0 tied = 1 behind = 2 hp = [[0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [0.0, 0.0, 0.0]] hp_total = [0.0, 0.0, 0.0] total = 0.0 pokereval = PokerEval() our_rank = pokereval.evaln(our_cards) # considers all two card combinations of the remaining cards for the opponent opponent_cards_combinations = list(itertools.combinations( full_deck, 2)) indices = [] for index, cards in enumerate(opponent_cards_combinations): card1, card2 = cards if card1 in our_cards or card2 in our_cards: indices.append(index) for index in reversed(indices): opponent_cards_combinations.pop(index) for opponent_card1, opponent_card2 in opponent_cards_combinations: opponent_rank = pokereval.evaln([opponent_card1] + [opponent_card2] + board_cards) if our_rank > opponent_rank: index = ahead elif our_rank == opponent_rank: index = tied else: index = behind # hp_total[index] += 1.0 # original version # all possible board cards to come deck = list(full_deck) dealt_card = current_hole_cards + board_cards + [ opponent_card1 ] + [opponent_card2] deck_without_dealt_cards = [ card for card in deck if card not in dealt_card ] if round_id == 1: # flop cards_combinations = list( itertools.combinations(deck_without_dealt_cards, 2)) # cards_combinations = random.sample(cards_combinations, int(len(cards_combinations)*0.2)) for turn, river in cards_combinations: # final 5-card board board = board_cards + [turn] + [river] our_future_rank = pokereval.evaln(current_hole_cards + board) opponent_future_rank = pokereval.evaln([opponent_card1] + [opponent_card2] + board) if our_future_rank > opponent_future_rank: hp[index][ahead] += 1.0 elif our_future_rank == opponent_future_rank: hp[index][tied] += 1.0 else: hp[index][behind] += 1.0 total += 1.0 hp_total[index] += 1.0 # new version else: # turn # cards = random.sample(deck_without_dealt_cards, int(len(deck_without_dealt_cards)*0.75)) cards = deck_without_dealt_cards for river in cards: # final 5-card board board = board_cards + [river] our_future_rank = pokereval.evaln(current_hole_cards + board) opponent_future_rank = pokereval.evaln([opponent_card1] + [opponent_card2] + board) if our_future_rank > opponent_future_rank: hp[index][ahead] += 1.0 elif our_future_rank == opponent_future_rank: hp[index][tied] += 1.0 else: hp[index][behind] += 1.0 total += 1.0 hp_total[index] += 1.0 # new version # the original formula: # ppot = (hp[behind][ahead] + hp[behind][tied]/2.0 + hp[tied][ahead]/2.0) / (hp_total[behind] + hp_total[tied]/2.0) # npot = (hp[ahead][behind] + hp[tied][behind]/2.0 + hp[ahead][tied]/2.0) / (hp_total[ahead] + hp_total[tied]/2.0) # ppot: were behind but moved ahead: cant use the original hp_total, because the result isnt normalzied and because it dont work for the heuristics # added hp[ahead][ahead] so already good hands wouldnt be given a below average potential ppot = (hp[ahead][ahead] + hp[behind][ahead] + hp[behind][tied] / 2.0 + hp[tied][ahead]) / (hp_total[behind] * 1.5 + hp_total[tied] * 1.0 + hp_total[ahead] * 1.0) # npot: were ahead but fell behind # npot = ((hp[ahead][behind]/total)*2.0 + (hp[ahead][tied]/total)*1.0 + (hp[tied][behind]/total)*1.0)/4.0 return ppot