Example #1
0
def printCards(cards):
    outStr = "[ ";
    for i in range(0, len(cards)):
        outStr += "{0}".format(Card.int_to_str(cards[i]));
        if i != len(cards)-1:
            outStr += ", ";
    outStr += " ]";
    print outStr;
Example #2
0
    def generate_action_data(self, table):
        if self.stage_actions[-1][0] == "ALL_IN":
            return 0

        hand_idx = table.hand_idx
        pos_idx = self.playing_position
        hero_cards = [[Card.int_to_str(x)[0],
                       Card.int_to_str(x)[1]] for x in self.hand]
        board = [[Card.int_to_str(x)[0],
                  Card.int_to_str(x)[1]] for x in table.board]

        # Padding board
        board += ["__"] * (5 - len(board))

        stage = table.stage
        stack = self.stack

        pot = table.current_pot
        action_type = self.stage_actions[-1][0]
        action_value = self.stage_actions[-1][1]
        current_bet = table.current_bet
        num_active_players = len(table.get_active_players())
        net_stack = np.nan

        flatten = lambda l: [item for sublist in l for item in sublist]

        data_list = []
        data_list.append(hand_idx)
        data_list.append(pos_idx)
        data_list += flatten(hero_cards)
        data_list += flatten(board)
        data_list.append(stage)
        data_list.append(stack)
        data_list.append(pot)
        data_list.append(action_type)
        data_list.append(action_value)
        data_list.append(current_bet)
        data_list.append(num_active_players)
        data_list.append(net_stack)

        # self.actions_df.append(data_list)
        self.actions_df.loc[len(self.actions_df), :] = data_list
Example #3
0
 def update_hand(self, hand, turn):
     # input hand of player, update to widget base on current turn
     openCardIndex = {1: 5, 2: 1, 3: 0, 4: -1}
     for i in range(5):
         self.ids[self.name + '_card' + str(i)].set_card(
             Card.int_to_str(hand[i]))
         if i > openCardIndex[turn]:
             self.ids[self.name + '_card' + str(i)].show_card_to_all()
         else:
             self.ids[self.name + '_card' + str(i)].show_card_to_self()
         '''
Example #4
0
 def get_or_create(cls, code):
     """
     Lookup a card by its code and create if it doesn't exist
     :param code: Deuces card code
     :return: The created Card
     """
     card = cls.query.filter_by(code=code).first()
     if not card:
         rank, suit = PokerCard.int_to_str(code)
         card = cls.create(code=code, suit=suit, rank=rank)
     return card
Example #5
0
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 upgrade():
    bind = op.get_bind()
    session = orm.Session(bind=bind)

    op.add_column('cards', sa.Column('rank',
                                     sa.String(length=1),
                                     nullable=True))
    op.add_column('cards', sa.Column('suit',
                                     sa.String(length=1),
                                     nullable=True))

    for card in session.query(Card):
        rank, suit = PokerCard.int_to_str(card.code)
        card.rank = rank
        card.suit = suit

    session.commit()

    op.alter_column('cards', 'rank', nullable=False)
    op.alter_column('cards', 'suit', nullable=False)
Example #7
0
def card_to_normal_str(card):
    " {'23456789TJQKA'} + {'shdc''} (note: lower case) "
    if card == -1:
        return ''
    return Card.int_to_str(card)
Example #8
0
card = Card.new('Ah')

# table cards
board = [
    Card.new('Ah'),
    Card.new('Kd'),
    Card.new('Jc')
]

# self cards
hand = [
   Card.new('Qs'),
   Card.new('Th')
]

# card is presented as int at deuces: https://github.com/worldveil/deuces/blob/master/deuces/card.py
print(card)# 268446761
# int to string
print(Card.int_to_str(card))#Ah


for c in board+hand:
    print(Card.int_to_str(c))
    
'''
Ah
Kd
Jc
Qs
Th
'''    
Example #9
0
def cards_to_str(cards):
    return {Card.int_to_str(card) for card in cards}
Example #10
0
def AI(hand, board, stack, opp_stack, BB, to_call, pot, dealer, bets,
       VARIABLES):

    if len(board) == 0:
        return preflop_decision(hand, board, stack, opp_stack, BB, to_call,
                                pot, dealer, bets)

    if len(board) == 3:
        name = "".join(
            sorted([Card.int_to_str(c) for c in board], key=str.lower))
        KEY = "flop_charts/%s.p" % name
        try:
            print "downloading file"
            s3.Bucket("chartsflopturn").download_file(
                KEY, os.path.join(os.path.dirname(__file__), 'currentflop.p'))
            print "found and downloaded file"
            with open(os.path.join(os.path.dirname(__file__), 'currentflop.p'),
                      "rb") as f:
                board_rankings = pickle.load(f)
                board_rankings = [i[0] for i in board_rankings]
        except:
            print "pickle loading error!"
            return int(0)

    if len(board) == 4:
        name = "".join(
            sorted([Card.int_to_str(c) for c in board], key=str.lower))
        KEY = "turn_charts/%s.p" % name
        try:
            print "downloading file"
            s3.Bucket("chartsflopturn").download_file(
                KEY, os.path.join(os.path.dirname(__file__), 'currentflop.p'))
            print "found and downloaded file"
            with open(os.path.join(os.path.dirname(__file__), 'currentflop.p'),
                      "rb") as f:
                board_rankings = pickle.load(f)
                board_rankings = [i[0] for i in board_rankings]
        except:
            print "pickle loading error!"
            return int(0)

    if len(board) == 5:
        board_rankings = generate_river_rankings(board)
        board_rankings = [i[0] for i in board_rankings]

    fixed_board_rankings = [
        i for i in board_rankings
        if (len(i & set(hand)) == 0 and len(i & set(board)) == 0) or (
            i == frozenset(hand))
    ]
    my_hand_percentile = 1 - (fixed_board_rankings.index(frozenset(hand)) /
                              float(len(fixed_board_rankings)))

    strong_bets = len([o for o in bets if o[0] == "bet" and o[1] > BB * 3])
    very_strong_bets = len(
        [o for o in bets if o[0] == "bet" and o[1] > pot / 2.5 and o[2] > 0])
    strong_calls = len([o for o in bets if o[0] == "call" and o[1] > BB * 3])
    checks = len([o for o in bets if o[0] == "check"])

    if very_strong_bets > 0:
        V_strength = max(1.0 + strong_bets + strong_calls + very_strong_bets,
                         1.0)
    else:
        V_strength = max(1.0 + strong_bets + strong_calls - checks, 1.0)

    if to_call > 25 * BB:
        V_strength += 2

    if to_call >= opp_stack / 2.0:
        V_strength += 1

    if to_call == opp_stack:
        if len(board) <= 3 or pot / 1.4 < to_call:
            V_strength += 2
        else:
            V_strength += 1

    if len(board) < 5:

        if to_call > 0:
            EV_CALL = (my_hand_percentile**V_strength) * pot - (
                1 - my_hand_percentile**V_strength) * to_call
            if EV_CALL > 0:
                if EV_CALL > pot / 2.0:
                    if random.random() > .5:
                        return to_call
                    else:
                        return min(
                            stack,
                            random.choice([to_call + pot, to_call * 2 + pot]))
                elif pot / 2.0 >= EV_CALL > pot / 3.5:
                    return min(stack, to_call + pot)
                else:
                    return to_call
            elif to_call < stack / 4.0:
                if V_strength < 3:
                    if random.random() > .85:
                        return min(
                            stack,
                            random.choice([
                                to_call + pot, to_call + pot, to_call * 2 + pot
                            ]))
                elif V_strength < 5:
                    if random.random() > .95:
                        return min(stack, random.choice([to_call + pot]))
                return int(0)
            return int(0)

        if to_call == 0:
            if dealer == True:
                if my_hand_percentile**V_strength > .8 and len(board) == 3:
                    return int(0)
                if my_hand_percentile**V_strength > .66:
                    return min(pot, stack)
                elif V_strength < 4 and random.random() > .75:
                    return min(pot, stack)
            else:
                if my_hand_percentile**V_strength > .85:
                    return int(0)
                elif my_hand_percentile**V_strength > .66:
                    return min(pot, stack)
                elif V_strength < 4 and random.random() > .85:
                    return min(pot, stack)
            return int(0)

    else:
        if my_hand_percentile**V_strength > .95:
            return min(stack, pot * 2 + to_call)

        if to_call > 0:
            EV_CALL = (my_hand_percentile**V_strength) * pot - (
                1 - my_hand_percentile**V_strength) * to_call
            if EV_CALL > 0:
                if EV_CALL > pot / 1.2:
                    return min(stack, pot * 2 + to_call)
                if EV_CALL > pot / 2.0:
                    return min(stack, to_call + pot)
                else:
                    return to_call
            if to_call < stack / 4.0 and to_call < opp_stack / 4.0 and strong_bets < 3:
                if random.random() < .1:
                    return min(stack, to_call * 2 + pot)
            return int(0)
        elif to_call == 0:
            if dealer == True:
                if my_hand_percentile**V_strength > .85:
                    return min(stack, pot * 2)
                elif my_hand_percentile**V_strength > .75:
                    return min(stack / 2.0, pot)
                elif my_hand_percentile**V_strength > .6:
                    return min(pot, stack / 3.0)
            else:
                if my_hand_percentile**V_strength > .9:
                    return min(stack, pot * 2)
                elif my_hand_percentile > .8:
                    return min(stack / 3.0, pot)
                elif my_hand_percentile**V_strength > .7:
                    return min(pot, stack / 4.0)
            return int(0)

        return int(0)
Example #11
0
def queryCardsToThrow(_hand):

    worse_cards = []
    thrown_cards = ''
    card_range = range(0, 5)

    ########### EVALUATE HAND ###########
    print '=== EVALUATION ==='

    poker_hand = []

    for n in _hand:
        poker_hand.append(Card.new(n))

    Card.print_pretty_cards(poker_hand)

    score = DEUCES_EVALUATOR.evaluate([], poker_hand)
    rank = DEUCES_EVALUATOR.get_rank_class(score)
    ########### / EVALUATE HAND ###########

    print("======= THROW CARDS ACTION =======")

    ########### SEARCH FOR COMBINATIONS ###########

    # Search for High Card
    def search_highCard(cards):
        max = 0
        k = 0
        for i in card_range:
            current = ((cards[i] & 0xF00) >> 8)
            if current > max:
                max = current
                k = i
        return k

    # Search for Pair combination
    def search_pair(cards):
        for i in range(0, 4):
            for j in range(i + 1, 5):
                if (cards[i] & 0xF00) == (cards[j] & 0xF00):
                    return [i, j]
        return []

    # Search for Two Pair combination
    def search_twoPair(cards):
        [k, l] = search_pair(cards)
        for i in range(k + 1, 4):
            for j in range(i + 1, 5):
                if (cards[i] & 0xF00) == (cards[j] & 0xF00):
                    return [k, l, i, j]
        return []

    # Search for Three of a Kind combination
    def search_threeKind(cards):
        [k, l] = search_pair(cards)
        for i in range(l + 1, 5):
            if (cards[i] & 0xF00) == (cards[l] & 0xF00):
                return [k, l, i]
        return []

    # Search for Four of a Kind combination
    def search_fourKind(cards):
        [k, l] = search_pair(cards)
        if k != 0:
            return 1
        else:
            for i in range(1, 5):
                if (cards[i] & 0xF00) != (cards[0] & 0xF00):
                    return i

    ########### / SEARCH ###########

    ########### ACTION DEPENDS HAND RANK ###########

    # For Four of a Kind: Keep the four of a kind a throw the rest away
    if rank == 2:
        worse_cards = [
            Card.int_to_str(poker_hand[search_fourKind(poker_hand)])
        ]
    # For Three of a Kind: Keep the three of a kind and throw the rest away
    elif rank == 6:
        keep_cards = set(search_threeKind(poker_hand))
        throw_away = filter(lambda x: x not in keep_cards, card_range)
        worse_cards = [
            Card.int_to_str(poker_hand[throw_away[0]]),
            Card.int_to_str(poker_hand[throw_away[1]])
        ]
    # For Two Pair: Keep the two pair and throw the rest away
    elif rank == 7:
        keep_cards = set(search_twoPair(poker_hand))
        throw_away = filter(lambda x: x not in keep_cards, card_range)
        worse_cards = [Card.int_to_str(poker_hand[throw_away[0]])]
    # For Pair: Keep the pair and throw the rest away
    elif rank == 8:
        keep_cards = set(search_pair(poker_hand))
        throw_away = filter(lambda x: x not in keep_cards, card_range)
        worse_cards = [
            Card.int_to_str(poker_hand[throw_away[0]]),
            Card.int_to_str(poker_hand[throw_away[1]]),
            Card.int_to_str(poker_hand[throw_away[2]])
        ]
    # For High Card: Keep the high card and throw the rest away
    elif rank == 9:
        keep_cards = [search_highCard(poker_hand)]
        throw_away = filter(lambda x: x not in keep_cards, card_range)
        worse_cards = [
            Card.int_to_str(poker_hand[throw_away[0]]),
            Card.int_to_str(poker_hand[throw_away[1]]),
            Card.int_to_str(poker_hand[throw_away[2]]),
            Card.int_to_str(poker_hand[throw_away[3]])
        ]

    ########### / ACTION ###########

    # Create string of thrown away cards:
    for i in range(0, len(worse_cards)):
        thrown_cards += worse_cards[i] + ' '
    print('Weakest cards are thrown away: ' + thrown_cards)

    return thrown_cards
Example #12
0
 def fget(self):
     return [Card.int_to_str(card) for card in self._board._cards]
Example #13
0
def print_cards(cards, rank_class):
    s = []
    for x in cards:
        s.append(Card.int_to_str(x))
    print(s, rank_class)
Example #14
0
 def _update_hand(self, hand):
     for i in range(2):
         self.ids["public_card" + str(i)].set_card(Card.int_to_str(hand[i]))
         self.ids["public_card" + str(i)].show_card_to_all()