def start(self, **kwargs): if self.player_order == "" or not self.player_order: prev = 0 else: prev = int(self.player_order[0] + 1) if prev == self.game.player_num: prev = 0 self.set_player_order(prev) self.set_player_active_dict() self.set_player_prev_bet_dict() new_deck = deuces.Deck() board = new_deck.draw(5) board_str = '' for card in board: board_str += str(card) + ',' self.dealer_cards = board_str[:-1] player_hands_dict = {} num = self.game.player_num for player in self.game.players.all(): player_hands_dict[player.id] = new_deck.draw(2) self.player_cards = str(player_hands_dict) self.player_fund_dict = self.game.player_fund_dict # ------------Send small blind and big blind ------------- player_order = self.player_order player_order_list_round = eval(player_order) self.set_player_prev_bet(player_order_list_round[0], self.min_bet) self.set_player_prev_bet(player_order_list_round[1], 2*self.min_bet) self.current_max_player = player_order_list_round[1]
def evaluate_showdown_probabilities(self, hand, board, nsim): board_cards = [Card.new(j) for j in board] hand_cards = [Card.new(j) for j in hand] cards_in_play = cp.copy(board_cards) cards_in_play.extend(hand_cards) board_cards_to_draw = (5 - len(board)) hand_cards_to_draw = (2 - len(hand)) villain_cards_to_draw = 2 num_cards_to_draw = board_cards_to_draw + hand_cards_to_draw + villain_cards_to_draw deck = de.Deck() draw_deck = list(set(deck.cards) - set(cards_in_play)) nwins = 0.0 for i in range(nsim): rest_of_cards = sample(draw_deck, num_cards_to_draw) board_sim = cp.copy(board_cards) hand_sim = cp.copy(hand_cards) board_sim.extend(rest_of_cards[0:board_cards_to_draw]) hand_sim.extend( rest_of_cards[board_cards_to_draw:(board_cards_to_draw + hand_cards_to_draw)]) villain_hand = rest_of_cards[(board_cards_to_draw + hand_cards_to_draw):] villain_rank = self.card_evaluator.evaluate( board_sim, villain_hand) hero_rank = self.card_evaluator.evaluate(board_sim, hand_sim) nwins += hero_rank < villain_rank win_pct = nwins / nsim return win_pct
def __init__(self, qntd_players): self.game_deck = deuces.Deck() self.game_players = [self.game_deck.draw(2), self.game_deck.draw(2)] self.last_act = [1, 1] self.board = self.game_deck.draw(5) self.player_turn = 0 self.now = 'bet' self.state = 1 self.eval = deuces.Evaluator()
def deal_hand(): deck = deuces.Deck() board = deck.draw(5) hand = deck.draw(2) # print("Dealing hand: ") # for current_card in hand: # card.print_pretty_card(current_card) # print("Dealing board: ") # for current_card in board: # card.print_pretty_card(current_card) return {'board': board, 'hand': hand}
def _init_deck(self, board=None, dead_cards=None): deck = deuces.Deck() for c in self.pokerscript.player.hand: deck.cards.remove(c) for c in board or self.pokerscript.game.community_cards: deck.cards.remove(c) if dead_cards: dead_cards = [ deuces.Card.new(r + s) for r, s in zip(hand[::2], hand[1::2]) ] for c in dead_cards: try: deck.cards.remove(c) except: pass return deck
def __init__(self, qntd_players): # self.game_deck = deuces.deck # self.player_turn = 0 # self.state = 0 # self.game = game(qntd_players) # self.game.board = self.game.game_deck.draw(5) # self.game.players = [self.game.game_deck.draw(2),self.game.game_deck.draw(2)] # self.players_list = self.game.player_on self.game_deck = deuces.Deck() self.game_players = [self.game_deck.draw(2), self.game_deck.draw(2)] self.last_act = [1, 1] self.board = self.game_deck.draw(5) self.player_turn = 0 self.state = 1 self.eval = deuces.Evaluator()
def play_war(wager_amount): deck = deuces.Deck() player_card = deck.draw(1) dealer_card = deck.draw(1) player_war_card = '' dealer_war_card = '' # evaluate the difference of the player and dealer cards result = evaluate_win(player_card, dealer_card) if result > 0: winnings = calculate_winnings(result, wager_amount, False) outcome = SG_Repository.WagerOutcome.WIN elif result < 0: winnings = calculate_winnings(result, wager_amount, False) outcome = SG_Repository.WagerOutcome.LOSE else: player_war_card = deck.draw(1) dealer_war_card = deck.draw(1) war_result = evaluate_win(player_war_card, dealer_war_card) if war_result > 0: winnings = calculate_winnings(war_result, wager_amount, True) outcome = SG_Repository.WagerOutcome.WIN elif war_result < 0: winnings = calculate_winnings(war_result, wager_amount, True) outcome = SG_Repository.WagerOutcome.LOSE else: winnings = calculate_winnings(war_result, wager_amount, True) outcome = SG_Repository.WagerOutcome.WIN player_war_card = card.int_to_pretty_str(player_war_card) dealer_war_card = card.int_to_pretty_str(dealer_war_card) wager_result = { 'player_card': card.int_to_pretty_str(player_card), 'dealer_card': card.int_to_pretty_str(dealer_card), 'outcome': outcome, 'winnings': winnings, 'player_war_card': player_war_card, 'dealer_war_card': dealer_war_card } return wager_result
def calc_win_prob_by_sampling(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 """ evaluator = deuces.Evaluator() o_hole_cards = [] o_board_cards = [] for card in hole_cards: o_hole_card = deuces.Card.new(card) o_hole_cards.append(o_hole_card) for card in board_cards: o_board_card = deuces.Card.new(card) o_board_cards.append(o_board_card) n = 1000 win = 0 succeeded_sample = 0 for i in range(n): deck = deuces.Deck() board_cards_to_draw = 5 - len(o_board_cards) o_board_sample = o_board_cards + deck.draw(board_cards_to_draw) o_hole_sample = deck.draw(2) try: my_rank = evaluator.evaluate(o_board_sample, o_hole_cards) rival_rank = evaluator.evaluate(o_board_sample, o_hole_sample) except: continue if my_rank <= rival_rank: win += 1 succeeded_sample += 1 print "==== sampling result ==== win : %d, total : %d" % (win, succeeded_sample) win_one_prob = win / float(succeeded_sample) win_all_prob = win_one_prob**virtual_player_count(data) print "==== Win probability ==== " + str(win_all_prob) return win_all_prob
def deal_cards(self): self.deck = deuces.Deck() for p in self.players: p.hand = self.deck.draw(4)
def play_war(wager_amount): deck = deuces.Deck() player_card = deck.draw(1) dealer_card = deck.draw(1) print "Player: {} {}".format(card.int_to_pretty_str(player_card), card.get_rank_int(player_card)) print "Dealer: {} {}".format(card.int_to_pretty_str(dealer_card), card.get_rank_int(dealer_card)) player_war_card = '' dealer_war_card = '' # evaluate the difference of the player and dealer cards result = evaluate_win(player_card, dealer_card) if result > 0: winnings = calculate_winnings(result, wager_amount, False) print "Player wins - winnings = {}!".format(winnings) outcome = SG_Repository.WagerOutcome.WIN elif result < 0: winnings = calculate_winnings(result, wager_amount, False) print "Player loses - winnings = {}!".format(winnings) outcome = SG_Repository.WagerOutcome.LOSE else: print "Tie! Going to war...\n\n" player_war_card = deck.draw(1) dealer_war_card = deck.draw(1) print "Player: {} {}".format(card.int_to_pretty_str(player_war_card), card.get_rank_int(player_war_card)) print "Dealer: {} {}".format(card.int_to_pretty_str(dealer_war_card), card.get_rank_int(dealer_war_card)) war_result = evaluate_win(player_war_card, dealer_war_card) if war_result > 0: winnings = calculate_winnings(war_result, wager_amount, True) print "Player wins - winnings = {}!".format(winnings) outcome = SG_Repository.WagerOutcome.WIN elif war_result < 0: winnings = calculate_winnings(war_result, wager_amount, True) print "Player loses - winnings = {}!".format(winnings) outcome = SG_Repository.WagerOutcome.LOSE else: winnings = calculate_winnings(war_result, wager_amount, True) print "Player wins war tie - winnings = {}!".format(winnings) outcome = SG_Repository.WagerOutcome.WIN player_war_card = card.int_to_pretty_str(player_war_card) dealer_war_card = card.int_to_pretty_str(dealer_war_card) wager_result = { 'player_card': card.int_to_pretty_str(player_card), 'dealer_card': card.int_to_pretty_str(dealer_card), 'outcome': outcome, 'winnings': winnings, 'player_war_card': player_war_card, 'dealer_war_card': dealer_war_card } print("War wager result:") pprint.pprint(wager_result) return wager_result
def play(self, dealt_hook=None, flop_hook=None, turn_hook=None, river_hook=None, winner_hook=None): """Generator to yield hand summaries after playing rounds of poker. All hooks are of signature func(the_game_instance) -> None Keyword Arguments: dealt_hook {callable} -- A function called after hands are dealt and blinds are placed. (default: {None}) flop_hook {callable} -- A function called after the flop cards were dealt but before players bet (default: {None}) turn_hook {callable} -- A function called after the turn card was dealt but before players bet (default: {None}) river_hook {callable} -- A function called after the river card was dealt but before players bet (default: {None}) winner_hook {callable} -- A function called after the winner is determined (default: {None}) """ while True: if len(self.players) == 1: return # big blind always position 1 # small blind always position 0 deck = deuces.Deck() board = [] # create copy of players for this round # this allows adding playing while a round is in progress players = self.players[:] for player in players: player.reset(deck.draw(2)) blind_positions = [(self.big_blind - 1) % len(players), self.big_blind] for b, bi in zip(self.blinds, blind_positions): p = players[bi] assert p.stack > 0 p.bet = min(p.stack, b) p.stack -= p.bet bet = max([players[i].bet for i in blind_positions]) if dealt_hook is not None: dealt_hook(self) rounds = [ (blind_positions[1] + 1) % len(players) ] +\ [ blind_positions[0] ] * 3 n_dealt = [3, 1, 1, 0] # note the last is dummy hooks = [flop_hook, turn_hook, river_hook, None] n_left = reduce(lambda x, y: x + int(not y.has_folded), players, 0) for i, hook, n_dealt in zip(rounds, hooks, n_dealt): bet, n_left = bet_round(i, players, board, bet, n_left) if n_left == 1: break board.extend(deck.draw(n_dealt)) if hook is not None: hook(self) winners = distribute_pot(players, board) players = list(filter(lambda p: p.stack > 0, players)) yield RoundSummary(self.hands_played, board, players, winners, self.blinds) self.big_blind = (self.big_blind + 1) % len(self.players) self.hands_played += 1 self.blinds = self.blindstep(self.hands_played, self.blinds) self.players = list(filter(lambda p: p.stack > 0, self.players))