def assign_cards_to_display(self, guest_cards_st, learner_cards_st, reset = False): if reset: for card in self.guest_cards+self.learner_cards: card.pack_forget() position_cards = [0, 0] for card in self.guest_cards_st: guest_card = self.form_image(card) guest_card.pack(side='left', expand = False, padx=position_cards[0], pady=position_cards[1]) self.guest_cards.append(guest_card) for card in self.learner_cards_st: learner_card = self.form_image(card, learner=True) #if not self.sd_tr else self.form_image(card, learner=False) learner_card.pack(side='right', expand = False, padx=position_cards[0], pady=position_cards[1]) self.learner_cards.append(learner_card) cd = [] if self.community_display is not None: for card in self.community_display: card.pack_forget() if self.community_cards is not None: if not(all(i < 0 for i in self.community_cards)): if self.community_cards[0] is not -1 and self.community_cards[1] is not -1 and self.community_cards[2] is not -1: cd.append(Card.int_to_str(self.community_cards[0]).upper()) cd.append(Card.int_to_str(self.community_cards[1]).upper()) cd.append(Card.int_to_str(self.community_cards[2]).upper()) if self.community_cards[3] is not -1: cd.append(Card.int_to_str(self.community_cards[3]).upper()) if self.community_cards[4] is not -1: cd.append(Card.int_to_str(self.community_cards[4]).upper()) for card in cd: c = self.form_image(card, community=True) c.pack(side='left', expand = False, padx=20, pady=20) self.community_display.append(c)
def CalculateWinProb(self, hand, board): hand_string = [Card.int_to_str(card) for card in hand] hand_string.append("?") hand_string.append("?") equity = calculate([Card.int_to_str(card) for card in board], self.EXACT, self.NUM_SIMULATIONS, self.INPUT_FILE, hand_string, self.VERBOSE)[1] return equity
def delegate_state_info(self, reset): self.update_local_state(reset=reset) # self.assign_player_objects_to_display(reset=reset) self.guest_cards_st = [Card.int_to_str(self.p2.hand[0]).upper(), Card.int_to_str(self.p2.hand[1]).upper()] self.learner_cards_st = [Card.int_to_str(self.p1.hand[0]).upper(), Card.int_to_str(self.p1.hand[1]).upper()] # self.assign_cards_to_display(self.guest_cards_st, self.learner_cards_st, reset=reset) self.update_pot_size()
def test_card(self): deck = Deck() for i in range(2): self.guest.card_player_set.create(card_str = Card.int_to_str(deck.draw(1))) self.learner.card_player_set.create(card_str = Card.int_to_str(deck.draw(1))) self.assertTrue([len(x.card_str)<3 for x in self.guest.card_player_set.all()]) self.assertTrue([len(x.card_str)<3 for x in self.learner.card_player_set.all()]) for i in range(5): self.game.card_community_set.create(card_str = Card.int_to_str(deck.draw(1))) self.assertTrue([len(x.card_str)<3 for x in self.game.card_player_set.all()])
def _get_equities_c(self, hands, community, dead): for i in range(len(hands)): hands[i] = ''.join( [Card.int_to_str(hands[i][0]), Card.int_to_str(hands[i][1])]) community = [Card.int_to_str(h) for h in community] dead = [Card.int_to_str(h) for h in dead] hands = bytes(':'.join(hands), encoding='utf-8') community = bytes(''.join(community), encoding='utf-8') dead = bytes(''.join(dead), encoding='utf-8') results = calc(hands, b'', b'', self.n_evaluations) return results.ev
def __str__(self): string = "type:" + str(type(self)) + "\n" string += "hands:" + (self.hands if self.hands == "" else str( [Card.int_to_str(card) for card in self.hands])) + "\n" string += "board:" + (self.board if self.board == "" else str( [Card.int_to_str(card) for card in self.board])) + "\n" string += "stack:" + str(self.stack) + "\n" string += "pot:" + str(self.pot) + "\n" string += "current_bet:" + str(self.current_bet) + "\n" string += "self_bet:" + str(self.self_bet) + "\n" string += "actions:" + ("None" if self.get_actions() == None else str( [action_to_str[action] for action in self.get_actions()])) + "\n" return string
def _write_show_down(self): self.history.append("*** SHOW DOWN ***") hand_types = [self.evaluator.class_to_string(self.evaluator.get_rank_class(p.hand_rank)) for p in self.players if p.state is PlayerState.ACTIVE] for player in self.players: if player.state is PlayerState.ACTIVE: player.calculate_hand_rank(self.evaluator, self.cards) player_hand_type = self.evaluator.class_to_string(self.evaluator.get_rank_class(player.hand_rank)) matches = len([m for m in hand_types if m is player_hand_type]) multiple = matches > 1 self.history.append("%s: shows [%s %s] (%s)" % (player.name, Card.int_to_str(player.cards[0]), Card.int_to_str(player.cards[1]), pretty_print_hand(player.cards, player_hand_type, self.cards, multiple)) )
def GenerateTurnAndRivers( myDeck, StringOutput = False): # Figure out how many combos: # = remaining cards Choose 2 without replace. CombosInt = list( itertools.combinations( myDeck._FULL_DECK, 2 ) ) # not sure if it should be .cards i think it should be ! i hope no error... CombosStr = [] Combos = [] if StringOutput == True: for i in range(CombosInt.__len__()): Card1 = Card.int_to_str(CombosInt[i][0]) Card2 = Card.int_to_str(CombosInt[i][1]) CombosStr.append([Card1,Card2]) Combos = CombosStr else: Combos = CombosInt return Combos
def index(): evaluator = Evaluator() deck = Deck() card = Card.new('Qh') board = deck.draw(5) player_names = ("player 1", "player 2", "player 3", "player 4", "player 5", "player 6", "player 7", "player 8") players = {} output = {} # this is procedural programming, not functional programming :( for p in player_names: hand = deck.draw(2) score = evaluator.evaluate(board, hand) text = evaluator.class_to_string(evaluator.get_rank_class(score)) players[p] = score output[p] = {'score': score, 'text': text} # What about a tie? tie = (len(players.values()) == len(set(players.values()))) winner = min( players, key=players.get) # always 1 result :( Don't forget to fix the TEST! # does the tie involve the winning hand though? # TODO https://stackoverflow.com/questions/17821079/how-to-check-if-two-keys-in-dictionary-hold-the-same-value output["winners"] = winner output["tie"] = tie output["card"] = Card.int_to_str(card) j = json.dumps(output) return j
def __construct_preflop_key(self, card_strs): cards = sorted(Card.hand_to_binary(card_strs), reverse=True) #print Card.print_pretty_cards(cards) key = ''.join([Card.int_to_str(card)[0] for card in cards]) if Card.get_suit_int(cards[0]) == Card.get_suit_int(cards[1]): key = key + "s" else: key = key + "o" return key
def _pick_unused_card(self, used_card): used = [ self.CardIDmapping[Card.int_to_str(card)] for card in used_card ] unused = [ card_id for card_id in self.TotalCards.keys() if card_id not in used ] return [Card.new(self.TotalCards[card_id]) for card_id in unused]
def ew_parse(self, card_list, is_num=True): list_trey_to_st = [] if (is_num): for card in card_list: list_trey_to_st.append(Card.int_to_str(card)) list_st_to_ppe = [] for card_st in list_trey_to_st: list_st_to_ppe.append(card_st[1].upper() + card_st[0]) return list_st_to_ppe
def hand_to_str(hand, mode): output = " " for i in range(len(hand)): c = hand[i] if c == -1: if i != len(hand) - 1: output += '[ ],' else: output += '[ ] ' continue if i != len(hand) - 1: if mode == "machine": output += '[' + str(Card.int_to_str(c)) + '],' else: output += str(Card.int_to_pretty_str(c)) + ',' else: if mode == "machine": output += '[' + str(Card.int_to_str(c)) + '] ' else: output += str(Card.int_to_pretty_str(c)) + ' ' return output
def setUp(self): """Serializer data matches the Company object for each field.""" self.guest = Player_Factory.create() self.learner = Player_Factory.create() self.game = Game_Factory.create(players=( self.guest, self.learner)) # object instance being created in query set form self.game_serializer = GameSerializer( self.game) # object being serialized into json for field_name in ['id', 'total_pot']: self.assertEqual(game_serializer.data[field_name], getattr(game, field_name)) deck = Deck() for i in range(2): self.guest.card_player_set.create( card_str=Card.int_to_str(deck.draw(1))) self.learner.card_player_set.create( card_str=Card.int_to_str(deck.draw(1))) for i in range(5): self.game.card_community_set.create( card_str=Card.int_to_str(deck.draw(1)))
def _write_hole_cards(self): self.history.append("*** HOLE CARDS ***") for i, player in enumerate(self.players): self.history.append("Dealt to %s [%s %s]" % (player.name, Card.int_to_str(player.cards[0]), Card.int_to_str(player.cards[1])))
def card_to_normal_str(card): " {'23456789TJQKA'} + {'shdc''} (note: lower case) " if card == -1: return '' return Card.int_to_str(card)
def run(rounds): reward_table = dict() reward_count = dict() round_number = 0 print("running heuristic:") print_progress(round_number, rounds, prefix='Progress:', suffix='Complete', bar_length=40) while round_number < rounds: deck = Deck() Player_1 = Player() Player_2 = Player() flop = deck.draw(3) #print("Flop Cards") # for cards in flop: #print(Card.int_to_pretty_str(cards)) player1_hand = deck.draw(2) #print("Player 1 Cards") # for cards in player1_hand: #print(Card.int_to_pretty_str(cards)) player2_hand = deck.draw(2) #print("Player 2 Cards") # for cards in player2_hand: #print(Card.int_to_pretty_str(cards)) evaluator = Evaluator() all_card_array_stage3 = np.zeros((4, 13)) all_card_array_stage1 = np.zeros((4, 13)) all_card_array_stage2 = np.zeros((4, 13)) def convert_pot_to_numpy(total_pot): pot_array = np.zeros((4, 13)) number_of_chips = int(total_pot / 25) if number_of_chips > 13: pot_array[1] = 1 left_over_chips = number_of_chips - 13 for i in range(0, left_over_chips): pot_array[2][i] = 1 else: for i in range(0, number_of_chips): pot_array[1][i] = 1 return pot_array def convert_to_numpy_array(Card_str, all_card_array): if Card_str[0] == "J": index_1 = 9 elif Card_str[0] == "Q": index_1 = 10 elif Card_str[0] == "K": index_1 = 11 elif Card_str[0] == "A": index_1 = 12 elif Card_str[0] == "T": index_1 = 8 else: index_1 = int(Card_str[0]) - 2 if Card_str[1] == "s": index_2 = 0 elif Card_str[1] == "c": index_2 = 1 elif Card_str[1] == "h": index_2 = 2 else: index_2 = 3 new_card_array = np.zeros((4, 13)) new_card_array[index_2][index_1] = 1 all_card_array[index_2][index_1] = 1 return new_card_array, all_card_array def get_possible_actions(player_action): if player_action == "Check/Call": return ["Fold", "Bet"] elif player_action == "Bet": return ["Fold", "Check/Call"] else: return ["Fold", "Bet", "Check/Call"] def betting(player1_turn_rank, player2_turn_rank, all_possible_actions): action_1, player1_new_bet = Player_1.make_bets( player1_turn_rank, all_possible_actions, 0) #print("Player 1",action_1) if action_1 == "Fold": return "Player2", 0, 0, 0, action_1 elif action_1 == "Bet": possible_actions = get_possible_actions(action_1) action_2, player2_new_bet = Player_2.make_bets( player2_turn_rank, possible_actions, player1_new_bet) #print("Player 2", action_2) if action_2 == "Fold": return "Player1", player1_new_bet, 0, player1_new_bet, action_1 else: return "", player1_new_bet, player1_new_bet, 2 * player1_new_bet, action_1 else: action_2, player2_new_bet = Player_2.make_bets( player2_turn_rank, all_possible_actions, 0) #print("Player 2", action_2) if action_2 == "Fold": return "Player1", 0, 0, 0, action_1 elif action_2 == "Check/Call": return "", 0, 0, 0, action_1 else: possible_actions = get_possible_actions(action_2) action_1, player1_new_bet = Player_1.make_bets( player1_turn_rank, possible_actions, player2_new_bet) #print("Player 1", action_1) if action_1 == "Fold": return "Player2", 0, player2_new_bet, player2_new_bet, action_1 else: return "", player2_new_bet, player2_new_bet, 2 * player2_new_bet, action_1 total_pot_size = 0 player1_bet = 0 player2_bet = 0 player1_turn1 = flop + player1_hand player2_turn1 = flop + player2_hand player1_turn1_rank = evaluator.class_to_string( evaluator.get_rank_class(evaluator._five(player1_turn1))) player2_turn1_rank = evaluator.class_to_string( evaluator.get_rank_class(evaluator._five(player2_turn1))) all_possible_action = ["Fold", "Bet", "Check/Call"] winner, player1_round1_bet, player2_round1_bet, round1_pot_size, player1_action_stage1 = betting( player1_turn1_rank, player2_turn1_rank, all_possible_action) player1_bet += player1_round1_bet player2_bet += player2_round1_bet total_pot_size += round1_pot_size Card1_stage1_array, all_card_array_stage1 = convert_to_numpy_array( Card.int_to_str(flop[0]), all_card_array_stage1) Card2_stage1_array, all_card_array_stage1 = convert_to_numpy_array( Card.int_to_str(flop[1]), all_card_array_stage1) Card3_stage1_array, all_card_array_stage1 = convert_to_numpy_array( Card.int_to_str(flop[2]), all_card_array_stage1) Card4_stage1_array = np.zeros((4, 13)) Card5_stage1_array = np.zeros((4, 13)) Card6_stage1_array, all_card_array_stage1 = convert_to_numpy_array( Card.int_to_str(player1_hand[0]), all_card_array_stage1) Card7_stage1_array, all_card_array_stage1 = convert_to_numpy_array( Card.int_to_str(player1_hand[1]), all_card_array_stage1) pot_array_stage1 = convert_pot_to_numpy(total_pot_size) state_array_stage1 = np.stack( (Card1_stage1_array, Card2_stage1_array, Card3_stage1_array, Card4_stage1_array, Card5_stage1_array, Card6_stage1_array, Card7_stage1_array, all_card_array_stage1, pot_array_stage1)) hash_key_stage1 = pickle.dumps(state_array_stage1) gain_loss_table_stage1 = np.zeros((3)) gain_loss_count_stage1 = np.zeros((3)) if winner == "Player1": #print(player1_action_stage1) if player1_action_stage1 == "Bet": if hash_key_stage1 in reward_table.keys(): reward_table[hash_key_stage1][2] += int(total_pot_size - player1_bet) reward_count[hash_key_stage1][2] += 1 else: gain_loss_count_stage1[2] += 1 gain_loss_table_stage1[2] += int(total_pot_size - player1_bet) elif player1_action_stage1 == "Check/Call": if hash_key_stage1 in reward_table.keys(): reward_table[hash_key_stage1][1] += int(total_pot_size - player1_bet) reward_count[hash_key_stage1][1] += 1 else: gain_loss_count_stage1[1] += 1 gain_loss_table_stage1[1] += int(total_pot_size - player1_bet) #print("Player 1 Wins: ",total_pot_size) #print("Player 1 Gain: ", total_pot_size-player1_bet) elif winner == "Player2": if hash_key_stage1 in reward_table.keys(): if player1_action_stage1 == "Fold": reward_table[hash_key_stage1][0] += -1 * int(player1_bet) reward_count[hash_key_stage1][0] += 1 else: if player1_action_stage1 == "Fold": gain_loss_table_stage1[0] = -1 * int(player1_bet) gain_loss_count_stage1[0] += 1 #print("Player 2 Wins: ",total_pot_size) #print("Player 2 Gain: ", total_pot_size-player2_bet) else: turn = deck.draw(1) #print("Turn ",Card.int_to_pretty_str(turn)) player1_turn2 = player1_turn1 + [turn] player2_turn2 = player2_turn1 + [turn] player1_turn2_rank = evaluator.class_to_string( evaluator.get_rank_class(evaluator._six(player1_turn2))) player2_turn2_rank = evaluator.class_to_string( evaluator.get_rank_class(evaluator._six(player2_turn2))) winner, player1_round2_bet, player2_round2_bet, round2_pot_size, player1_action_stage2 = betting( player1_turn2_rank, player2_turn2_rank, all_possible_action) player1_bet += player1_round2_bet player2_bet += player2_round2_bet total_pot_size += round2_pot_size Card1_stage2_array, all_card_array_stage2 = convert_to_numpy_array( Card.int_to_str(flop[0]), all_card_array_stage2) Card2_stage2_array, all_card_array_stage2 = convert_to_numpy_array( Card.int_to_str(flop[1]), all_card_array_stage2) Card3_stage2_array, all_card_array_stage2 = convert_to_numpy_array( Card.int_to_str(flop[2]), all_card_array_stage2) Card4_stage2_array, all_card_array_stage2 = convert_to_numpy_array( Card.int_to_str(turn), all_card_array_stage2) Card5_stage2_array = np.zeros((4, 13)) Card6_stage2_array, all_card_array_stage2 = convert_to_numpy_array( Card.int_to_str(player1_hand[0]), all_card_array_stage2) Card7_stage2_array, all_card_array_stage2 = convert_to_numpy_array( Card.int_to_str(player1_hand[1]), all_card_array_stage2) pot_array_stage2 = convert_pot_to_numpy(total_pot_size) state_array_stage2 = np.stack( (Card1_stage2_array, Card2_stage2_array, Card3_stage2_array, Card4_stage2_array, Card5_stage2_array, Card6_stage2_array, Card7_stage2_array, all_card_array_stage2, pot_array_stage2)) hash_key_stage2 = pickle.dumps(state_array_stage2) gain_loss_table_stage2 = np.zeros((3)) gain_loss_count_stage2 = np.zeros((3)) if winner == "Player1": #print(player1_action_stage2) if player1_action_stage2 == "Bet": if hash_key_stage2 in reward_table.keys(): reward_table[hash_key_stage2][2] += int( total_pot_size - player1_bet) reward_count[hash_key_stage2][2] += 1 else: gain_loss_count_stage2[2] += 1 gain_loss_table_stage2[2] += int(total_pot_size - player1_bet) elif player1_action_stage2 == "Check/Call": if hash_key_stage2 in reward_table.keys(): reward_table[hash_key_stage2][1] += int( total_pot_size - player1_bet) reward_count[hash_key_stage2][1] += 1 else: gain_loss_count_stage2[1] += 1 gain_loss_table_stage2[1] += int(total_pot_size - player1_bet) if player1_action_stage1 == "Bet": if hash_key_stage1 in reward_table.keys(): reward_table[hash_key_stage1][2] += int( total_pot_size - player1_bet) reward_count[hash_key_stage1][2] += 1 else: gain_loss_count_stage1[2] += 1 gain_loss_table_stage1[2] += int(total_pot_size - player1_bet) elif player1_action_stage1 == "Check/Call": if hash_key_stage1 in reward_table.keys(): reward_table[hash_key_stage1][1] += int( total_pot_size - player1_bet) reward_count[hash_key_stage1][1] += 1 else: gain_loss_count_stage1[1] += 1 gain_loss_table_stage1[1] += int(total_pot_size - player1_bet) #print("Player 1 Wins: ",total_pot_size) #print("Player 1 Gain: ",total_pot_size-player1_bet) elif winner == "Player2": if hash_key_stage2 in reward_table.keys(): if player1_action_stage2 == "Fold": reward_table[hash_key_stage2][0] += -1 * int( player1_bet) reward_count[hash_key_stage2][0] += 1 else: if player1_action_stage2 == "Fold": gain_loss_table_stage2[0] = -1 * int(player1_bet) gain_loss_count_stage2[0] += 1 if player1_action_stage1 == "Bet": if hash_key_stage1 in reward_table.keys(): reward_table[hash_key_stage1][2] += int( total_pot_size - player1_bet) reward_count[hash_key_stage1][2] += 1 else: gain_loss_count_stage1[2] += 1 gain_loss_table_stage1[2] += int(total_pot_size - player1_bet) elif player1_action_stage1 == "Check/Call": if hash_key_stage1 in reward_table.keys(): reward_table[hash_key_stage1][1] += -1 * int( player1_bet) reward_count[hash_key_stage1][1] += 1 else: gain_loss_count_stage1[1] += 1 gain_loss_table_stage1[1] += -1 * int(player1_bet) #print("Player 2 Wins: ",total_pot_size) #print("Player 2 Gain: ",total_pot_size-player2_bet) else: river = deck.draw(1) #print("River ",Card.int_to_pretty_str(river)) player1_turn3 = player1_turn2 + [river] player2_turn3 = player2_turn2 + [river] #Creating state array Card1 = Card.int_to_str(flop[0]) Card2 = Card.int_to_str(flop[1]) Card3 = Card.int_to_str(flop[2]) Card4 = Card.int_to_str(turn) Card5 = Card.int_to_str(river) Card6 = Card.int_to_str(player1_hand[0]) Card7 = Card.int_to_str(player1_hand[1]) Card1_stage3_array, all_card_array_stage3 = convert_to_numpy_array( Card1, all_card_array_stage3) Card2_stage3_array, all_card_array_stage3 = convert_to_numpy_array( Card2, all_card_array_stage3) Card3_stage3_array, all_card_array_stage3 = convert_to_numpy_array( Card3, all_card_array_stage3) Card4_stage3_array, all_card_array_stage3 = convert_to_numpy_array( Card4, all_card_array_stage3) Card5_stage3_array, all_card_array_stage3 = convert_to_numpy_array( Card5, all_card_array_stage3) Card6_stage3_array, all_card_array_stage3 = convert_to_numpy_array( Card6, all_card_array_stage3) Card7_stage3_array, all_card_array_stage3 = convert_to_numpy_array( Card7, all_card_array_stage3) player1_turn3_rank = evaluator.class_to_string( evaluator.get_rank_class(evaluator._seven(player1_turn3))) player2_turn3_rank = evaluator.class_to_string( evaluator.get_rank_class(evaluator._seven(player2_turn3))) winner, player1_round3_bet, player2_round3_bet, round3_pot_size, player1_action = betting( player1_turn3_rank, player2_turn3_rank, all_possible_action) player1_bet += player1_round3_bet player2_bet += player2_round3_bet total_pot_size += round3_pot_size total_pot_array = convert_pot_to_numpy(total_pot_size) state_array_stage3 = np.stack( (Card1_stage3_array, Card2_stage3_array, Card3_stage3_array, Card4_stage3_array, Card5_stage3_array, Card6_stage3_array, Card7_stage3_array, all_card_array_stage3, total_pot_array)) # #print(state_array) hash_key = pickle.dumps(state_array_stage3) gain_loss_table_stage3 = np.zeros((3)) gain_loss_count_stage3 = np.zeros((3)) if winner == "Player1": #print(player1_action) if hash_key in reward_table.keys(): if player1_action == "Check/Call": reward_table[hash_key][1] += int(total_pot_size - player1_bet) reward_count[hash_key][1] += 1 elif player1_action == "Bet": reward_table[hash_key][2] += int(total_pot_size - player1_bet) reward_count[hash_key][2] += 1 else: if player1_action == "Check/Call": gain_loss_table_stage3[1] = int(total_pot_size - player1_bet) gain_loss_count_stage3[1] += 1 elif player1_action == "Bet": gain_loss_table_stage3[2] = int(total_pot_size - player1_bet) gain_loss_count_stage3[2] += 1 if player1_action_stage2 == "Bet": if hash_key_stage2 in reward_table.keys(): reward_table[hash_key_stage2][2] += int( total_pot_size - player1_bet) reward_count[hash_key_stage2][2] += 1 else: gain_loss_count_stage2[2] += 1 gain_loss_table_stage2[2] += int(total_pot_size - player1_bet) elif player1_action_stage2 == "Check/Call": if hash_key_stage2 in reward_table.keys(): reward_table[hash_key_stage2][1] += int( total_pot_size - player1_bet) reward_count[hash_key_stage2][1] += 1 else: gain_loss_count_stage2[1] += 1 gain_loss_table_stage2[1] += int(total_pot_size - player1_bet) if player1_action_stage1 == "Bet": if hash_key_stage1 in reward_table.keys(): reward_table[hash_key_stage1][2] += int( total_pot_size - player1_bet) reward_count[hash_key_stage1][2] += 1 else: gain_loss_count_stage1[2] += 1 gain_loss_table_stage1[2] += int(total_pot_size - player1_bet) elif player1_action_stage1 == "Check/Call": if hash_key_stage1 in reward_table.keys(): reward_table[hash_key_stage1][1] += int( total_pot_size - player1_bet) reward_count[hash_key_stage1][1] += 1 else: gain_loss_count_stage1[1] += 1 gain_loss_table_stage1[1] += int(total_pot_size - player1_bet) #print("Player 1 Wins: ",total_pot_size) #print("Player 1 Gain: ",total_pot_size-player1_bet) elif winner == "Player2": if hash_key in reward_table.keys(): if player1_action == "Fold": reward_table[hash_key][0] += -1 * int(player1_bet) reward_count[hash_key][0] += 1 else: if player1_action == "Fold": gain_loss_table_stage3[0] = -1 * int(player1_bet) gain_loss_count_stage3[0] += 1 if player1_action_stage2 == "Bet": if hash_key_stage2 in reward_table.keys(): reward_table[hash_key_stage2][2] += -1 * int( player1_bet) reward_count[hash_key_stage2][2] += 1 else: gain_loss_count_stage2[2] += 1 gain_loss_table_stage2[2] += -1 * int(player1_bet) elif player1_action_stage2 == "Check/Call": if hash_key_stage2 in reward_table.keys(): reward_table[hash_key_stage2][1] += -1 * int( player1_bet) reward_count[hash_key_stage2][1] += 1 else: gain_loss_count_stage2[1] += 1 gain_loss_table_stage2[1] += -1 * int(player1_bet) if player1_action_stage1 == "Bet": if hash_key_stage1 in reward_table.keys(): reward_table[hash_key_stage1][2] += -1 * int( player1_bet) reward_count[hash_key_stage1][2] += 1 else: gain_loss_count_stage1[2] += 1 gain_loss_table_stage1[2] += -1 * int(player1_bet) elif player1_action_stage1 == "Check/Call": if hash_key_stage1 in reward_table.keys(): reward_table[hash_key_stage1][1] += -1 * int( player1_bet) reward_count[hash_key_stage1][1] += 1 else: gain_loss_count_stage1[1] += 1 gain_loss_table_stage1[1] += -1 * int(player1_bet) #print("Player 2 Wins: ",total_pot_size) #print("Player 2 Gain: ",total_pot_size-player2_bet) else: final_score_player1 = evaluator.get_rank_class( evaluator._seven(player1_turn3)) final_score_player2 = evaluator.get_rank_class( evaluator._seven(player2_turn3)) if final_score_player1 < final_score_player2: #print(player1_action) if hash_key in reward_table.keys(): if player1_action == "Check/Call": reward_table[hash_key][1] += int( total_pot_size - player1_bet) reward_count[hash_key][1] += 1 elif player1_action == "Bet": reward_table[hash_key][2] += int( total_pot_size - player1_bet) reward_count[hash_key][2] += 1 else: if player1_action == "Check/Call": gain_loss_table_stage3[1] = int( total_pot_size - player1_bet) gain_loss_count_stage3[1] += 1 elif player1_action == "Bet": gain_loss_table_stage3[2] = int( total_pot_size - player1_bet) gain_loss_count_stage3[2] += 1 if player1_action_stage2 == "Bet": if hash_key_stage2 in reward_table.keys(): reward_table[hash_key_stage2][2] += int( total_pot_size - player1_bet) reward_count[hash_key_stage2][2] += 1 else: gain_loss_count_stage2[2] += 1 gain_loss_table_stage2[2] += int( total_pot_size - player1_bet) elif player1_action_stage2 == "Check/Call": if hash_key_stage2 in reward_table.keys(): reward_table[hash_key_stage2][1] += int( total_pot_size - player1_bet) reward_count[hash_key_stage2][1] += 1 else: gain_loss_count_stage2[1] += 1 gain_loss_table_stage2[1] += int( total_pot_size - player1_bet) if player1_action_stage1 == "Bet": if hash_key_stage1 in reward_table.keys(): reward_table[hash_key_stage1][2] += int( total_pot_size - player1_bet) reward_count[hash_key_stage1][2] += 1 else: gain_loss_count_stage1[2] += 1 gain_loss_table_stage1[2] += int( total_pot_size - player1_bet) elif player1_action_stage1 == "Check/Call": if hash_key_stage1 in reward_table.keys(): reward_table[hash_key_stage1][1] += int( total_pot_size - player1_bet) reward_count[hash_key_stage1][1] += 1 else: gain_loss_count_stage1[1] += 1 gain_loss_table_stage1[1] += int( total_pot_size - player1_bet) #print("Player 1 Wins: ",total_pot_size) #print("Player 1 Gain: ",total_pot_size-player1_bet) else: if hash_key in reward_table.keys(): if player1_action == "Check/Call": reward_table[hash_key][1] += -1 * int( player1_bet) reward_count[hash_key][1] += 1 elif player1_action == "Bet": reward_table[hash_key][2] += -1 * int( player1_bet) reward_count[hash_key][2] += 1 else: if player1_action == "Check/Call": gain_loss_table_stage3[1] = -1 * int( player1_bet) gain_loss_count_stage3[1] += 1 elif player1_action == "Bet": gain_loss_table_stage3[2] = -1 * int( player1_bet) gain_loss_count_stage3[2] += 1 if player1_action_stage2 == "Bet": if hash_key_stage2 in reward_table.keys(): reward_table[hash_key_stage2][2] += -1 * int( player1_bet) reward_count[hash_key_stage2][2] += 1 else: gain_loss_count_stage2[2] += 1 gain_loss_table_stage2[2] += -1 * int( player1_bet) elif player1_action_stage2 == "Check/Call": if hash_key_stage2 in reward_table.keys(): reward_table[hash_key_stage2][1] += -1 * int( player1_bet) reward_count[hash_key_stage2][1] += 1 else: gain_loss_count_stage2[1] += 1 gain_loss_table_stage2[1] += -1 * int( player1_bet) if player1_action_stage1 == "Bet": if hash_key_stage1 in reward_table.keys(): reward_table[hash_key_stage1][2] += -1 * int( player1_bet) reward_count[hash_key_stage1][2] += 1 else: gain_loss_count_stage1[2] += 1 gain_loss_table_stage1[2] += -1 * int( player1_bet) elif player1_action_stage1 == "Check/Call": if hash_key_stage1 in reward_table.keys(): reward_table[hash_key_stage1][1] += -1 * int( player1_bet) reward_count[hash_key_stage1][1] += 1 else: gain_loss_count_stage1[1] += 1 gain_loss_table_stage1[1] += -1 * int( player1_bet) #print("Player 2 Wins: ",total_pot_size) #print("Player 2 Gain: ",total_pot_size-player2_bet) if hash_key not in reward_table.keys(): reward_table[hash_key] = gain_loss_table_stage3 reward_count[hash_key] = gain_loss_count_stage3 if hash_key_stage2 not in reward_table.keys(): reward_table[hash_key_stage2] = gain_loss_table_stage2 reward_count[hash_key_stage2] = gain_loss_count_stage2 if hash_key_stage1 not in reward_table.keys(): reward_table[hash_key_stage1] = gain_loss_table_stage1 reward_count[hash_key_stage1] = gain_loss_count_stage1 round_number += 1 print_progress(round_number, rounds, prefix='Progress:', suffix='Complete', bar_length=40) for key in reward_table.keys(): count_fold = reward_count[key][0] count_check = reward_count[key][1] count_bet = reward_count[key][2] if count_fold != 0: reward_table[key][0] = reward_table[key][0] / count_fold if count_check != 0: reward_table[key][1] = reward_table[key][1] / count_check if count_bet != 0: reward_table[key][2] = reward_table[key][2] / count_bet return reward_table
def _finish_hand(self): for player in self.players: player.step(None, None, True) if self.hand_history_enabled: if player.winnings > 0: player.winnings = np.round(player.winnings, 2) self._write_event("%s collected $%.2f from pot" % (player.name, player.winnings*BB)) self._write_event("*** SUMMARY ***") self._write_event("Total pot $%.2f | Rake $%.2f" % (self.pot*BB, 0)) if self.street == GameState.FLOP: self._write_event("Board [%s %s %s]" % (Card.int_to_str(self.cards[0]), Card.int_to_str(self.cards[1]), Card.int_to_str(self.cards[2])) ) elif self.street == GameState.TURN: self._write_event("Board [%s %s %s %s]" % (Card.int_to_str(self.cards[0]), Card.int_to_str(self.cards[1]), Card.int_to_str(self.cards[2]), Card.int_to_str(self.cards[3])) ) elif self.street == GameState.RIVER: self._write_event("Board [%s %s %s %s %s]" % (Card.int_to_str(self.cards[0]), Card.int_to_str(self.cards[1]), Card.int_to_str(self.cards[2]), Card.int_to_str(self.cards[3]), Card.int_to_str(self.cards[4])) ) if self.hand_history_enabled and self.hand_history_location is not None: with open('%s/handhistory_%s.txt' % (self.hand_history_location,time.time()), 'w') as f: for row in self.history: f.writelines(row + '\n')
def get_card_class(card_int_list): res = [ Card.new(Card.int_to_str(c)) for c in card_int_list if c != -1 ] return res
def play_hand(self): if self.hand_history_enabled: self._history_initialize() blinds_collected = False hand_is_over = False for street in GameState: street_finished = False last_bet_placed_by = None self.street = street self.bet_to_match = 0 self.minimum_raise = 0 for player in self.players: player.finish_street() if street == GameState.FLOP: self.cards = self.deck.draw(3) self._write_event("*** FLOP *** [%s %s %s]" % (Card.int_to_str(self.cards[0]), Card.int_to_str(self.cards[1]), Card.int_to_str(self.cards[2]))) if street == GameState.TURN: new = self.deck.draw(1) self.cards.append(new) self._write_event("*** TURN *** [%s %s %s] [%s]" % (Card.int_to_str(self.cards[0]), Card.int_to_str(self.cards[1]), Card.int_to_str(self.cards[2]), Card.int_to_str(self.cards[3]))) if street == GameState.RIVER: new = self.deck.draw(1) self.cards.append(new) self._write_event("*** RIVER *** [%s %s %s %s] [%s]" % (Card.int_to_str(self.cards[0]), Card.int_to_str(self.cards[1]), Card.int_to_str(self.cards[2]), Card.int_to_str(self.cards[3]), Card.int_to_str(self.cards[4]))) while not street_finished and not hand_is_over: for player in self.players: if player.all_in or player.state is not PlayerState.ACTIVE: continue if street == GameState.PREFLOP and not blinds_collected: if player.position == TablePosition.SB: self.pot += player.bet(0.5) self._change_bet_to_match(0.5) self._write_event("%s: posts small blind $%.2f" % (player.name, SB)) continue elif player.position == TablePosition.BB: self.pot += player.bet(1) self._change_bet_to_match(1) last_bet_placed_by = player blinds_collected = True self._write_event("%s: posts big blind $%.2f" % (player.name, BB)) if self.hand_history_enabled: self._write_hole_cards() continue have_actions = [p for p in self.players if p.state is PlayerState.ACTIVE if not p.all_in] if len(have_actions) < 2: amount = 0 # Everone else is all in or folded if self.active_players > 1: street_finished = True biggest_match = max([p.bet_this_street for p in self.players if p.state is PlayerState.ACTIVE if p is not player]) if biggest_match < player.bet_this_street: amount = player.bet_this_street - biggest_match # Everone else has folded else: hand_is_over = True amount = self.minimum_raise if amount > 0: self.pot -= amount player.stack += amount player.money_in_pot -= amount player.bet_this_street -= amount self._write_event( "Uncalled bet ($%.2f) returned to %s" % (amount * BB, player.name) ) break # If no one has raised after a bet, the betting round is over if last_bet_placed_by is player: street_finished = True break observation = self._get_observation(player) valid_actions = self._get_valid_actions(player) action = player.step(observation, valid_actions) # If action is not valid, a valid action (check or fold) is automatically taken in _is_action_valid if not self._is_action_valid(player, action, valid_actions): player.punish_invalid_action() continue if action.action_type is PlayerAction.FOLD: player.fold() self.active_players -= 1 self._write_event("%s: folds" % player.name) elif action.action_type is PlayerAction.CHECK: player.check() self._write_event("%s: checks" % player.name) elif action.action_type is PlayerAction.CALL: call_size = player.call(self.bet_to_match) self.pot += call_size if player.all_in: self._write_event("%s: calls $%.2f and is all-in" % (player.name, call_size*BB)) else: self._write_event("%s: calls $%.2f" % (player.name, call_size*BB)) elif action.action_type is PlayerAction.BET: previous_bet_this_street = player.bet_this_street actual_bet_size = player.bet(np.round(action.bet_amount, 2)) self.pot += actual_bet_size if self.bet_to_match == 0: if player.all_in: self._write_event("%s: bets $%.2f and is all-in" % (player.name, actual_bet_size*BB)) else: self._write_event("%s: bets $%.2f" % (player.name, actual_bet_size*BB)) else: if player.all_in: self._write_event("%s: raises $%.2f to $%.2f and is all-in" % (player.name, ((actual_bet_size+previous_bet_this_street)-self.bet_to_match)*BB, (actual_bet_size+previous_bet_this_street)*BB) ) else: self._write_event("%s: raises $%.2f to $%.2f" % (player.name, ((actual_bet_size+previous_bet_this_street)-self.bet_to_match)*BB, (actual_bet_size+previous_bet_this_street)*BB) ) self._change_bet_to_match(actual_bet_size+previous_bet_this_street) last_bet_placed_by = player else: raise Exception('Invalid action specified') if last_bet_placed_by is None: break if hand_is_over: break if not hand_is_over: if self.hand_history_enabled: self._write_show_down() self._distribute_pot() self._finish_hand()
print(Card.int_to_pretty_str(card)) turn = deck.draw(1) print("Turn ", Card.int_to_pretty_str(turn)) river = deck.draw(1) print("River ", Card.int_to_pretty_str(river)) evaluator = Evaluator() player1_complete_hand = flop + [turn] + [river] + player1_hand player2_complete_hand = flop + [turn] + [river] + player2_hand player1_bet = 0 player2_bet = 0 total_pot_size = 0 all_card_array_player1 = np.zeros((4, 13)) all_card_array_player2 = np.zeros((4, 13)) flop1_array, all_card_array_player1, all_card_array_player2 = convert_to_numpy_array( Card.int_to_str(flop[0]), all_card_array_player1, all_card_array_player2) flop2_array, all_card_array_player1, all_card_array_player2 = convert_to_numpy_array( Card.int_to_str(flop[1]), all_card_array_player1, all_card_array_player2) flop3_array, all_card_array_player1, all_card_array_player2 = convert_to_numpy_array( Card.int_to_str(flop[2]), all_card_array_player1, all_card_array_player2) turn_array = np.zeros((4, 13)) river_array = np.zeros((4, 13)) Card6_array_player1, all_card_array_player1 = convert_to_numpy_array_playercards( Card.int_to_str(player1_hand[0]), all_card_array_player1) Card7_array_player1, all_card_array_player1 = convert_to_numpy_array_playercards( Card.int_to_str(player1_hand[1]), all_card_array_player1) Card6_array_player2, all_card_array_player2 = convert_to_numpy_array_playercards( Card.int_to_str(player2_hand[0]), all_card_array_player2) Card7_array_player2, all_card_array_player2 = convert_to_numpy_array_playercards( Card.int_to_str(player2_hand[1]), all_card_array_player2)
def conv_card(card): rank = card // 4 suit = 1 << (card % 4) return Card.new(Card.int_to_str((rank << 8) + (suit << 12)))
def EquityVsRange(vilRange, heroHand, flop): handEval = Evaluator() WIN = 0 LOOSE = 0 # vilRange will come as defined above, heroHand as writen in input for now, flop same. ############## Factorize func toRemove = [] #toRemoveString = [] ### its already in some kind of string format....Pass toRemove, Create 'Qs' type string, before creating hand, check its plausible. for i in range(flop.__len__()): cardTempstr = Card.int_to_str(flop[i]) cardRankTemp = cardTempstr[0] cardSuitTemp = cardTempstr[1] toRemove.append([cardRankTemp, cardSuitTemp]) for i in range(heroHand.__len__()): cardTempstr = Card.int_to_str(heroHand[i]) cardRankTemp = cardTempstr[0] cardSuitTemp = cardTempstr[1] toRemove.append([cardRankTemp, cardSuitTemp]) # we need to remove the vilains cards from the deck each time.....in fact this is a frikin issue. ############# Factorize func above myDeck = treys.deck2.Deck2() for HAND in range(vilRange.__len__()): # could probably more efficient without range and HAND = numeric hand val. (efficiency)(e1) vilHandTemp = vilRange[HAND] # I need to convert suited and non-suited hands into actual sets of cards.... ooor. loop around all possibilities. vilHandCombos = rangeSelectorValueToHandList( vilHandTemp, toRemove)#(e1) #print(vilHandCombos[0][0]) for COMBO in range(vilHandCombos.__len__()): # Villain Hand for now Default AcAs vilHand = [ Card.new( Card.int_to_str( vilHandCombos[COMBO][0] ) ), Card.new( Card.int_to_str( vilHandCombos[COMBO][1] ) ) ] # print('vilHand:') # print(vilHand) for V in range(vilHand.__len__()): cardTempstr = Card.int_to_str(vilHand[V]) cardRankTemp = cardTempstr[0] cardSuitTemp = cardTempstr[1] if V == 0: toRemoveTemp = toRemove.copy() # print('redefined toRemoveTemp :') # print('toRemove V=0') # print(toRemove) # print('toRemoveTemp V=0') # print(toRemoveTemp) # print('toRemoveTemp prior to append'+str(V) ) # print(toRemoveTemp) # print('Card to append') # print([cardRankTemp, cardSuitTemp]) toRemoveTemp.append([cardRankTemp, cardSuitTemp]) #print('toRemove after adding hands') #print(toRemoveTemp) myDeck.GetCustomDeck( *toRemoveTemp ) possibleTurnAndRiver = GenerateTurnAndRivers(myDeck, StringOutput = False) for TnR in range(possibleTurnAndRiver.__len__()): #print(TnR) #print(tuple(flop) ) #print( possibleTurnAndRiver[TnR] ) flopTemp = tuple(flop) + possibleTurnAndRiver[TnR] # print(type(vilHand)) # print(type(flopTemp)) # print('Inputs to handEval that is failing:') # print(vilHand) # print(heroHand) # print(list(flopTemp)) ResultsVil = handEval.evaluate( vilHand, list(flopTemp) ) ResultsHero = handEval.evaluate( heroHand, list(flopTemp) ) # print(ResultsVil) # print(ResultsHero) if ResultsHero < ResultsVil: WIN = WIN + 1 if ResultsHero > ResultsVil: LOOSE = LOOSE + 1 if ResultsHero == ResultsVil: LOOSE = LOOSE + 0.5 WIN = WIN + 0.5 # Calc Equity: # No tie Equity = win/win+loose # When people talk about the equity in their hand, they win they %win... #-> BOARD = A A A A K , both have 0 % to win # Upon tying, i could add 0.5 to each counter. This makes good sense. in the above example equity = 50% correct # intuitivly this seems correct to me, we could confirm this results vs something else. Equity = WIN / (WIN + LOOSE) Equity = round(Equity,3) return(Equity)
def get_card_class(self,card_int_list): res = [Card.int_to_str(c) for c in card_int_list if c != -1] return res
def card_to_str(self, card_list=[]): return [Card.int_to_str(card) for card in card_list if card != -1] """
def get(self, request): self.env = gym.make('TexasHoldem-v1') # holdem.TexasHoldemEnv(2) self.env.add_player( 0, stack=2000) # add a player to seat 0 with 2000 "chips" # env.add_player(1, stack=2000) # tight self.env.add_player(2, stack=2000) # aggressive # s.configure('call.TButton', foreground='green', bg='#c1bb78') # s.configure('raise.TButton', foreground='red', bg='#c1bb78') # s.configure('fold.TButton', foreground='blue', bg='#c1bb78') self.Q = defaultdict(lambda: np.zeros(self.env.action_space.n)) self.policy = make_epsilon_greedy_policy(self.Q, self.env.action_space.n, epsilon) self.guest_cards = [] self.learner_cards = [] self.state = None self.player_states, self.community_infos, self.community_cards = None, None, None self.player_infos, player_hands = None, None self.current_state = None self.state_set = None self.p1 = self.env._player_dict[0] # Learner self.p2 = self.env._player_dict[2] # Guest self.episode_list = [] self.total_pot_label = None self._round = None self.current_player = None self.guest_action = None self.call_button = None self.raise_button = None self.fold_button = None # self.guest_buttons = [call_button, raise_button, fold_button] self.terminal = False self.guest_label = None self.learner_label = None self.p1_pos = None self.p2_pos = None self.guest_cards_st, self.learner_cards_st = None, None self.episodes = [] self.last_bet_label = None self.community_display = [] self.is_new_game = False self.is_end_game = False self.mrp = None self.sd_tr = False self.last_learner_cards = None self.initial_action = True # part_init = None self.start() cc = [Card.int_to_str(card).upper() for card in self.community_cards] if any( [x + 1 for x in self.community_cards]) else ["", "", "", "", ""] cc_gen = self.yield_card_set(*cc) a, b, c, d, e = [next(cc_gen) for _ in cc] commu = poker_model.CommunityCards(flop_0=a, flop_1=b, flop_2=c, turn=d, river=e) commu.save() player_gen = self.yield_card_set(*(self.guest_cards_st + self.learner_cards_st)) guest_a, guest_b, learner_a, learner_b = [ next(player_gen) for _ in self.guest_cards_st + self.learner_cards_st ] guest_cards = poker_model.CardHolding(first=guest_a, second=guest_b) learner_cards = poker_model.CardHolding(first=learner_a, second=learner_b) guest_cards.save() learner_cards.save() game = poker_model.Game(total_pot=self.total_pot_label, guest_cards=guest_cards, learner_cards=learner_cards, community_cards=commu).save() return render(request, "poker/poker.html")
def run(rounds, model): reward_table = dict() reward_count = dict() print("running self-play") round_number = 0 print_progress(round_number, rounds, prefix='Progress:', suffix='Complete', bar_length=40) while round_number < rounds: deck = Deck() evaluator = Evaluator() flop = deck.draw(3) # for card in flop: # print(Card.int_to_pretty_str(card)) player1_hand = deck.draw(2) # for card in player1_hand: # print(Card.int_to_pretty_str(card)) player2_hand = deck.draw(3) turn = deck.draw(1) river = deck.draw(1) player1_complete_hand = flop + [turn] + [river] + player1_hand player2_complete_hand = flop + [turn] + [river] + player2_hand def get_action_position2(y_stage, other_player_action): if other_player_action == "Bet": out_of_bounds_index = 2 index = 0 max_gain_loss = y_stage[0] for i in range(1, len(y_stage)): if i != out_of_bounds_index: if max_gain_loss < y_stage[i]: index = i max_gain_loss = y_stage[i] if index == 0: return "Fold" elif index == 1: return "Check/Call" else: return "Bet" def get_action(y_stage): index = 0 max_gain_loss = y_stage[0][0] for i in range(1, len(y_stage[0])): if max_gain_loss < y_stage[0][i]: index = i max_gain_loss = y_stage[0][i] if index == 0: return "Fold" elif index == 1: return "Check/Call" else: return "Bet" def convert_pot_to_numpy(total_pot): pot_array = np.zeros((4, 13)) number_of_chips = int(total_pot / 25) if number_of_chips > 13: pot_array[1] = 1 left_over_chips = number_of_chips - 13 for i in range(0, left_over_chips): pot_array[2][i] = 1 else: for i in range(0, number_of_chips): pot_array[1][i] = 1 return pot_array def convert_to_numpy_array(Card_str, all_card_array_player1, all_card_array_player2): if Card_str[0] == "J": index_1 = 9 elif Card_str[0] == "Q": index_1 = 10 elif Card_str[0] == "K": index_1 = 11 elif Card_str[0] == "A": index_1 = 12 elif Card_str[0] == "T": index_1 = 8 else: index_1 = int(Card_str[0]) - 2 if Card_str[1] == "s": index_2 = 0 elif Card_str[1] == "c": index_2 = 1 elif Card_str[1] == "h": index_2 = 2 else: index_2 = 3 new_card_array = np.zeros((4, 13)) new_card_array[index_2][index_1] = 1 all_card_array_player1[index_2][index_1] = 1 all_card_array_player2[index_2][index_1] = 1 return new_card_array, all_card_array_player1, all_card_array_player2 def convert_to_numpy_array_playercards(Card_str, all_card_array): if Card_str[0] == "J": index_1 = 9 elif Card_str[0] == "Q": index_1 = 10 elif Card_str[0] == "K": index_1 = 11 elif Card_str[0] == "A": index_1 = 12 elif Card_str[0] == "T": index_1 = 8 else: index_1 = int(Card_str[0]) - 2 if Card_str[1] == "s": index_2 = 0 elif Card_str[1] == "c": index_2 = 1 elif Card_str[1] == "h": index_2 = 2 else: index_2 = 3 new_card_array = np.zeros((4, 13)) new_card_array[index_2][index_1] = 1 all_card_array[index_2][index_1] = 1 return new_card_array, all_card_array def betting(model1_player, model2_player, flop1_array, flop2_array, flop3_array, turn_array, river_array, Card1_array_player1, Card2_array_player1, Card1_array_player2, Card2_array_player2, all_card_array_player1, all_card_array_player2, stage_initial_potsize): pot_array_stage = convert_pot_to_numpy(stage_initial_potsize) state_array_player1 = np.stack( (flop1_array, flop2_array, flop3_array, turn_array, river_array, Card1_array_player1, Card2_array_player1, all_card_array_player1, pot_array_stage)) state_array_player1 = np.expand_dims(state_array_player1, 0) input_shape = [9, 17, 17] right_left_pad = input_shape[1] - state_array_player1.shape[2] left_pad = right_left_pad // 2 right_pad = left_pad + (right_left_pad % 2) top_bottom_pad = input_shape[2] - state_array_player1.shape[3] top_pad = top_bottom_pad // 2 bottom_pad = top_pad + (top_bottom_pad % 2) state_array_player1 = np.pad(state_array_player1, ((0, 0), (0, 0), (left_pad, right_pad), (top_pad, bottom_pad)), mode='constant') y_player1 = Model.predict(model1_player, x=state_array_player1) action_player1 = get_action(y_player1) if action_player1 == "Fold": return "Player 2", stage_initial_potsize, 0, 0, action_player1, "" elif action_player1 == "Check/Call": state_array_player2 = np.stack( (flop1_array, flop2_array, flop3_array, turn_array, river_array, Card1_array_player2, Card2_array_player2, all_card_array_player2, pot_array_stage)) state_array_player2 = np.expand_dims(state_array_player2, 0) right_left_pad = input_shape[1] - state_array_player2.shape[2] left_pad = right_left_pad // 2 right_pad = left_pad + (right_left_pad % 2) top_bottom_pad = input_shape[2] - state_array_player2.shape[3] top_pad = top_bottom_pad // 2 bottom_pad = top_pad + (top_bottom_pad % 2) state_array_player2 = np.pad(state_array_player2, ((0, 0), (0, 0), (left_pad, right_pad), (top_pad, bottom_pad)), mode='constant') y_player2 = Model.predict(model2_player, x=state_array_player2) action_player2 = get_action(y_player2) if action_player2 == "Fold": return "Player 1", stage_initial_potsize, 0, 0, action_player1, action_player2 elif action_player2 == "Check/Call": return "", stage_initial_potsize, 0, 0, action_player1, action_player2 else: stage_initial_potsize += 100 pot_array_stage = convert_pot_to_numpy( stage_initial_potsize) state_array_player1 = np.stack( (flop1_array, flop2_array, flop3_array, turn_array, river_array, Card1_array_player1, Card2_array_player1, all_card_array_player1, pot_array_stage)) state_array_player1 = np.expand_dims( state_array_player1, 0) right_left_pad = input_shape[ 1] - state_array_player1.shape[2] left_pad = right_left_pad // 2 right_pad = left_pad + (right_left_pad % 2) top_bottom_pad = input_shape[ 2] - state_array_player1.shape[3] top_pad = top_bottom_pad // 2 bottom_pad = top_pad + (top_bottom_pad % 2) state_array_player1 = np.pad(state_array_player1, ((0, 0), (0, 0), (left_pad, right_pad), (top_pad, bottom_pad)), mode='constant') y_player1 = Model.predict(model1_player, x=state_array_player1) action_player1 = get_action_position2(y_player1, "Bet") if action_player1 == "Fold": return "Player 2", stage_initial_potsize, 0, 100, action_player1, action_player2 else: return "", stage_initial_potsize + 100, 100, 100, action_player1, action_player2 else: stage_initial_potsize += 100 pot_array_stage = convert_pot_to_numpy(stage_initial_potsize) state_array_player2 = np.stack( (flop1_array, flop2_array, flop3_array, turn_array, river_array, Card1_array_player2, Card2_array_player2, all_card_array_player2, pot_array_stage)) state_array_player2 = np.expand_dims(state_array_player2, 0) right_left_pad = input_shape[1] - state_array_player2.shape[2] left_pad = right_left_pad // 2 right_pad = left_pad + (right_left_pad % 2) top_bottom_pad = input_shape[2] - state_array_player2.shape[3] top_pad = top_bottom_pad // 2 bottom_pad = top_pad + (top_bottom_pad % 2) state_array_player2 = np.pad(state_array_player2, ((0, 0), (0, 0), (left_pad, right_pad), (top_pad, bottom_pad)), mode='constant') y_player2 = Model.predict(model2_player, x=state_array_player2) # print(y_player2) action_player2 = get_action_position2(y_player2, "Bet") if action_player2 == "Fold": return "Player 1", stage_initial_potsize, 100, 0, action_player1, action_player2 else: return "", stage_initial_potsize + 100, 100, 100, action_player1, action_player2 model_player1 = model model_player2 = model player1_bet = 0 player2_bet = 0 total_pot_size = 0 all_card_array_player1 = np.zeros((4, 13)) all_card_array_player2 = np.zeros((4, 13)) flop1_array, all_card_array_player1, all_card_array_player2 = convert_to_numpy_array( Card.int_to_str(flop[0]), all_card_array_player1, all_card_array_player2) flop2_array, all_card_array_player1, all_card_array_player2 = convert_to_numpy_array( Card.int_to_str(flop[1]), all_card_array_player1, all_card_array_player2) flop3_array, all_card_array_player1, all_card_array_player2 = convert_to_numpy_array( Card.int_to_str(flop[2]), all_card_array_player1, all_card_array_player2) turn_array = np.zeros((4, 13)) river_array = np.zeros((4, 13)) Card6_array_player1, all_card_array_player1 = convert_to_numpy_array_playercards( Card.int_to_str(player1_hand[0]), all_card_array_player1) Card7_array_player1, all_card_array_player1 = convert_to_numpy_array_playercards( Card.int_to_str(player1_hand[1]), all_card_array_player1) Card6_array_player2, all_card_array_player2 = convert_to_numpy_array_playercards( Card.int_to_str(player2_hand[0]), all_card_array_player2) Card7_array_player2, all_card_array_player2 = convert_to_numpy_array_playercards( Card.int_to_str(player2_hand[1]), all_card_array_player2) pot_array_stage1 = convert_pot_to_numpy(total_pot_size) state_array_player1_stage1 = np.stack( (flop1_array, flop2_array, flop3_array, turn_array, river_array, Card6_array_player1, Card7_array_player1, all_card_array_player1, pot_array_stage1)) hash_key_stage1 = pickle.dumps(state_array_player1_stage1) gain_loss_stage1 = np.zeros((3)) gain_loss_stage1_count = np.zeros((3)) winner, pot_size, player1_new_bet, player2_new_bet, player1_action_stage1, player2_action = betting( model_player1, model_player2, flop1_array, flop2_array, flop3_array, turn_array, river_array, Card6_array_player1, Card7_array_player1, Card6_array_player2, Card7_array_player2, all_card_array_player1, all_card_array_player2, total_pot_size) total_pot_size += pot_size player1_bet += player1_new_bet player2_bet += player2_new_bet if winner == "Player 1": # print("Player 1 Wins!!",total_pot_size) # print("Player 1 Gain!!",int(total_pot_size-player1_bet)) if player1_action_stage1 == "Bet": if hash_key_stage1 in reward_table.keys(): reward_table[hash_key_stage1][2] += int(total_pot_size - player1_bet) reward_count[hash_key_stage1][2] += 1 else: gain_loss_stage1[2] += int(total_pot_size - player1_bet) gain_loss_stage1_count[2] += 1 elif player1_action_stage1 == "Check/Call": if hash_key_stage1 in reward_table.keys(): reward_table[hash_key_stage1][1] += int(total_pot_size - player1_bet) reward_count[hash_key_stage1][1] += 1 else: gain_loss_stage1[1] += int(total_pot_size - player1_bet) gain_loss_stage1_count[1] += 1 elif winner == "Player 2": # print("Player 2 Wins!!",total_pot_size) # print("Player 2 Gain!!",int(total_pot_size-player2_bet)) if player1_action_stage1 == "Fold": if hash_key_stage1 in reward_table.keys(): reward_table[hash_key_stage1][0] = -1 * int(player1_bet) reward_count[hash_key_stage1][0] += 1 else: gain_loss_stage1[0] = -1 * int(player1_bet) gain_loss_stage1_count[0] = 1 else: turn_array, all_card_array_player1, all_card_array_player2 = convert_to_numpy_array( Card.int_to_str(turn), all_card_array_player1, all_card_array_player2) pot_array_stage2 = convert_pot_to_numpy(total_pot_size) state_array_player1_stage2 = np.stack( (flop1_array, flop2_array, flop3_array, turn_array, river_array, Card6_array_player1, Card7_array_player1, all_card_array_player1, pot_array_stage2)) hash_key_stage2 = pickle.dumps(state_array_player1_stage2) gain_loss_stage2 = np.zeros((3)) gain_loss_stage2_count = np.zeros((3)) winner, pot_size, player1_new_bet, player2_new_bet, player1_action_stage2, player2_action = betting( model_player1, model_player2, flop1_array, flop2_array, flop3_array, turn_array, river_array, Card6_array_player1, Card7_array_player1, Card6_array_player2, Card7_array_player2, all_card_array_player1, all_card_array_player2, total_pot_size) total_pot_size += pot_size player1_bet += player1_new_bet player2_bet += player2_new_bet if winner == "Player 1": # print("Player 1 Wins!!", total_pot_size) # print("Player 1 Gain!!", int(total_pot_size - player1_bet)) if player1_action_stage2 == "Bet": if hash_key_stage2 in reward_table.keys(): reward_table[hash_key_stage2][2] += int( total_pot_size - player1_bet) reward_count[hash_key_stage2][2] += 1 else: gain_loss_stage2[2] = int(total_pot_size - player1_bet) gain_loss_stage2_count[2] = 1 elif player1_action_stage2 == "Check/Call": if hash_key_stage2 in reward_table.keys(): reward_table[hash_key_stage2][1] += int( total_pot_size - player1_bet) reward_count[hash_key_stage2][1] += 1 else: gain_loss_stage2[1] = int(total_pot_size - player1_bet) gain_loss_stage2_count[1] = 1 if player1_action_stage1 == "Bet": if hash_key_stage1 in reward_table.keys(): reward_table[hash_key_stage1][2] += int( total_pot_size - player1_bet) reward_count[hash_key_stage1][2] += 1 else: gain_loss_stage1[2] = int(total_pot_size - player1_bet) gain_loss_stage1_count[2] = 1 elif player1_action_stage1 == "Check/Call": if hash_key_stage1 in reward_table.keys(): reward_table[hash_key_stage1][1] += int( total_pot_size - player1_bet) reward_count[hash_key_stage1][1] += 1 else: gain_loss_stage1[1] = int(total_pot_size - player1_bet) gain_loss_stage1_count[1] = 1 elif winner == "Player 2": # print("Player 2 Wins!!", total_pot_size) # print("Player 2 Gain!!", int(total_pot_size - player2_bet)) if player1_action_stage2 == "Fold": if hash_key_stage2 in reward_table.keys(): reward_table[hash_key_stage2][0] = -1 * int( player1_bet) reward_count[hash_key_stage2][0] += 1 else: gain_loss_stage2[0] = -1 * int(player1_bet) gain_loss_stage2_count[0] += 1 if player1_action_stage1 == "Bet": if hash_key_stage1 in reward_table.keys(): reward_table[hash_key_stage1][2] += -1 * int( player1_bet) reward_count[hash_key_stage1][2] += 1 else: gain_loss_stage1[2] = -1 * int(player1_bet) gain_loss_stage1_count[2] = 1 elif player1_action_stage1 == "Check/Call": if hash_key_stage1 in reward_table.keys(): reward_table[hash_key_stage1][1] += -1 * int( player1_bet) reward_count[hash_key_stage1][1] += 1 else: gain_loss_stage1[1] = -1 * int(player1_bet) gain_loss_stage1_count[1] = 1 else: river_array, all_card_array_player1, all_card_array_player2 = convert_to_numpy_array( Card.int_to_str(river), all_card_array_player1, all_card_array_player2) winner, pot_size, player1_new_bet, player2_new_bet, player1_action_stage3, player2_action = betting( model_player1, model_player2, flop1_array, flop2_array, flop3_array, turn_array, river_array, Card6_array_player1, Card7_array_player1, Card6_array_player2, Card7_array_player2, all_card_array_player1, all_card_array_player2, total_pot_size) pot_array_stage3 = convert_pot_to_numpy(total_pot_size) state_array_player1_stage3 = np.stack( (flop1_array, flop2_array, flop3_array, turn_array, river_array, Card6_array_player1, Card7_array_player1, all_card_array_player1, pot_array_stage3)) hash_key_stage3 = pickle.dumps(state_array_player1_stage3) gain_loss_stage3 = np.zeros((3)) gain_loss_stage3_count = np.zeros((3)) total_pot_size += pot_size player1_bet += player1_new_bet player2_bet += player2_new_bet if winner == "Player 1": # print("Player 1 Wins!!", total_pot_size) # print("Player 1 Gain!!", int(total_pot_size - player1_bet)) if player1_action_stage3 == "Bet": if hash_key_stage3 in reward_table.keys(): reward_table[hash_key_stage3][2] += int( total_pot_size - player1_bet) reward_count[hash_key_stage3][2] += 1 else: gain_loss_stage3[2] = int(total_pot_size - player1_bet) gain_loss_stage3_count[2] = 1 elif player1_action_stage3 == "Check/Call": if hash_key_stage3 in reward_table.keys(): reward_table[hash_key_stage3][1] += int( total_pot_size - player1_bet) reward_count[hash_key_stage3][1] += 1 else: gain_loss_stage3[1] = int(total_pot_size - player1_bet) gain_loss_stage3_count[1] = 1 if player1_action_stage1 == "Bet": if hash_key_stage1 in reward_table.keys(): reward_table[hash_key_stage1][2] += int( total_pot_size - player1_bet) reward_count[hash_key_stage1][2] += 1 else: gain_loss_stage1[2] = int(total_pot_size - player1_bet) gain_loss_stage1_count[2] = 1 elif player1_action_stage1 == "Check/Call": if hash_key_stage1 in reward_table.keys(): reward_table[hash_key_stage1][1] += int( total_pot_size - player1_bet) reward_count[hash_key_stage1][1] += 1 else: gain_loss_stage1[1] = int(total_pot_size - player1_bet) gain_loss_stage1_count[1] = 1 if player1_action_stage2 == "Bet": if hash_key_stage2 in reward_table.keys(): reward_table[hash_key_stage2][2] += int( total_pot_size - player1_bet) reward_count[hash_key_stage2][2] += 1 else: gain_loss_stage2[2] = int(total_pot_size - player1_bet) gain_loss_stage2_count[2] = 1 elif player1_action_stage2 == "Check/Call": if hash_key_stage2 in reward_table.keys(): reward_table[hash_key_stage2][1] += int( total_pot_size - player1_bet) reward_count[hash_key_stage2][1] += 1 else: gain_loss_stage2[1] = int(total_pot_size - player1_bet) gain_loss_stage2_count[1] = 1 elif winner == "Player 2": # print("Player 2 Wins!!", total_pot_size) # print("Player 2 Gain!!", int(total_pot_size - player2_bet)) if player1_action_stage3 == "Fold": if hash_key_stage3 in reward_table.keys(): reward_table[hash_key_stage3][0] = -1 * int( player1_bet) reward_count[hash_key_stage3][0] += 1 else: gain_loss_stage3[0] = -1 * int(player1_bet) gain_loss_stage3_count[0] += 1 if player1_action_stage1 == "Bet": if hash_key_stage1 in reward_table.keys(): reward_table[hash_key_stage1][2] += -1 * int( player1_bet) reward_count[hash_key_stage1][2] += 1 else: gain_loss_stage1[2] = -1 * int(player1_bet) gain_loss_stage1_count[2] = 1 elif player1_action_stage1 == "Check/Call": if hash_key_stage1 in reward_table.keys(): reward_table[hash_key_stage1][1] += -1 * int( player1_bet) reward_count[hash_key_stage1][1] += 1 else: gain_loss_stage1[1] = -1 * int(player1_bet) gain_loss_stage1_count[1] = 1 if player1_action_stage2 == "Bet": if hash_key_stage2 in reward_table.keys(): reward_table[hash_key_stage2][2] += -1 * int( player1_bet) reward_count[hash_key_stage2][2] += 1 else: gain_loss_stage2[2] = -1 * int(player1_bet) gain_loss_stage2_count[2] = 1 elif player1_action_stage2 == "Check/Call": if hash_key_stage2 in reward_table.keys(): reward_table[hash_key_stage2][1] += -1 * int( player1_bet) reward_count[hash_key_stage2][1] += 1 else: gain_loss_stage2[1] = -1 * int(player1_bet) gain_loss_stage2_count[1] = 1 else: final_score_player1 = evaluator.get_rank_class( evaluator._seven(player1_complete_hand)) final_score_player2 = evaluator.get_rank_class( evaluator._seven(player2_complete_hand)) if final_score_player1 < final_score_player2: # print("Player 1 Wins!!",total_pot_size) # print("Player 1 Gain!!",int(total_pot_size - player1_bet)) if player1_action_stage3 == "Bet": if hash_key_stage3 in reward_table.keys(): reward_table[hash_key_stage3][2] += int( total_pot_size - player1_bet) reward_count[hash_key_stage3][2] += 1 else: gain_loss_stage3[2] = int(total_pot_size - player1_bet) gain_loss_stage3_count[2] = 1 elif player1_action_stage3 == "Check/Call": if hash_key_stage3 in reward_table.keys(): reward_table[hash_key_stage3][1] += int( total_pot_size - player1_bet) reward_count[hash_key_stage3][1] += 1 else: gain_loss_stage3[1] = int(total_pot_size - player1_bet) gain_loss_stage3_count[1] = 1 if player1_action_stage1 == "Bet": if hash_key_stage1 in reward_table.keys(): reward_table[hash_key_stage1][2] += int( total_pot_size - player1_bet) reward_count[hash_key_stage1][2] += 1 else: gain_loss_stage1[2] = int(total_pot_size - player1_bet) gain_loss_stage1_count[2] = 1 elif player1_action_stage1 == "Check/Call": if hash_key_stage1 in reward_table.keys(): reward_table[hash_key_stage1][1] += int( total_pot_size - player1_bet) reward_count[hash_key_stage1][1] += 1 else: gain_loss_stage1[1] = int(total_pot_size - player1_bet) gain_loss_stage1_count[1] = 1 if player1_action_stage2 == "Bet": if hash_key_stage2 in reward_table.keys(): reward_table[hash_key_stage2][2] += int( total_pot_size - player1_bet) reward_count[hash_key_stage2][2] += 1 else: gain_loss_stage2[2] = int(total_pot_size - player1_bet) gain_loss_stage2_count[2] = 1 elif player1_action_stage2 == "Check/Call": if hash_key_stage2 in reward_table.keys(): reward_table[hash_key_stage2][1] += int( total_pot_size - player1_bet) reward_count[hash_key_stage2][1] += 1 else: gain_loss_stage2[1] = int(total_pot_size - player1_bet) gain_loss_stage2_count[1] = 1 else: # print("Player 2 Wins!!",total_pot_size) # print("Player 2 Gain!!",int(total_pot_size-player2_bet)) # print("Player 2 Wins!!", total_pot_size) # print("Player 2 Gain!!", int(total_pot_size - player2_bet)) if player1_action_stage3 == "Fold": if hash_key_stage3 in reward_table.keys(): reward_table[hash_key_stage3][0] = -1 * int( player1_bet) reward_count[hash_key_stage3][0] += 1 else: gain_loss_stage3[0] = -1 * int(player1_bet) gain_loss_stage3_count[0] += 1 if player1_action_stage1 == "Bet": if hash_key_stage1 in reward_table.keys(): reward_table[hash_key_stage1][2] += -1 * int( player1_bet) reward_count[hash_key_stage1][2] += 1 else: gain_loss_stage1[2] = -1 * int(player1_bet) gain_loss_stage1_count[2] = 1 elif player1_action_stage1 == "Check/Call": if hash_key_stage1 in reward_table.keys(): reward_table[hash_key_stage1][1] += -1 * int( player1_bet) reward_count[hash_key_stage1][1] += 1 else: gain_loss_stage1[1] = -1 * int(player1_bet) gain_loss_stage1_count[1] = 1 if player1_action_stage2 == "Bet": if hash_key_stage2 in reward_table.keys(): reward_table[hash_key_stage2][2] += -1 * int( player1_bet) reward_count[hash_key_stage2][2] += 1 else: gain_loss_stage2[2] = -1 * int(player1_bet) gain_loss_stage2_count[2] = 1 elif player1_action_stage2 == "Check/Call": if hash_key_stage2 in reward_table.keys(): reward_table[hash_key_stage2][1] += -1 * int( player1_bet) reward_count[hash_key_stage2][1] += 1 else: gain_loss_stage2[1] = -1 * int(player1_bet) gain_loss_stage2_count[1] = 1 if hash_key_stage3 not in reward_table.keys(): reward_table[hash_key_stage3] = gain_loss_stage3 reward_count[hash_key_stage3] = gain_loss_stage3_count if hash_key_stage2 not in reward_table.keys(): reward_table[hash_key_stage2] = gain_loss_stage2 reward_count[hash_key_stage2] = gain_loss_stage2_count if hash_key_stage1 not in reward_table.keys(): reward_table[hash_key_stage1] = gain_loss_stage1 reward_count[hash_key_stage1] = gain_loss_stage1_count round_number += 1 print_progress(round_number, rounds, prefix='Progress:', suffix='Complete', bar_length=40) for key in reward_table.keys(): count_fold = reward_count[key][0] count_check = reward_count[key][1] count_bet = reward_count[key][2] if count_fold != 0: reward_table[key][0] = reward_table[key][0] / count_fold if count_check != 0: reward_table[key][1] = reward_table[key][1] / count_check if count_bet != 0: reward_table[key][2] = reward_table[key][2] / count_bet return reward_table
def get_card_str(self): return [Card.int_to_str(x) for x in self.cards]