def test_hand_vs_range_monte_carlo(self): hand = map(eval7.Card, ("As", "Ad")) villain = eval7.HandRange("AA, A3o, 32s") board = [] equity = eval7.py_hand_vs_range_monte_carlo(hand, villain, board, 10000000) self.assertAlmostEqual(equity, 0.85337, delta=0.002)
def get_v_range(fraction): ind = get_fraction_index(fraction) #check if we haven't make the vrange obect yet if vranges[ind] == -1: vranges[ind] = eval7.HandRange(vrange_strings[ind]) return vranges[ind]
def test_all_hands_vs_range(self): hero = eval7.HandRange("AsAd, 3h2c") villain = eval7.HandRange("AA, A3o, 32s") board = [] equity_map = eval7.py_all_hands_vs_range(hero, villain, board, 10000000) self.assertEqual(len(equity_map), 2) hand1 = tuple(map(eval7.Card, ("As", "Ad"))) hand2 = tuple(map(eval7.Card, ("3h", "2c"))) self.assertAlmostEqual(equity_map[hand1], 0.85337, delta=0.002) self.assertAlmostEqual(equity_map[hand2], 0.22865, delta=0.002) # Hero has an impossible hand in his range. hero = eval7.HandRange("JsJc,QsJs") villain = eval7.HandRange("JJ") board = tuple(map(eval7.Card, ("Kh", "Jd", "8c"))) equity_map = eval7.py_all_hands_vs_range(hero, villain, board, 10000000) hand = tuple(map(eval7.Card, ("Qs", "Js"))) self.assertAlmostEqual(equity_map[hand], 0.03687, delta=0.0002) self.assertEqual(len(equity_map), 1)
def test_hand_vs_range_exact(self): cases = ( (("Ac", "Ah"), "AA", (""), 0.5), (("Ac", "Ah"), "AsAd", (""), 0.5), (("As", "Ad"), "AA, A3o, 32s", (""), 0.95), ) for hand_strs, range_str, board_strs, expected_equity in cases: hand = tuple(map(eval7.Card, hand_strs)) villain = eval7.HandRange(range_str) board = tuple(map(eval7.Card, board_strs)) equity = eval7.py_hand_vs_range_exact(hand, villain, board) self.assertTrue(True)
def __init__(self): ''' Called when a new game starts. Called exactly once. Arguments: Nothing. Returns: Nothing. ''' # store preflop hand rankings and hands (sorted), # updating as new evidence comes in self.hand_values = dict((hand, 0) for hand in HANDS) self.sorted_hands = [] self.value_ranks = value_ranking(order_ensemble) self.trans_hands = translate_hands(self.value_ranks, HANDS) self.evidence_updated = True self.random_hand = eval7.HandRange(','.join([ ALL_RANKS[i] + ALL_RANKS[j] for i in range(len(ALL_RANKS)) for j in range(i, len(ALL_RANKS)) ]))
from operator import add pokereval = PokerEval() ''' full flop: 52*51*50/6 = 22,100 rainbow : 2197(given three color) * 4 (C1,4) = 8788 39% TwoColor: 2*(13*12/2)*13* 6 (C2,4) = 12168 55% OneColor: 13*12*11/6 * 4 (C1,4) = 1144 5.2% ''' # hr1 3-bet for value # hr2 3-bet for lesser value # hr3 for call hr1 = eval7.HandRange("TT+, AQ+, KQ+") # 78 combo hr2 = eval7.HandRange("77+, A9+, KT+, QT+, JT, T9s, 98s, 87s, 76s, 65s") hr3 = eval7.HandRange( "22+, A7o+, KT+, QT+, JT, T9, 98s, 87s, 76s, 65s, A2s+, K9s, K8s, Q9s, Q8s, \ 64s, 75s, 86s, 97s, T8s, J9s") rank1 = [ "2h", "3h", "4h", "5h", "6h", "7h", "8h", "9h", "Th", "Jh", "Qh", "Kh", "Ah" ] rank2 = [ "2d", "3d", "4d", "5d", "6d", "7d", "8d", "9d", "Td", "Jd", "Qd", "Kd", "Ad" ] rank3 = [ "2c", "3c", "4c", "5c", "6c", "7c", "8c", "9c", "Tc", "Jc", "Qc", "Kc",
def river_action_wrapped(cards, board_cards, opponents, bet, pot, raised=False, first_bet=False, pot_type=2): hand = [eval7.Card(s) for s in (cards[:2], cards[2:])] board = [ eval7.Card(s) for s in [board_cards[i:i + 2] for i in range(0, len(board_cards), 2)] ] current_hand = eval7.hand_type(eval7.evaluate(hand + board)) # if you have nuts, raise or jam RFI = "22+, 32s, 53s+, 63s+, 74s+, 84s+, 95s+, T6s+, J6s+, " \ "Q4s+, K2s+, A2s+, A2o+, K8o+, Q8o+, J8o+, T8o+, 98o+" TBet = "ATo+, KJo+, QJo+, 44+, 65s, 76s, 87s, 97s+, T8s+, J9s+, Q9s+, K9s+, A2s+" FBet = "TT+, AJs+, KQs, AQo+" villain_ranges = {2: RFI, 3: TBet, 4: FBet} villain = eval7.HandRange(villain_ranges[pot_type]) equity = eval7.py_hand_vs_range_monte_carlo(hand, villain, board, 1000) if raised: if equity > 0.99: return "Raise " + str(2.5 * bet) elif equity > 0.95: return "Call" else: return "Fold" if equity > 0.97: if bet > 0: return "Raise " + str(2.5 * bet) else: return "Bet " + str(0.8 * pot) # else, look into non-raising methods # multi-way pot if opponents > 1: # someone bet, only call if top pair or better (< 2 rounds of bets) if bet > 0: if first_bet: if current_hand != 'High Card' and not play_the_board( cards, board_cards): if current_hand == 'Pair': if not tpttk(cards, board_cards): return "Fold" return "Call" if play_the_board(cards, board_cards): return "Fold" if current_hand == 'High Card' or current_hand == 'Pair': return "Fold" elif current_hand == 'Two Pair': if paired_card(board_cards) and max(list( board_cards[::2])) >= highest_two_pair( cards, board_cards): return "Fold" return "Call" else: return "Call" # no one bet, bet if you have trips or better if current_hand not in ['High Card', 'Pair', 'Two Pair' ] and not play_the_board(cards, board_cards): return "Bet " + str(pot * 0.8) else: return "Check" # heads up else: # someone bet, only call if enough equity, above 80% if bet > 0: if first_bet: if equity > bet / (bet + pot): return "Call" else: return "Fold" elif equity > 0.80: return "Call" else: return "Fold" else: if equity > 0.80: return "Bet " + str(pot * 0.8) else: return "Check"
def betting_round_2_wrapped(cards, board_cards, opponents, bet, pot, preflop_agg=False, position=False, raised=False, pot_type=2): hand = [eval7.Card(s) for s in (cards[:2], cards[2:])] board = [ eval7.Card(s) for s in [board_cards[i:i + 2] for i in range(0, len(board_cards), 2)] ] current_hand = eval7.hand_type(eval7.evaluate(hand + board)) if raised: if current_hand in ['High Card', 'Pair', 'Two Pair'] or play_the_board( cards, board_cards): return "Fold" else: return "Call" # multi-way pot, bluff less if opponents > 1: # someone bet again, only call if have top pair (if bet < pot) or better if bet > 0: if play_the_board(cards, board_cards): return "Fold" if current_hand == 'High Card': return "Fold" elif current_hand == 'Pair': if not tpttk(cards, board_cards): return "Fold" else: return "Call" elif current_hand == 'Two Pair': if paired_card(board_cards): return "Fold" return "Call" else: return "Call" # no one bet, bet if you are pre-flop aggressor IP if (preflop_agg or position) and current_hand not in ['High Card', 'Pair'] and \ not play_the_board(cards, board_cards): return "Bet " + str(pot * 0.70) else: return "Check" # heads up else: # since not playing limp pot, pots are either RFI, 3-bet, 4-bet+ RFI = "22+, 32s, 53s+, 63s+, 74s+, 84s+, 95s+, T6s+, J6s+, " \ "Q4s+, K2s+, A2s+, A2o+, K8o+, Q8o+, J8o+, T8o+, 98o+" TBet = "ATo+, KJo+, QJo+, 44+, 65s, 76s, 87s, 97s+, T8s+, J9s+, Q9s+, K9s+, A2s+" FBet = "TT+, AJs+, KQs, AQo+" villain_ranges = {2: RFI, 3: TBet, 4: FBet} villain = eval7.HandRange(villain_ranges[pot_type]) equity = eval7.py_hand_vs_range_monte_carlo(hand, villain, board, 1000) # someone bet, only call if above 75% equity, since second bullet if bet > 0: if equity > 0.75: return "Call" else: return "Fold" # no one bet, bet if you are pre-flop aggressor and have > 50% equity against range if (preflop_agg or position) and equity > 0.5: return "Bet " + str(pot * 0.7) else: return "Check"
def betting_round_1_wrapped(cards, board_cards, opponents, bet, pot, preflop_agg=False, position=False, raised=False, pot_type=2): hand = [eval7.Card(s) for s in (cards[:2], cards[2:])] board = [ eval7.Card(s) for s in [board_cards[i:i + 2] for i in range(0, len(board_cards), 2)] ] current_hand = eval7.hand_type(eval7.evaluate(hand + board)) if raised: if current_hand in ['High Card', 'Pair'] or play_the_board( cards, board_cards): return "Fold" elif current_hand == 'Two Pair': if paired_card(board_cards) and paired_card( board_cards) >= highest_two_pair(cards, board_cards): return "Fold" return "Call" else: return "Call" # multi-way pot, bluff less if opponents > 1: # someone bet, only call if have pair, oesd, fd if bet > 0: if play_the_board(cards, board_cards): return "Fold" if current_hand == 'High Card': # want a good flush draw, or an oesd w/ hole cards if bet < pot and (good_flush_draw(cards, board_cards) or (oesd(cards, board_cards) and not oesd("", board_cards))): return "Call" return "Fold" elif current_hand == 'Pair' and bet > 0.5 * pot: return "Fold" else: return "Call" # no one bet, bet if you are pre-flop aggressor IP if (preflop_agg or position ) and current_hand != 'High Card' and not play_the_board( cards, board_cards): return "Bet " + str(pot / 3) else: return "Check" # heads up else: # since not playing limp pot, pots are either RFI, 3-bet, 4-bet+ RFI = "22+, 32s, 53s+, 63s+, 74s+, 84s+, 95s+, T6s+, J6s+, " \ "Q4s+, K2s+, A2s+, A2o+, K8o+, Q8o+, J8o+, T8o+, 98o+" TBet = "ATo+, KJo+, QJo+, 44+, 65s, 76s, 87s, 97s+, T8s+, J9s+, Q9s+, K9s+, A2s+" FBet = "TT+, AJs+, KQs, AQo+" villain_ranges = {2: RFI, 3: TBet, 4: FBet} villain = eval7.HandRange(villain_ranges[pot_type]) equity = eval7.py_hand_vs_range_monte_carlo(hand, villain, board, 1000) # someone bet, only call if enough equity if bet > 0: if equity > bet / (bet + pot): return "Call" else: return "Fold" # no one bet, bet if you are pre-flop aggressor and have > 50% equity against range if (preflop_agg or position) and equity > 0.5: return "Bet " + str(pot / 3) else: return "Check"
#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Created on Sun Jan 14 20:42:30 2018 @author: ant """ import eval7 import pprint hand = map(eval7.Card, ("As", "Kd")) villain = eval7.HandRange("2c 2s") board = [] equity_monte = eval7.py_hand_vs_range_monte_carlo(hand, villain, board, 1000000) equity_exact = eval7.py_hand_vs_range_exact(hand, villain, board) print(equity_monte) print(equity_exact)
import matplotlib.pyplot as plt import numpy as np N = 1000 VALUES = [str(i) for i in range(2, 10)] + ['T', 'J', 'Q', 'K', 'A'] # build all possible hands # pairs: hands = [[s + s, 6, 0] for s in VALUES] # non-pairs: hands += [[VALUES[i] + VALUES[j] + s, 12 if s == 'o' else 4, 0] for i in range(len(VALUES)) for j in range(i + 1, len(VALUES)) for s in ['o', 's']] total_hands = sum(x[1] for x in hands) random_hand = eval7.HandRange(','.join(x[0] for x in hands)) print('Calculating hand equities...') for i, hand in enumerate(hands): equities = eval7.py_all_hands_vs_range(eval7.HandRange(hand[0]), random_hand, [], N) hand[2] = np.mean([equities[v] for v in equities]) hands = sorted(hands, key=lambda x: x[2], reverse=True) f = np.zeros(len(hands)) equities = np.zeros(len(hands)) print('Calculating range equities...') cur_fraction = 0 for i, hand in enumerate(hands):