Ejemplo n.º 1
0
 def key_part(part):
     """
     returns a comparison key for a part
     """
     if part[1] in SUIT_MAP.keys():
         # odds
         is_pair = part[0] == part[2]
         is_suited = part[1] == part[3]
         key_rank1 = Rank.from_mnemonic(part[0])
         key_rank2 = Rank.from_mnemonic(part[2])
         key_suit1 = Suit.from_mnemonic(part[1])
         key_suit2 = Suit.from_mnemonic(part[3])
     else:
         # evens
         is_pair = part[0] == part[1]
         is_suited = len(part) > 2 and part[2] == "s"
         key_rank1 = Rank.from_mnemonic(part[0])
         key_rank2 = Rank.from_mnemonic(part[1])
         key_suit1 = SPADES # no way to compare, e.g. AA to AhAs
         key_suit2 = SPADES # ditto
     return [is_pair, is_suited, key_rank1, key_rank2, key_suit1, key_suit2]
Ejemplo n.º 2
0
 def key_part(part):
     """
     returns a comparison key for a part
     """
     if part[1] in SUIT_MAP.keys():
         # odds
         is_pair = part[0] == part[2]
         is_suited = part[1] == part[3]
         key_rank1 = Rank.from_mnemonic(part[0])
         key_rank2 = Rank.from_mnemonic(part[2])
         key_suit1 = Suit.from_mnemonic(part[1])
         key_suit2 = Suit.from_mnemonic(part[3])
     else:
         # evens
         is_pair = part[0] == part[1]
         is_suited = len(part) > 2 and part[2] == "s"
         key_rank1 = Rank.from_mnemonic(part[0])
         key_rank2 = Rank.from_mnemonic(part[1])
         key_suit1 = SPADES  # no way to compare, e.g. AA to AhAs
         key_suit2 = SPADES  # ditto
     return [is_pair, is_suited, key_rank1, key_rank2, key_suit1, key_suit2]
Ejemplo n.º 3
0
def _colate_group(g):
    """
    takes a list of hands that share a primary rank
    may be pairs, suited, or offsuit
    returns a list of range parts, e.g. ["AA", "TT", "99"] -> ["AA", "TT-99"]
    """
    # pylint:disable=R0912
    # I know this code reads weird
    if not g:
        return []
    is_pairs = (g[0][0] == g[0][1])
    prefix = g[0][0]  # only means anything for non-pair hands
    # only means anything for non-pair hands
    primary = Rank.from_mnemonic(prefix)
    postfix = g[0][2:]  # only means anything for non-pair hands
    secondaries = [text[1] for text in g]
    ranks = [Rank.from_mnemonic(ch) for ch in secondaries]
    ranks.sort(reverse=True)
    all_ranks = RANKS_HIGH_TO_LOW[:]
    # so we have:
    # ranks =     [A, T, 9]
    # all_ranks = [A, K, Q, J, T, 9, 8, 7, 6, 5, 4, 3, 2] 
    # and we want [A, T-9]
    # or as an interim result: [[A], [T, 9]] (we will call this list groups)
    groups = []
    # algorithm:
    # - run down all_ranks until we find a card that matches head of ranks
    # - add a new group, and add this rank to the current group
    # - keep running down while cards match, adding them to the current group
    # - when one doesn't match, set current group to None, as per we started
    current_group = None
    for rank in all_ranks:
        if not ranks:
            break
        if current_group is None:
            if rank == ranks[0]:
                current_group = [rank]
                del ranks[0]
                groups.append(current_group)
        else:
            if rank == ranks[0]:
                current_group.append(rank)
                del ranks[0]
            else:
                current_group = None
    results = []
    for group in groups:
        if len(group) == 1:
            if is_pairs:
                results.append(RANK_INVERT[group[0]] * 2)
            else:
                results.append("%s%s%s" % (prefix, RANK_INVERT[group[0]],
                                           postfix))
        else:
            if is_pairs:
                if group[0] == ACE:
                    results.append("%s+" % (RANK_INVERT[group[-1]] * 2))
                else:
                    results.append("-".join([RANK_INVERT[group[0]] * 2,
                                             RANK_INVERT[group[-1]] * 2]))
            else:
                # this now may be a "AJs-ATs" type hand or "ATs+" type hand
                # the condition is: the secondary of the second element is the
                # next lowest rank than the secondary of the first.
                if RANKS_LOW_TO_HIGH.index(primary)  \
                    - RANKS_LOW_TO_HIGH.index(group[0]) == 1:
                    results.append("%s%s%s+" %
                                   (prefix, RANK_INVERT[group[-1]], postfix))
                else:
                    results.append("%s%s%s-%s%s%s" %
                                   (prefix, RANK_INVERT[group[0]], postfix,
                                    prefix, RANK_INVERT[group[-1]], postfix))
    return results
Ejemplo n.º 4
0
def _colate_group(g):
    """
    takes a list of hands that share a primary rank
    may be pairs, suited, or offsuit
    returns a list of range parts, e.g. ["AA", "TT", "99"] -> ["AA", "TT-99"]
    """
    # pylint:disable=R0912
    # I know this code reads weird
    if not g:
        return []
    is_pairs = (g[0][0] == g[0][1])
    prefix = g[0][0]  # only means anything for non-pair hands
    # only means anything for non-pair hands
    primary = Rank.from_mnemonic(prefix)
    postfix = g[0][2:]  # only means anything for non-pair hands
    secondaries = [text[1] for text in g]
    ranks = [Rank.from_mnemonic(ch) for ch in secondaries]
    ranks.sort(reverse=True)
    all_ranks = RANKS_HIGH_TO_LOW[:]
    # so we have:
    # ranks =     [A, T, 9]
    # all_ranks = [A, K, Q, J, T, 9, 8, 7, 6, 5, 4, 3, 2]
    # and we want [A, T-9]
    # or as an interim result: [[A], [T, 9]] (we will call this list groups)
    groups = []
    # algorithm:
    # - run down all_ranks until we find a card that matches head of ranks
    # - add a new group, and add this rank to the current group
    # - keep running down while cards match, adding them to the current group
    # - when one doesn't match, set current group to None, as per we started
    current_group = None
    for rank in all_ranks:
        if not ranks:
            break
        if current_group is None:
            if rank == ranks[0]:
                current_group = [rank]
                del ranks[0]
                groups.append(current_group)
        else:
            if rank == ranks[0]:
                current_group.append(rank)
                del ranks[0]
            else:
                current_group = None
    results = []
    for group in groups:
        if len(group) == 1:
            if is_pairs:
                results.append(RANK_INVERT[group[0]] * 2)
            else:
                results.append("%s%s%s" %
                               (prefix, RANK_INVERT[group[0]], postfix))
        else:
            if is_pairs:
                if group[0] == ACE:
                    results.append("%s+" % (RANK_INVERT[group[-1]] * 2))
                else:
                    results.append("-".join([
                        RANK_INVERT[group[0]] * 2, RANK_INVERT[group[-1]] * 2
                    ]))
            else:
                # this now may be a "AJs-ATs" type hand or "ATs+" type hand
                # the condition is: the secondary of the second element is the
                # next lowest rank than the secondary of the first.
                if RANKS_LOW_TO_HIGH.index(primary)  \
                    - RANKS_LOW_TO_HIGH.index(group[0]) == 1:
                    results.append("%s%s%s+" %
                                   (prefix, RANK_INVERT[group[-1]], postfix))
                else:
                    results.append("%s%s%s-%s%s%s" %
                                   (prefix, RANK_INVERT[group[0]], postfix,
                                    prefix, RANK_INVERT[group[-1]], postfix))
    return results