forked from ktseng/holdem_calc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
holdem_calc.py
73 lines (67 loc) · 3.49 KB
/
holdem_calc.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import time
import holdem_functions
import holdem_argparser
def main():
hole_cards, num, exact, board, file_name = holdem_argparser.parse_args()
run(hole_cards, num, exact, board, file_name, True)
def calculate(board, exact, num, input_file, hole_cards, verbose):
args = holdem_argparser.LibArgs(board, exact, num, input_file, hole_cards)
hole_cards, n, e, board, filename = holdem_argparser.parse_lib_args(args)
return run(hole_cards, n, e, board, filename, 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 run_simulation(hole_cards, num, exact, given_board, deck, verbose):
num_players = len(hole_cards)
# Create results data structures which track 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_histograms, winner_list = [], [0] * (num_players + 1)
for _ in xrange(num_players):
result_histograms.append([0] * len(holdem_functions.hand_rankings))
# Choose whether we're running a Monte Carlo or exhaustive simulation
board_length = 0 if given_board is 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
if (None, None) in hole_cards:
hole_cards_list = list(hole_cards)
unknown_index = hole_cards.index((None, None))
for filler_hole_cards in holdem_functions.generate_hole_cards(deck):
hole_cards_list[unknown_index] = filler_hole_cards
deck_list = list(deck)
deck_list.remove(filler_hole_cards[0])
deck_list.remove(filler_hole_cards[1])
holdem_functions.find_winner(generate_boards, tuple(deck_list),
tuple(hole_cards_list), num,
board_length, given_board, winner_list,
result_histograms)
else:
holdem_functions.find_winner(generate_boards, deck, hole_cards, num,
board_length, given_board, winner_list,
result_histograms)
if verbose:
holdem_functions.print_results(hole_cards, winner_list,
result_histograms)
return holdem_functions.find_winning_percentage(winner_list)
if __name__ == '__main__':
start = time.time()
main()
print "\nTime elapsed(seconds): ", time.time() - start