def run_round(self, players): ''' Runs one round of poker. ''' deck = eval7.Deck() deck.shuffle() hands = [deck.deal(2), deck.deal(2)] pips = [cfg.SMALL_BLIND, cfg.BIG_BLIND] stacks = [ cfg.STARTING_STACK - cfg.SMALL_BLIND, cfg.STARTING_STACK - cfg.BIG_BLIND ] round_state = RoundState(0, 0, pips, stacks, hands, deck, None) while not isinstance(round_state, TerminalState): self.log_round_state(players, round_state) active = round_state.button % 2 player = players[active] action = player.query(round_state, self.player_messages[active], self.log) bet_override = (round_state.pips == [0, 0]) self.log_action(player.name, action, bet_override) round_state = round_state.proceed(action) self.log_terminal_state(players, round_state) for player, player_message, delta in zip(players, self.player_messages, round_state.deltas): player.query(round_state, player_message, self.log) player.bankroll += delta
def getScore(c1, c2, iters=10000): score = 0 for i in range(iters): deck = eval7.Deck() deck.shuffle() myHand = [c1, c2] d = deck.deal(52) d.remove(c1) d.remove(c2) oppHand = [] for i in range(2): oppHand.append(d.pop(0)) street = [] for i in range(5): street.append(d.pop(0)) myHand = myHand + street oppHand = oppHand + street if eval7.evaluate(myHand) > eval7.evaluate(oppHand): score += 1 return score / iters
def calc_win_prob_by_sampling_eval7(sim_num, hole_cards, board_cards, data): """ Calculate the probability to win current players by sampling unknown cards Compute the probability to win one player first And then take the power of virtual player count """ o_hole_cards = [] o_board_cards = [] deck = eval7.Deck() # remove hole cards and board cards from deck for card in hole_cards: card = eval7.Card(card) o_hole_cards.append(card) deck.cards.remove(card) board_cards_to_draw = 5 for card in board_cards: board_cards_to_draw -= 1 card = eval7.Card(card) o_board_cards.append(card) deck.cards.remove(card) vpc_weighted = 1 rivals_count = get_rivals_count(data) win = 0 succeeded_sample = 0 start = time.time() if board_cards_to_draw: for _ in range(sim_num // rivals_count): simulate_cards = deck.sample(board_cards_to_draw + 2 * rivals_count) o_board_sample = o_board_cards + simulate_cards[:board_cards_to_draw] my_rank = eval7.evaluate(o_board_sample + o_hole_cards) won = True for i in range(board_cards_to_draw, board_cards_to_draw + 2 * rivals_count, 2): if my_rank < eval7.evaluate(o_board_sample + simulate_cards[i:i + 2]): won = False break if won: win += 1 succeeded_sample += 1 else: my_rank = eval7.evaluate(o_board_cards + o_hole_cards) n = sim_num * 1.1 for _ in range(n // rivals_count): won = True simulate_cards = deck.sample(2 * rivals_count) for i in range(0, 2 * rivals_count, 2): if my_rank < eval7.evaluate(o_board_cards + simulate_cards[i:i + 2]): won = False break if won: win += 1 succeeded_sample += 1 print("==== sampling result ==== win : %d, total : %d, takes: %f seconds" % (win, succeeded_sample, time.time() - start)) win_one_prob = win * vpc_weighted / succeeded_sample # win_all_prob = win_one_prob ** virtual_player_count(data) print("==== Win probability ==== " + str(win_one_prob)) return win_one_prob
def calculate_strength(self, hole, board_cards, iters): ''' A Monte Carlo method meant to estimate the win probability of a pair of hole cards. Simlulates 'iters' games and determines the win rates of our cards Arguments: hole: a list of our two hole cards iters: a integer that determines how many Monte Carlo samples to take ''' deck = eval7.Deck() #eval7 object! hole_cards = [eval7.Card(card) for card in hole] #card objects, used to evaliate hands board_cards = [eval7.Card(card) for card in board_cards if card] for card in board_cards: deck.cards.remove(card) for card in hole_cards: deck.cards.remove(card) score = 0 for _ in range(iters): #take 'iters' samples deck.shuffle() #make sure our samples are random _COMM = 5 - len(board_cards) #the number of cards we need to draw _OPP = 2 draw = deck.peek(_COMM + _OPP) opp_hole = draw[:_OPP] community = draw[_OPP:] our_hand = hole_cards + community + board_cards #the two showdown hands opp_hand = opp_hole + community + board_cards our_hand_value = eval7.evaluate( our_hand ) #the ranks of our hands (only useful for comparisons) opp_hand_value = eval7.evaluate(opp_hand) if our_hand_value > opp_hand_value: #we win! score += 2 elif our_hand_value == opp_hand_value: #we tie. score += 1 else: #we lost.... score += 0 hand_strength = score / (2 * iters) #this is our win probability! return hand_strength
def create_new_round(button_player): """ Randomly generate a round_state to start a new round. button_player (int) : 0 if PLAYER1 should be button (small blind), 1 if PLAYER2. NOTE: Button should alternate every time. """ deck = eval7.Deck() deck.shuffle() hands = [deck.deal(2), deck.deal(2)] sb = Constants.SMALL_BLIND_AMOUNT bb = 2 * Constants.SMALL_BLIND_AMOUNT pips = [sb, bb] stacks = [Constants.INITIAL_STACK - sb, Constants.INITIAL_STACK - bb] if button_player == 1: pips.reverse() stacks.reverse() round_state = RoundState(button_player, 0, pips, stacks, hands, deck, None, [[1, 2]], button_player) return round_state
def calc_num_outs(curr_hand): # no outs pre flop if len(curr_hand) == 2: return 0 # calculate outs after flop and turn deck = eval7.Deck() remaining_cards = list(set(deck.cards) - set(curr_hand)) num_outs = 0 curr_strength = eval7.evaluate(curr_hand) curr_rank = eval7.handtype(curr_strength) for card in remaining_cards: hand = curr_hand + [card] hand_strength = eval7.evaluate(hand) hand_rank = eval7.handtype(hand_strength) if hand_strength > curr_strength and hand_rank != curr_rank: num_outs += 1 return num_outs
def run_round(self, players): ''' Runs one round of poker. ''' deck = eval7.Deck() deck.shuffle() hands = [deck.deal(NUM_BOARDS * 2), deck.deal(NUM_BOARDS * 2)] new_decks = [SmallDeck(deck) for i in range(NUM_BOARDS)] for new_deck in new_decks: new_deck.shuffle() stacks = [ STARTING_STACK - NUM_BOARDS * SMALL_BLIND, STARTING_STACK - NUM_BOARDS * BIG_BLIND ] board_states = [ BoardState((i + 1) * BIG_BLIND, [SMALL_BLIND, BIG_BLIND], None, new_decks[i], None) for i in range(NUM_BOARDS) ] round_state = RoundState(-2, 0, stacks, hands, board_states, None) while not isinstance(round_state, TerminalState): self.log_round_state(players, round_state) active = round_state.button % 2 player = players[active] actions = player.query(round_state, self.player_messages[active], self.log, active) bet_overrides = [ (round_state.board_states[i].pips == [0, 0]) if isinstance( round_state.board_states[i], BoardState) else None for i in range(NUM_BOARDS) ] self.log_actions(player.name, actions, bet_overrides, active) round_state = round_state.proceed(actions) self.log_terminal_state(players, round_state) for player, player_message, delta in zip(players, self.player_messages, round_state.deltas): player.query(round_state, player_message, self.log, None) player.bankroll += delta
import eval7
def _set_zeros(self): # Initialize range with zeros. deck = eval7.Deck() for i, card in enumerate(deck[:-1]): for other_card in deck[i + 1:]: self[(other_card, card)] = 0.0 # Higher card must be first!
def calc_win_prob_by_sampling_eval7(self, hole_cards, board_cards, data, sim_num): """ Calculate the probability to win current players by sampling unknown cards Compute the probability to win one player first And then take the power of virtual player count """ o_hole_cards = [] o_board_cards = [] deck = eval7.Deck() # remove hole cards and board cards from deck for card in hole_cards: card = eval7.Card(card) o_hole_cards.append(card) deck.cards.remove(card) board_cards_to_draw = 5 for card in board_cards: board_cards_to_draw -= 1 card = eval7.Card(card) o_board_cards.append(card) deck.cards.remove(card) # vpc_weighted = virtual_player_count_weighted() rivals_count = 1 # self.get_rivals_count(data) win = 0 succeeded_sample = 0 start = time.time() # TODO: Remove small rivals if board_cards_to_draw: n = sim_num index = 10 for iteration in range(n // rivals_count): simulate_cards, _ = self.exclude_impossible_rivals_lookup(deck, board_cards_to_draw, rivals_count) o_board_sample = o_board_cards + simulate_cards[:board_cards_to_draw] my_rank = eval7.evaluate(o_board_sample + o_hole_cards) won = True for i in range(board_cards_to_draw, board_cards_to_draw + 2 * rivals_count, 2): if my_rank < eval7.evaluate(o_board_sample + simulate_cards[i:i + 2]): won = False break if won: win += 1 succeeded_sample += 1 if iteration == index: print("==== sampling result ==== win : %d, total : %d, probability : %f, takes: %f seconds" % (win, succeeded_sample, win / succeeded_sample, time.time() - start)) index *= 10 else: my_rank = eval7.evaluate(o_board_cards + o_hole_cards) n = sim_num index = 10 for iteration in range(n // rivals_count): won = True simulate_cards, _ = self.exclude_impossible_rivals_lookup(deck, 0, rivals_count) # simulate_cards = deck.sample(2 * rivals_count) for i in range(0, 2 * rivals_count, 2): if my_rank < eval7.evaluate(o_board_cards + simulate_cards[i:i + 2]): won = False break if won: win += 1 succeeded_sample += 1 if iteration == index: print("==== sampling result ==== win : %d, total : %d, probability : %f, takes: %f seconds" % (win, succeeded_sample, win / succeeded_sample, time.time() - start)) index *= 10 print("==== sampling result ==== win : %d, total : %d, probability : %f, takes: %f seconds" % (win, succeeded_sample, win / succeeded_sample, time.time() - start)) return win / succeeded_sample
from itertools import combinations import eval7 import numpy as np from neural import get_answer deck = eval7.Deck() def play_against(net_1, net_2, iterations, blinds, hands, learning=True): """ Play two different NeuralNetCollections against eachother. """ for i in range(iterations): for network_from_1 in net_1.networks: for network_from_2 in net_2.networks: play_game(network_from_1, network_from_2, blinds, hands) # Switch places play_game(network_from_2, network_from_1, blinds, hands) if learning: net_1.update_networks(i) net_2.update_networks(i) else: net_1.print_networks(i) net_2.print_networks(i) net_1.reset_performance() net_2.reset_performance() answer = input("Do you want to save the networks? y/n ") if answer == "y":