Ejemplo n.º 1
0
 def str_to_card(card_string):
     """
     Convert this string to a pokerbots.engine.game.Card instance.
     Note that I don't check whether or not you passed the right format!
     """
     if isinstance(card_string, Card):
         return card
     rank_str = card_string[0].lower()
     suit_str = card_string[1].lower()
     rank = 2
     suit = 1
     if rank_str == "t":
         rank = 10
     elif rank_str == "j":
         rank = 11
     elif rank_str == "q":
         rank = 12
     elif rank_str == "k":
         rank = 13
     elif rank_str == "a":
         rank = 14
     if suit_str == "s":
         suit = 1
     elif suit_str == "h":
         suit = 2
     elif suit_str == "d":
         suit = 3
     elif suit_str == "c":
         suit = 4
     return Card(rank, suit)
Ejemplo n.º 2
0
def write_odd_xors_to_rank():  # 7-0
    result = {}

    for combination in combinations(xrange(13), 7):
        # All bits are different, so you can just use or
        # Should have 13 choose 7 = 1716
        odd_xor = reduce(__or__, map(lambda rank: 1 << rank, combination))
        hand = [
            Card(combination[0] + 2, 1),
            Card(combination[1] + 2, 2),
            Card(combination[2] + 2, 3),
            Card(combination[3] + 2, 4),
            Card(combination[4] + 2, 1),
            Card(combination[5] + 2, 2),
            Card(combination[6] + 2, 3)
        ]
        hand_combinations = combinations(hand, 5)
        result[odd_xor] = min(
            map(HandEvaluator.Five.evaluate_rank, hand_combinations))
    assert len(result) == 1716
    fh = open("seven.odd_xors_to_rank.py", "w")
    fh.write("odd_xors_to_rank = {\n")
    fh.write(",\n".join(
        map(
            lambda chunk: ", ".join(
                map(lambda k: "%s: %s" % (k, result[k]), chunk)),
            chunker(result.keys(), 4))))
    fh.write("}")
    fh.close()
Ejemplo n.º 3
0
def write_even_xors_to_rank():  # 0-3
    result = {}
    for combination in combinations(xrange(13), 3):
        # you can just use or again, since they're all the same
        # Should have 13 choose 3 = 286
        even_xor = reduce(__or__, map(lambda rank: 1 << rank, combination))
        hand = [
            Card(combination[0] + 2, 1),
            Card(combination[0] + 2, 2),
            Card(combination[1] + 2, 3),
            Card(combination[1] + 2, 4),
            Card(combination[2] + 2, 1),
            Card(combination[2] + 2, 2)
        ]
        hand_combinations = combinations(hand, 5)
        result[even_xor] = min(
            map(HandEvaluator.Five.evaluate_rank, hand_combinations))
    assert len(result) == 286
    fh = open("six.even_xors_to_rank.py", "w")
    fh.write("even_xors_to_rank = {\n")
    for key_group in chunker(result.keys(), 4):
        if len(key_group) == 4:
            fh.write("%s: %s, %s: %s, %s: %s, %s: %s,\n" %
                     (key_group[0], result[key_group[0]], key_group[1],
                      result[key_group[1]], key_group[2], result[key_group[2]],
                      key_group[3], result[key_group[3]]))
        else:  # last group has 2
            fh.write("%s: %s, %s: %s\n" % (key_group[0], result[key_group[0]],
                                           key_group[1], result[key_group[1]]))
    fh.write("}")
    fh.close()
Ejemplo n.º 4
0
def write_flush_rank_bits_to_rank():
    result = {}
    for i in xrange(5, 8):  # 5,6,7 ranks of same suit
        # Should have 13 choose 7 plus 13 choose 6 plus 13 choose 5
        # which is 1716 + 1716 + 1287 = 4719
        for combination in combinations(xrange(13), i):
            combo = list(combination)
            bits = reduce(__or__, map(lambda rank: 1 << rank, combination))
            cards = map(lambda rank: Card(rank + 2, 1), combination)
            hand_combinations = combinations(cards, 5)
            result[bits] = min(
                map(HandEvaluator.Five.evaluate_rank, hand_combinations))
    assert len(result) == 1716 + 1716 + 1287
    fh = open("seven.flush_rank_bits_to_rank.py", "w")
    fh.write("flush_rank_bits_to_rank = {\n")
    fh.write(",\n".join(
        map(
            lambda chunk: ", ".join(
                map(lambda k: "%s: %s" % (k, result[k]), chunk)),
            chunker(result.keys(), 4))))
    fh.write("}")
    fh.close()
Ejemplo n.º 5
0
def write_prime_products_to_rank():  # 4-0, 2-1, 0-2
    """
    Write a table mapping products of 6 ranks' corresponding prime
    numbers to hand rank. Only do it for hands in certain combinations,
    though, or the table gets really big.
    """
    result = {}

    # Choose four ranks, frequency (1,1,1,3)
    for combination in combinations(xrange(13), 4):
        # Choose each one to appear three times
        # Should be 4 * 13 choose 4 = 2860
        combo = list(combination)
        for i in xrange(4):
            rank = combo[i]
            other_ranks = combo[0:i] + combo[i + 1:4]
            prime_for_rank = LookupTables.primes[rank]

            # first get the product we need
            product = prime_for_rank * prime_for_rank * prime_for_rank * \
                reduce(mul,
                    map(lambda rank: LookupTables.primes[rank], other_ranks))

            # now generate the hand we need
            hand = [Card(rank + 2, 1), Card(rank + 2, 2), Card(rank + 2, 3)]
            hand = hand + map(lambda rank: Card(rank + 2, 4), other_ranks)
            hand_combinations = combinations(hand, 5)
            # now map the product to the 5-card hand rank
            result[product] = min(
                map(HandEvaluator.Five.evaluate_rank, hand_combinations))

    # Choose three cards, frequencies (1,1,4) and (1,3,2)
    for combination in combinations(xrange(13), 3):
        combo = list(combination)
        # Choose each one to appear four times for (1,1,4)
        # Should be 3 * 13 choose 3 = 858
        for i in xrange(3):
            rank = combo[i]
            other_ranks = combo[0:i] + combo[i + 1:3]
            prime_for_rank = LookupTables.primes[rank]

            product = prime_for_rank * prime_for_rank * \
                prime_for_rank * prime_for_rank * \
                reduce(mul,
                    map(lambda rank: LookupTables.primes[rank], other_ranks))

            hand = [
                Card(rank + 2, 1),
                Card(rank + 2, 2),
                Card(rank + 2, 3),
                Card(rank + 2, 4)
            ]
            hand = hand + map(lambda rank: Card(rank + 2, 4), other_ranks)
            hand_combinations = combinations(hand, 5)
            result[product] = min(
                map(HandEvaluator.Five.evaluate_rank, hand_combinations))

        # Now for each combination, permute so that we can do (1,3,2)
        # Should be 6 * 13 choose 3 = 1716
        for permute in permutations(combo):
            # permute contains the ranks in each order
            primes = map(lambda rank: LookupTables.primes[rank], permute)
            product = primes[0] * primes[1] * primes[1] * primes[1] * primes[
                2] * primes[2]
            hand = [
                Card(permute[0] + 2, 1),
                Card(permute[1] + 2, 2),
                Card(permute[1] + 2, 3),
                Card(permute[1] + 2, 4),
                Card(permute[2] + 2, 1),
                Card(permute[2] + 2, 2)
            ]
            hand_combinations = combinations(hand, 5)
            result[product] = min(
                map(HandEvaluator.Five.evaluate_rank, hand_combinations))

    # Choose two ranks, frequencies (2,4)
    for combination in combinations(xrange(13), 2):
        combo = list(combination)
        # Each needs to be the two once
        # Should have 2 * 13 choose 2 = 156
        for permute in permutations(combo):
            # permute contains the ranks in each order
            primes = map(lambda rank: LookupTables.primes[rank], permute)
            product = primes[0] * primes[0] * primes[1] * primes[1] * primes[
                1] * primes[1]
            hand = [
                Card(permute[0] + 2, 1),
                Card(permute[0] + 2, 2),
                Card(permute[1] + 2, 3),
                Card(permute[1] + 2, 4),
                Card(permute[1] + 2, 1),
                Card(permute[1] + 2, 2)
            ]
            hand_combinations = combinations(hand, 5)
            result[product] = min(
                map(HandEvaluator.Five.evaluate_rank, hand_combinations))

    # at the end, we should have 2860 + 858 + 1716 + 156 keys in the table
    assert len(result) == (2860 + 858 + 1716 + 156)
    fh = open("six.prime_products_to_rank.py", "w")
    fh.write("prime_products_to_rank = {\n")
    for key_group in chunker(result.keys(), 4):
        if len(key_group) == 4:
            fh.write("%s: %s, %s: %s, %s: %s, %s: %s,\n" %
                     (key_group[0], result[key_group[0]], key_group[1],
                      result[key_group[1]], key_group[2], result[key_group[2]],
                      key_group[3], result[key_group[3]]))
        else:  # last group has 2
            fh.write("%s: %s, %s: %s\n" % (key_group[0], result[key_group[0]],
                                           key_group[1], result[key_group[1]]))
    fh.write("}")
    fh.close()
Ejemplo n.º 6
0
def write_even_xors_to_odd_xors_to_rank():  # 4-1, 2-2
    result = defaultdict(dict)
    # choose five ranks, frequencies (1,1,1,1,2)
    for combination in combinations(xrange(13), 5):
        # Choose each one to appear twice
        # Should be 5 * 13 choose 5 = 6435
        combo = list(combination)
        for i in xrange(5):
            rank = combo[i]
            other_ranks = combo[0:i] + combo[i + 1:5]

            even_xor = 1 << rank
            odd_xor = reduce(__or__, map(lambda rank: 1 << rank, other_ranks))

            # now generate the hand we need
            hand = [Card(rank + 2, 1), Card(rank + 2, 2)]
            hand = hand + map(lambda rank: Card(rank + 2, 3), other_ranks)
            hand_combinations = combinations(hand, 5)
            # now map the product to the 5-card hand rank
            result[even_xor][odd_xor] = min(
                map(HandEvaluator.Five.evaluate_rank, hand_combinations))

    # choose four ranks, frequencies (1,1,2,2)
    for combination in combinations(xrange(13), 4):
        # Now choose every possible pair from those four to appear twice
        # Should have (4 choose 2) * (13 choose 4) = 6 * 715 = 4290
        combo = list(combination)
        for pair_ranks in combinations(combo, 2):
            one_ranks = filter(lambda rank: rank not in pair_ranks, combo)

            even_xor = reduce(__or__, map(lambda rank: 1 << rank, pair_ranks))
            odd_xor = reduce(__or__, map(lambda rank: 1 << rank, one_ranks))

            hand = [
                Card(pair_ranks[0] + 2, 1),
                Card(pair_ranks[0] + 2, 2),
                Card(pair_ranks[1] + 2, 3),
                Card(pair_ranks[1] + 2, 4),
                Card(one_ranks[0] + 2, 1),
                Card(one_ranks[1] + 2, 2)
            ]
            hand_combinations = combinations(hand, 5)
            result[even_xor][odd_xor] = min(
                map(HandEvaluator.Five.evaluate_rank, hand_combinations))

    # this assertion is more complex
    # First, count the even_xor keys
    assert len(result) == 13 + 78
    # Then count the leaves
    assert reduce(add, map(lambda even_xor: len(result[even_xor]),
                           result)) == 6435 + 4290
    fh = open("six.even_xors_to_odd_xors_rank.py", "w")
    fh.write("even_xors_to_odd_xors_to_rank = {\n")
    for even_xor in result.keys():
        fh.write("%s: { " % (even_xor, ))
        fh.write(",\n".join(
            map(
                lambda chunk: ", ".join(
                    map(
                        lambda odd_xor: "%s: %s" %
                        (odd_xor, result[even_xor][odd_xor]), chunk)),
                chunker(result[even_xor].keys(), 4))))
        fh.write(" },\n")
    fh.write("}")
    fh.close()
Ejemplo n.º 7
0
def write_flush_rank_bits_to_rank():
    result = {}
    for combination in combinations(xrange(13), 6):
        # Should have 13 choose 6 = 1716
        combo = list(combination)
        bits = reduce(__or__, map(lambda rank: 1 << rank, combination))
        cards = [
            Card(combo[0] + 2, 1),
            Card(combo[1] + 2, 1),
            Card(combo[2] + 2, 1),
            Card(combo[3] + 2, 1),
            Card(combo[4] + 2, 1),
            Card(combo[5] + 2, 1)
        ]
        hand_combinations = combinations(cards, 5)
        result[bits] = min(
            map(HandEvaluator.Five.evaluate_rank, hand_combinations))

    for combination in combinations(xrange(13), 5):
        # Should have 13 choose 5 = 1287
        combo = list(combination)
        bits = reduce(__or__, map(lambda rank: 1 << rank, combination))
        cards = [
            Card(combo[0] + 2, 1),
            Card(combo[1] + 2, 1),
            Card(combo[2] + 2, 1),
            Card(combo[3] + 2, 1),
            Card(combo[4] + 2, 1)
        ]
        result[bits] = HandEvaluator.Five.evaluate_rank(cards)

    assert len(result) == 1716 + 1287
    fh = open("six.flush_rank_bits_to_rank.py", "w")
    fh.write("flush_rank_bits_to_rank = {\n")
    for key_group in chunker(result.keys(), 4):
        if len(key_group) == 4:
            fh.write("%s: %s, %s: %s, %s: %s, %s: %s,\n" %
                     (key_group[0], result[key_group[0]], key_group[1],
                      result[key_group[1]], key_group[2], result[key_group[2]],
                      key_group[3], result[key_group[3]]))
        else:  # last group has 3
            fh.write(
                "%s: %s, %s: %s, %s: %s\n" %
                (key_group[0], result[key_group[0]], key_group[1],
                 result[key_group[1]], key_group[2], result[key_group[2]]))
    fh.write("}")
    fh.close()
Ejemplo n.º 8
0
def write_odd_xors_to_rank():  # 6-0, 2-0
    result = {}

    for combination in combinations(xrange(13), 6):
        # in the case where six different bits are set, you
        # can substitute or
        # Should have 13 choose 6 = 1716
        odd_xor = reduce(__or__, map(lambda rank: 1 << rank, combination))
        hand = [
            Card(combination[0] + 2, 1),
            Card(combination[1] + 2, 2),
            Card(combination[2] + 2, 3),
            Card(combination[3] + 2, 4),
            Card(combination[4] + 2, 1),
            Card(combination[5] + 2, 2)
        ]
        hand_combinations = combinations(hand, 5)
        result[odd_xor] = min(
            map(HandEvaluator.Five.evaluate_rank, hand_combinations))

    for combination in combinations(xrange(13), 2):
        # convert to rank number with 2 bits on
        # by only choosing two numbers, we can use or instead of xor
        # Should have 13 choose 2 = 78
        odd_xor = reduce(__or__, map(lambda rank: 1 << rank, combination))
        hand = [
            Card(combination[0] + 2, 1),
            Card(combination[0] + 2, 2),
            Card(combination[0] + 2, 3),
            Card(combination[1] + 2, 4),
            Card(combination[1] + 2, 1),
            Card(combination[1] + 2, 2)
        ]
        hand_combinations = combinations(hand, 5)
        result[odd_xor] = min(
            map(HandEvaluator.Five.evaluate_rank, hand_combinations))
    assert len(result) == 1716 + 78
    fh = open("six.odd_xors_to_rank.py", "w")
    fh.write("odd_xors_to_rank = {\n")
    for key_group in chunker(result.keys(), 4):
        if len(key_group) == 4:
            fh.write("%s: %s, %s: %s, %s: %s, %s: %s,\n" %
                     (key_group[0], result[key_group[0]], key_group[1],
                      result[key_group[1]], key_group[2], result[key_group[2]],
                      key_group[3], result[key_group[3]]))
        else:  # last group has 2
            fh.write("%s: %s, %s: %s\n" % (key_group[0], result[key_group[0]],
                                           key_group[1], result[key_group[1]]))
    fh.write("}")
    fh.close()
Ejemplo n.º 9
0
    def evaluate_opponent(self):

        if self.hands_played >= 1:
            last_pot = 0.0
            self_bet_for_round = 0
            opponent_bet_for_round = 0
            opponent_bet_strength_preflop = []
            opponent_bet_strength_flop = []
            opponent_bet_strength_turn = []
            opponent_bet_strength_river = []

            street = 'preflop'

            # obtain opponent's betting behavior from the previous round, and determine strength of hand if there's a showdown
            #
            for play in self.last[1]:
                if play[0] == self.name:
                    if isinstance(play[1], Post):
                        last_pot = last_pot + play[1].amount
                        self_bet_for_round = play[1].amount
                    elif isinstance(play[1], Bet):
                        last_pot = last_pot + play[1].amount
                        self_bet_for_round = play[1].amount
                    elif isinstance(play[1], Raise):
                        last_pot = last_pot - self_bet_for_round + play[
                            1].amount
                        self_bet_for_round = play[1].amount
                    elif isinstance(play[1], Call):
                        last_pot = last_pot + opponent_bet_for_round - self_bet_for_round
                        self_bet_for_round = opponent_bet_for_round
                elif play[0] == self.opponent['name']:
                    strength_of_bet = zeros(0)
                    if isinstance(play[1], Post):
                        last_pot = last_pot + play[1].amount
                        opponent_bet_for_round = play[1].amount
                    elif isinstance(play[1], Bet):
                        last_pot = last_pot + play[1].amount
                        opponent_bet_for_round = play[1].amount
                        opponent_bet_strength = play[1].amount / last_pot
                    elif isinstance(play[1], Raise):
                        last_pot = last_pot - opponent_bet_for_round + play[
                            1].amount
                        opponent_bet_strength = (
                            play[1].amount - opponent_bet_for_round) / last_pot
                        opponent_bet_for_round = play[1].amount
                    elif isinstance(play[1], Call):
                        last_pot = last_pot + self_bet_for_round - opponent_bet_for_round
                        opponent_bet_strength = (
                            self_bet_for_round -
                            opponent_bet_for_round) / last_pot
                        opponent_bet_for_round = self_bet_for_round
                    elif isinstance(play[1], Check):
                        opponent_bet_strength = 0.0
                    elif isinstance(play[1], Show):
                        #print play[1].hand
                        #print last_board
                        opponent_hand_strength_preflop = HandEvaluator.evaluate_hand(
                            play[1].hand, [])
                        opponent_hand_strength_flop = HandEvaluator.evaluate_hand(
                            play[1].hand, last_board[0:3])
                        opponent_hand_strength_turn = HandEvaluator.evaluate_hand(
                            play[1].hand, last_board[0:4])
                        opponent_hand_strength_river = HandEvaluator.evaluate_hand(
                            play[1].hand, last_board)

                        opponent_hand_strength = [
                            opponent_hand_strength_preflop,
                            opponent_hand_strength_flop,
                            opponent_hand_strength_turn,
                            opponent_hand_strength_river
                        ]
                        opponent_bet_strength = [
                            opponent_bet_strength_preflop,
                            opponent_bet_strength_flop,
                            opponent_bet_strength_turn,
                            opponent_bet_strength_river
                        ]
                        for i in xrange(0, 4):
                            for j in xrange(0, len(opponent_bet_strength[i])):
                                self.opponent_showdown_hand_strength.append(
                                    opponent_hand_strength[i])
                                self.opponent_showdown_bet_strength.append(
                                    opponent_bet_strength[i][j])
                                if len(self.opponent_showdown_hand_strength
                                       ) > self.p6:
                                    self.opponent_showdown_hand_strength = self.opponent_showdown_hand_strength[
                                        -self.p6:]
                                    self.opponent_showdown_bet_strength = self.opponent_showdown_bet_strength[
                                        -self.p6:]
                                degree = 1
                                if len(self.opponent_showdown_hand_strength
                                       ) > self.p6 / 2:
                                    self.coeff = polyfit(
                                        self.opponent_showdown_hand_strength,
                                        self.opponent_showdown_bet_strength,
                                        degree)
                                    self.corr = corrcoef(
                                        self.opponent_showdown_hand_strength,
                                        self.opponent_showdown_bet_strength)[0,
                                                                             1]
                                    # store first coefficient, which estimate's opponent's A
                                    self.opponent_showdown_potodds_estimate = self.coeff[
                                        0]
                                else:
                                    self.corr = 0

                    if isinstance(play[1], Bet) or isinstance(play[1], Raise):
                        if street == 'preflop':
                            opponent_bet_strength_preflop.append(
                                opponent_bet_strength)
                        elif street == 'flop':
                            opponent_bet_strength_flop.append(
                                opponent_bet_strength)
                        elif street == 'turn':
                            opponent_bet_strength_turn.append(
                                opponent_bet_strength)
                        elif street == 'river':
                            opponent_bet_strength_river.append(
                                opponent_bet_strength)

                    self.opponent_bet_history = append(
                        self.opponent_bet_history, strength_of_bet)

                elif play[0] == 'Dealer':

                    self_bet_for_round = 0
                    opponent_bet_for_round = 0

                    if len(play[1].cards) == 12:
                        street = 'flop'
                    elif len(play[1].cards) == 16:
                        street = 'turn'
                    elif len(play[1].cards) == 20:
                        street = 'river'

                        card_string = play[1].cards
                        last_board_string = play[1].cards
                        last_board = []
                        for i in xrange(0, 5):
                            str_pos = 4 * i + 1
                            if card_string[str_pos] == 'A':
                                card_rank = 14
                            elif card_string[str_pos] == 'K':
                                card_rank = 13
                            elif card_string[str_pos] == 'Q':
                                card_rank = 12
                            elif card_string[str_pos] == 'J':
                                card_rank = 11
                            elif card_string[str_pos] == 'T':
                                card_rank = 10
                            else:
                                card_rank = int(card_string[str_pos])
                            str_pos = 4 * i + 2
                            if card_string[str_pos] == 's':
                                card_suit = 1
                            elif card_string[str_pos] == 'h':
                                card_suit = 2
                            elif card_string[str_pos] == 'd':
                                card_suit = 3
                            elif card_string[str_pos] == 'c':
                                card_suit = 4
                            last_board.append(Card(card_rank, card_suit))
Ejemplo n.º 10
0
def write_even_xors_to_odd_xors_to_rank():  # 5-1, 3-2, 1-3, 1-1
    result = defaultdict(dict)
    # choose six ranks, frequencies (1,1,1,1,1,2))
    for combination in combinations(xrange(13), 6):
        # Choose each one to appear twice
        # Should be 6 * 13 choose 6 = 10296
        combo = list(combination)
        for i in xrange(6):
            rank = combo[i]
            other_ranks = combo[0:i] + combo[i + 1:6]

            even_xor = 1 << rank
            odd_xor = reduce(__or__, map(lambda rank: 1 << rank, other_ranks))

            # now generate the hand we need
            hand = [
                Card(rank + 2, 1),
                Card(rank + 2, 2),
                Card(other_ranks[0] + 2, 3),
                Card(other_ranks[1] + 2, 4),
                Card(other_ranks[2] + 2, 1),
                Card(other_ranks[3] + 2, 2),
                Card(other_ranks[4] + 2, 3),
            ]
            hand_combinations = combinations(hand, 5)
            # now map the product to the 5-card hand rank
            result[even_xor][odd_xor] = min(
                map(HandEvaluator.Five.evaluate_rank, hand_combinations))

    # choose five ranks, frequencies (1,1,1,2,2)
    for combination in combinations(xrange(13), 5):
        # Now choose every possible pair from those five to appear twice
        # Should have (5 choose 2) * (13 choose 5) = 12870
        combo = list(combination)
        for pair_ranks in combinations(combo, 2):
            one_ranks = filter(lambda rank: rank not in pair_ranks, combo)

            even_xor = reduce(__or__, map(lambda rank: 1 << rank, pair_ranks))
            odd_xor = reduce(__or__, map(lambda rank: 1 << rank, one_ranks))

            hand = [
                Card(pair_ranks[0] + 2, 1),
                Card(pair_ranks[0] + 2, 2),
                Card(pair_ranks[1] + 2, 3),
                Card(pair_ranks[1] + 2, 4),
                Card(one_ranks[0] + 2, 1),
                Card(one_ranks[1] + 2, 2),
                Card(one_ranks[2] + 2, 3)
            ]
            hand_combinations = combinations(hand, 5)
            result[even_xor][odd_xor] = min(
                map(HandEvaluator.Five.evaluate_rank, hand_combinations))

    # choose four ranks, frequencies (1,2,2,2)
    for combination in combinations(xrange(13), 4):
        # Now choose every possible rank to be the 1
        # Should have 4 * 13 choose 4 = 2860
        combo = list(combination)
        for i in xrange(4):
            rank = combo[i]
            other_ranks = combo[0:i] + combo[i + 1:4]

            even_xor = reduce(__or__, map(lambda rank: 1 << rank, other_ranks))
            odd_xor = 1 << rank

            # now generate the hand we need
            hand = [
                Card(rank + 2, 1),
                Card(other_ranks[0] + 2, 2),
                Card(other_ranks[0] + 2, 3),
                Card(other_ranks[1] + 2, 4),
                Card(other_ranks[1] + 2, 1),
                Card(other_ranks[2] + 2, 2),
                Card(other_ranks[2] + 2, 3),
            ]
            hand_combinations = combinations(hand, 5)
            # now map the product to the 5-card hand rank
            result[even_xor][odd_xor] = min(
                map(HandEvaluator.Five.evaluate_rank, hand_combinations))

    # choose two ranks, frequencies (3,4)
    for combination in combinations(xrange(13), 2):
        # Both can be the 3
        # Should have 2 * 13 choose 2 = 156
        combo = list(combination)
        for i in xrange(2):
            three = combo[i]
            four = combo[1 - i]

            even_xor = 1 << four
            odd_xor = 1 << three

            hand = [
                Card(three + 2, 1),
                Card(three + 2, 2),
                Card(three + 2, 3),
                Card(four + 2, 1),
                Card(four + 2, 2),
                Card(four + 2, 3),
                Card(four + 2, 4)
            ]
            hand_combinations = combinations(hand, 5)
            result[even_xor][odd_xor] = min(
                map(HandEvaluator.Five.evaluate_rank, hand_combinations))

    # this assertion is more complex
    # First, count the even_xor keys
    assert len(result) == 13 + 78 + 286
    # Then count the leaves
    assert reduce(add, map(lambda even_xor: len(result[even_xor]),
                           result)) == 10296 + 12870 + 2860 + 156
    fh = open("seven.even_xors_to_odd_xors_rank.py", "w")
    fh.write("even_xors_to_odd_xors_to_rank = {\n")
    for even_xor in result.keys():
        fh.write("%s: { " % (even_xor, ))
        fh.write(",\n".join(
            map(
                lambda chunk: ", ".join(
                    map(
                        lambda odd_xor: "%s: %s" %
                        (odd_xor, result[even_xor][odd_xor]), chunk)),
                chunker(result[even_xor].keys(), 4))))
        fh.write(" },\n")
    fh.write("}")
    fh.close()
Ejemplo n.º 11
0
def write_prime_products_to_rank():  # 5-0, 3-0, 3-1, 1-2
    """
    Write a table mapping products of 6 ranks' corresponding prime
    numbers to hand rank. Only do it for hands in certain combinations,
    though, or the table gets really big.
    """
    result = {}

    # Choose five ranks, frequency (1,1,1,1,3)
    for combination in combinations(xrange(13), 5):
        # Choose each one to appear three times
        # Should be 5 * 13 choose 5 = 6435
        combo = list(combination)
        for i in xrange(5):
            rank = combo[i]
            other_ranks = combo[0:i] + combo[i + 1:5]
            prime_for_rank = LookupTables.primes[rank]

            # first get the product we need
            product = prime_for_rank * prime_for_rank * prime_for_rank * \
                reduce(mul,
                    map(lambda rank: LookupTables.primes[rank], other_ranks))

            # now generate the hand we need
            hand = [Card(rank + 2, 1), Card(rank + 2, 2), Card(rank + 2, 3)]
            hand = hand + map(lambda rank: Card(rank + 2, 4), other_ranks)
            hand_combinations = combinations(hand, 5)
            # now map the product to the 5-card hand rank
            result[product] = min(
                map(HandEvaluator.Five.evaluate_rank, hand_combinations))

    # Choose three cards, frequencies (1,3,3), (1,2,4), or (3,2,2)
    for combination in combinations(xrange(13), 3):
        combo = list(combination)
        # Choose each one to appear once (1,3,3)
        # Same one can be the 3 in (3,2,2)
        # Should be 2 * 3 * 13 choose 3 = 1716
        for i in xrange(3):
            rank = combo[i]
            other_ranks = combo[0:i] + combo[i + 1:3]
            prime_for_rank = LookupTables.primes[rank]

            # (1,3,3)
            product = prime_for_rank * reduce(
                mul,
                map(
                    lambda rank: LookupTables.primes[rank] * LookupTables.
                    primes[rank] * LookupTables.primes[rank], other_ranks))

            hand = [
                Card(rank + 2, 1),
                Card(other_ranks[0] + 2, 2),
                Card(other_ranks[0] + 2, 3),
                Card(other_ranks[0] + 2, 4),
                Card(other_ranks[1] + 2, 1),
                Card(other_ranks[1] + 2, 2),
                Card(other_ranks[1] + 2, 3)
            ]
            hand_combinations = combinations(hand, 5)
            result[product] = min(
                map(HandEvaluator.Five.evaluate_rank, hand_combinations))

            # (3,2,2)
            product = prime_for_rank * prime_for_rank * prime_for_rank * reduce(
                mul,
                map(
                    lambda rank: LookupTables.primes[rank] * LookupTables.
                    primes[rank], other_ranks))
            hand = [
                Card(rank + 2, 1),
                Card(rank + 2, 2),
                Card(rank + 2, 3),
                Card(other_ranks[0] + 2, 1),
                Card(other_ranks[0] + 2, 2),
                Card(other_ranks[1] + 2, 3),
                Card(other_ranks[1] + 2, 4)
            ]
            hand_combinations = combinations(hand, 5)
            result[product] = min(
                map(HandEvaluator.Five.evaluate_rank, hand_combinations))

        # Permute each combination (1,2,4)
        # Should be 6 * 13 choose 3 = 1716
        for permute in permutations(combination):
            one = permute[0]
            two = permute[1]
            four = permute[2]

            product = LookupTables.primes[one] *\
                LookupTables.primes[two] * LookupTables.primes[two] *\
                LookupTables.primes[four] * LookupTables.primes[four] *\
                LookupTables.primes[four] * LookupTables.primes[four]
            hand = [
                Card(one + 2, 1),
                Card(two + 2, 2),
                Card(two + 2, 3),
                Card(four + 2, 1),
                Card(four + 2, 2),
                Card(four + 2, 3),
                Card(four + 2, 4)
            ]
            hand_combinations = combinations(hand, 5)
            result[product] = min(
                map(HandEvaluator.Five.evaluate_rank, hand_combinations))

    # Choose four ranks, frequencies (1,1,1,4) and (1,1,3,2)
    for combination in combinations(xrange(13), 4):
        combo = list(combination)
        # Choose one to be the 4
        # This one is also the 3
        for i in xrange(4):
            # Deal with (1,1,1,4)
            # Should have 4 * 13 choose 4 = 2860
            rank = combo[i]
            other_ranks = combo[0:i] + combo[i + 1:4]
            prime_for_rank = LookupTables.primes[rank]

            product = prime_for_rank * prime_for_rank *\
                prime_for_rank * prime_for_rank *\
                reduce(mul, map(lambda rank: LookupTables.primes[rank], other_ranks))
            hand = [
                Card(rank + 2, 1),
                Card(rank + 2, 2),
                Card(rank + 2, 3),
                Card(rank + 2, 4)
            ]
            hand = hand + map(lambda rank: Card(rank + 2, 1), other_ranks)
            hand_combinations = combinations(hand, 5)
            result[product] = min(
                map(HandEvaluator.Five.evaluate_rank, hand_combinations))

            # Use the 4 as the 3 now
            # Now pick another to be the 2 to do (1,1,3,2)
            # Should have 4 * 3 * 13 choose 4 = 8580
            three = rank
            for j in xrange(3):
                two = other_ranks[j]
                ones = other_ranks[0:j] + other_ranks[j + 1:3]

                product = LookupTables.primes[three] * LookupTables.primes[three] * LookupTables.primes[three] *\
                    LookupTables.primes[two] * LookupTables.primes[two] *\
                    LookupTables.primes[ones[0]] * LookupTables.primes[ones[1]]
                hand = [
                    Card(three + 2, 1),
                    Card(three + 2, 2),
                    Card(three + 2, 3),
                    Card(two + 2, 1),
                    Card(two + 2, 2),
                    Card(ones[0] + 2, 1),
                    Card(ones[1] + 2, 2)
                ]
                hand_combinations = combinations(hand, 5)
                result[product] = min(
                    map(HandEvaluator.Five.evaluate_rank, hand_combinations))

    # Choose two ranks, frequencies (2,4)
    for combination in combinations(xrange(13), 2):
        combo = list(combination)
        # Each needs to be the two once
        # Should have 2 * 13 choose 2 = 156
        for permute in permutations(combo):
            # permute contains the ranks in each order
            primes = map(lambda rank: LookupTables.primes[rank], permute)
            product = primes[0] * primes[0] * primes[1] * primes[1] * primes[
                1] * primes[1]
            hand = [
                Card(permute[0] + 2, 1),
                Card(permute[0] + 2, 2),
                Card(permute[1] + 2, 3),
                Card(permute[1] + 2, 4),
                Card(permute[1] + 2, 1),
                Card(permute[1] + 2, 2)
            ]
            hand_combinations = combinations(hand, 5)
            result[product] = min(
                map(HandEvaluator.Five.evaluate_rank, hand_combinations))

    # at the end, we should have 2860 + 858 + 1716 + 156 keys in the table
    assert len(result) == (6435 + 1716 + 1716 + 2860 + 8580 + 156)
    fh = open("seven.prime_products_to_rank.py", "w")
    fh.write("prime_products_to_rank = {\n")
    fh.write(",\n".join(
        map(
            lambda chunk: ", ".join(
                map(lambda k: "%s: %s" % (k, result[k]), chunk)),
            chunker(result.keys(), 4))))
    fh.write("}")
    fh.close()
Ejemplo n.º 12
0
import sys
sys.path.append("../")
from pokerbots.engine.game import Card
from pokerbots.player.hand_evaluator import HandEvaluator
from itertools import combinations
import random

deck = [
    Card(2,1), Card(2,2), Card(2,3), Card(2,4),
    Card(3,1), Card(3,2), Card(3,3), Card(3,4),
    Card(4,1), Card(4,2), Card(4,3), Card(4,4),
    Card(5,1), Card(5,2), Card(5,3), Card(5,4),
    Card(6,1), Card(6,2), Card(6,3), Card(6,4),
    Card(7,1), Card(7,2), Card(7,3), Card(7,4),
    Card(8,1), Card(8,2), Card(8,3), Card(8,4),
    Card(9,1), Card(9,2), Card(9,3), Card(9,4),
    Card(10,1), Card(10,2), Card(10,3), Card(10,4),
    Card(11,1), Card(11,2), Card(11,3), Card(11,4),
    Card(12,1), Card(12,2), Card(12,3), Card(12,4),
    Card(13,1), Card(13,2), Card(13,3), Card(13,4),
    Card(14,1), Card(14,2), Card(14,3), Card(14,4)
]

number_of_runs = 1000

def eval_6_with_5():
    for i in xrange(number_of_runs):
        turn = random.sample(deck, 6)
        possible_hands = combinations(turn, 5)
        rank = min(map(HandEvaluator.Five.evaluate_rank, possible_hands))
Ejemplo n.º 13
0
# This is just a file to test the lookup bot
# primes = [2,3,5,7,11,13,17,19,23,29,31,37,41]
import sys
sys.path.append("../")
from pokerbots.engine.game import Card
from pokerbots.player.hand_evaluator import HandEvaluator
from itertools import combinations

# 2 card hands
hands_2 = {
    "7-2": [Card(2, 1), Card(7, 2)],
    "7-2 suited": [Card(2, 1), Card(7, 1)],
    "9-8 suited": [Card(8, 1), Card(9, 1)],
    "6-6": [Card(6, 1), Card(6, 2)],
    "10-10": [Card(10, 2), Card(10, 3)],
    "A-K": [Card(14, 3), Card(13, 3)],
    "big slick": [Card(14, 3), Card(13, 3)],
    "rockets": [Card(14, 3), Card(14, 2)]
}

# 5 card hands
hands_5 = {
    "straight flush":
    [Card(14, 1),
     Card(13, 1),
     Card(12, 1),
     Card(11, 1),
     Card(10, 1)],
    "quads": [Card(7, 1),
              Card(7, 2),
              Card(7, 3),