def run(hole_cards, num, exact, board, file_name, verbose, print_elapsed_time=False): t0 = time.time() if file_name: input_file = open(file_name, 'r') for line in input_file: if line is not None and len(line.strip()) == 0: continue hole_cards, board = holdem_argparser.parse_file_args(line) deck = holdem_functions.generate_deck(hole_cards, board) run_simulation(hole_cards, num, exact, board, deck, verbose) print("-----------------------------------") input_file.close() else: deck = holdem_functions.generate_deck(hole_cards, board) result = run_simulation(hole_cards, num, exact, board, deck, verbose) if print_elapsed_time: print("Time elapsed: ", time.time() - t0) return result
def parse_args(): # Define possible command line arguments parser = argparse.ArgumentParser( description="Find the odds that a Texas Hold'em hand will win. Note " "that cards must be given in the following format: As, Jc, Td, 3h.") parser.add_argument("cards", type=str, nargs="*", metavar="hole card", help="Hole cards you want to find the odds for.") parser.add_argument("-b", "--board", nargs="*", type=str, metavar="card", help="Add board cards") parser.add_argument("-e", "--exact", action="store_true", help="Find exact odds by enumerating every possible board") parser.add_argument("-n", type=int, default=100000, help="Run N Monte Carlo simulations") # Parse command line arguments and check for errors args = parser.parse_args() error_check(args) # Parse hole cards hole_cards = parse_hole_cards(args.cards) board = None # Create the deck. If the user has defined a board, parse the board. if args.board: board = parse_cards(args.board) all_cards = list(hole_cards) all_cards.append(board) deck = holdem_functions.generate_deck(all_cards) else: deck = holdem_functions.generate_deck(hole_cards) return hole_cards, args.n, args.exact, board, deck
def run(hole_cards, num, exact, board, file_name, verbose): if file_name: input_file = open(file_name, 'r') for line in input_file: if line is not None and len(line.strip()) == 0: continue hole_cards, board = holdem_argparser.parse_file_args(line) deck = holdem_functions.generate_deck(hole_cards, board) run_simulation(hole_cards, num, exact, board, deck, verbose) print("-----------------------------------") input_file.close() else: deck = holdem_functions.generate_deck(hole_cards, board) return run_simulation(hole_cards, num, exact, board, deck, verbose)
def run(hole_cards, num, exact, board, file_name, verbose): if file_name: input_file = open(file_name, 'r') for line in input_file: if line is not None and len(line.strip()) == 0: continue hole_cards, board = holdem_argparser.parse_file_args(line) deck = holdem_functions.generate_deck(hole_cards, board) run_simulation(hole_cards, num, exact, board, deck, verbose) print "-----------------------------------" input_file.close() else: deck = holdem_functions.generate_deck(hole_cards, board) return run_simulation(hole_cards, num, exact, board, deck, verbose)
def evaluate(hole_cards, adversary_hole_cards, given_board=None, is_parallel=True, num_iterations=1000000): """Evaluate the winning probability given player's cards and board cards. :param hole_cards: a list of string like ["As", "Td"] :param adversary_hole_cards: a list of string like ["As", "Td"] :param given_board: a list of string like ["Ac", "Kd", "3c"] :param num_iterations: int :return: two floats, player's winning probability and his adversary's """ hole_cards = tuple([Card(x) for x in hole_cards]) adversary_hole_cards = tuple([Card(x) for x in adversary_hole_cards]) all_cards = [hole_cards, adversary_hole_cards] hole_cards = (hole_cards, adversary_hole_cards) if given_board is not None: given_board = [Card(x) for x in given_board] all_cards.append(given_board) deck = holdem_functions.generate_deck(all_cards) if is_parallel: return _parallel_evaluate( hole_cards, deck, given_board=given_board, num_iterations=num_iterations ) else: return _evaluate( hole_cards, deck, given_board=given_board, num_iterations=num_iterations )
def evaluate(hole_cards, adversary_hole_cards, given_board=None, is_parallel=True, num_iterations=1000000): """Evaluate the winning probability given player's cards and board cards. :param hole_cards: a list of string like ["As", "Td"] :param adversary_hole_cards: a list of string like ["As", "Td"] :param given_board: a list of string like ["Ac", "Kd", "3c"] :param num_iterations: int :return: two floats, player's winning probability and his adversary's """ hole_cards = tuple([Card(x) for x in hole_cards]) adversary_hole_cards = tuple([Card(x) for x in adversary_hole_cards]) all_cards = [hole_cards, adversary_hole_cards] hole_cards = (hole_cards, adversary_hole_cards) if given_board is not None: given_board = [Card(x) for x in given_board] all_cards.append(given_board) deck = holdem_functions.generate_deck(all_cards) if is_parallel: return _parallel_evaluate(hole_cards, deck, given_board=given_board, num_iterations=num_iterations) else: return _evaluate(hole_cards, deck, given_board=given_board, num_iterations=num_iterations)
def parse_args(): # Define possible command line arguments parser = argparse.ArgumentParser( description="Find the odds that a Texas Hold'em hand will win. Note " "that cards must be given in the following format: As, Jc, Td, 3h.") parser.add_argument("cards", type=str, nargs="*", metavar="hole card", help="Hole cards you want to find the odds for.") parser.add_argument("-b", "--board", nargs="*", type=str, metavar="card", help="Add board cards") parser.add_argument( "-e", "--exact", action="store_true", help="Find exact odds by enumerating every possible board") parser.add_argument("-n", type=int, default=100000, help="Run N Monte Carlo simulations") parser.add_argument("-p", type=int, default=0, help="Add P components") # Parse command line arguments and check for errors args = parser.parse_args() error_check(args) # Parse hole cards hole_cards = parse_hole_cards(args.cards) board = None # Create the deck. If the user has defined a board, parse the board. if args.board: board = parse_cards(args.board) all_cards = list(hole_cards) all_cards.append(board) deck = holdem_functions.generate_deck(all_cards) else: deck = holdem_functions.generate_deck(hole_cards) return hole_cards, args.n, args.exact, board, deck, args.p
def calc(hold_card, board_card=None): # generate parameter hole_cards = holdem_argparser.parse_hole_cards(hold_card) given_board = holdem_argparser.parse_cards(board_card) if given_board: all_cards = list(hole_cards) all_cards.append(given_board) deck = holdem_functions.generate_deck(all_cards) else: deck = holdem_functions.generate_deck(hole_cards) num_iterations = 10000 exact = False # copy from holem_calc.py main fuction num_players = 1 # Create results data structures which tracks results of comparisons # 1) result_histograms: a list for each player that shows the number of # times each type of poker hand (e.g. flush, straight) was gotten # 2) winner_list: number of times each player wins the given round # 3) result_list: list of the best possible poker hand for each pair of # hole cards for a given board result_list, winner_list = [None] * num_players, [0] * (num_players + 1) result_histograms = [] for player in xrange(num_players): result_histograms.append([0] * 10) # Choose whether we're running a Monte Carlo or exhaustive simulation board_length = 0 if given_board == None else len(given_board) # When a board is given, exact calculation is much faster than Monte Carlo # simulation, so default to exact if a board is given if exact or given_board is not None: generate_boards = holdem_functions.generate_exhaustive_boards else: generate_boards = holdem_functions.generate_random_boards # Run simulations for remaining_board in generate_boards(deck, num_iterations, board_length): # Generate a new board if given_board: board = given_board[:] board.extend(remaining_board) else: board = remaining_board # Find the best possible poker hand given the created board and the # hole cards and save them in the results data structures (suit_histogram, histogram, max_suit) = holdem_functions.preprocess_board(board) for index, hole_card in enumerate(hole_cards): result_list[index] = holdem_functions.detect_hand( hole_card, board, suit_histogram, histogram, max_suit) # Find the winner of the hand and tabulate results winner_index = holdem_functions.compare_hands(result_list) winner_list[winner_index] += 1 # Increment what hand each player made for index, result in enumerate(result_list): result_histograms[index][result[0]] += 1 float_iterations = float(sum(winner_list)) pro_result = [None] * 10 for index, elem in enumerate(result_histograms[0]): pro_result[index] = float(elem) / float_iterations return pro_result
def run(hole_cards, num, exact, board, file_name, verbose): deck = holdem_functions.generate_deck(hole_cards, board) return run_simulation(hole_cards, num, exact, board, deck, verbose)
def calc(hold_card, board_card=None): # generate parameter hole_cards = holdem_argparser.parse_hole_cards(hold_card) given_board = holdem_argparser.parse_cards(board_card) if given_board: all_cards = list(hole_cards) all_cards.append(given_board) deck = holdem_functions.generate_deck(all_cards) else: deck = holdem_functions.generate_deck(hole_cards) num_iterations = 10000 exact = False # copy from holem_calc.py main fuction num_players = 1 # Create results data structures which tracks results of comparisons # 1) result_histograms: a list for each player that shows the number of # times each type of poker hand (e.g. flush, straight) was gotten # 2) winner_list: number of times each player wins the given round # 3) result_list: list of the best possible poker hand for each pair of # hole cards for a given board result_list, winner_list = [None] * num_players, [0] * (num_players + 1) result_histograms = [] for player in xrange(num_players): result_histograms.append([0] * 10) # Choose whether we're running a Monte Carlo or exhaustive simulation board_length = 0 if given_board == None else len(given_board) # When a board is given, exact calculation is much faster than Monte Carlo # simulation, so default to exact if a board is given if exact or given_board is not None: generate_boards = holdem_functions.generate_exhaustive_boards else: generate_boards = holdem_functions.generate_random_boards # Run simulations for remaining_board in generate_boards(deck, num_iterations, board_length): # Generate a new board if given_board: board = given_board[:] board.extend(remaining_board) else: board = remaining_board # Find the best possible poker hand given the created board and the # hole cards and save them in the results data structures (suit_histogram, histogram, max_suit) = holdem_functions.preprocess_board(board) for index, hole_card in enumerate(hole_cards): result_list[index] = holdem_functions.detect_hand(hole_card, board, suit_histogram, histogram, max_suit) # Find the winner of the hand and tabulate results winner_index = holdem_functions.compare_hands(result_list) winner_list[winner_index] += 1 # Increment what hand each player made for index, result in enumerate(result_list): result_histograms[index][result[0]] += 1 float_iterations = float(sum(winner_list)) pro_result = [None]*10 for index, elem in enumerate(result_histograms[0]): pro_result[index] = float(elem) / float_iterations return pro_result