metameta v1.2

This program has been disqualified.


AuthorKyle Miller
Submission date2011-08-01 22:51:21.832149
Rating5706
Matches played117
Win rate57.26

Source code:

# metameta.py by Kyle Miller, 2011
# I never meta strategy I didn't like

import random

if not input :
    rps_to_num = {"R":0, "P":1, "S":2}
    num_to_rps = {0:"R", 1:"P", 2:"S"}

    def their_history_strategy(my_move, their_move, my_history, their_history) :
        """See if they've played these moves before, and mirror what
        they're going to play."""
        max_length = 0
        best_move = random.randint(0, 2)
        for i in xrange(max(0, len(their_history)-8), len(their_history)-1) :
            if their_history[i] == their_move :
                length = 1
                for j in xrange(0, i) :
                    if their_history[i-j-1] == their_history[-j-1] :
                        length += 1
                    else :
                        break
                if length >= max_length :
                    max_length = length
                    best_move = their_history[i+1]
        return best_move
    def our_history_strategy(my_move, their_move, my_history, their_history) :
        """See if they've played these moves before, and mirror what
        they're going to play."""
        max_length = 0
        best_move = random.randint(0, 2)
        for i in xrange(max(0, len(their_history)-8), len(their_history)-1) :
            if their_history[i] == their_move and my_history[i] == my_move :
                length = 1
                for j in xrange(0, i) :
                    if their_history[i-j-1] == their_history[-j-1] and my_history[i-j-1] == my_history[-j-1]:
                        length += 1
                    else :
                        break
                if length >= max_length :
                    max_length = length
                    best_move = their_history[i+1]
        return best_move

    last_guesses = None
    metastrategies = []
    def get_guesses(my_move, their_move, my_history, their_history) :
        guesses = [0,
                   their_move, my_move,
                   their_history_strategy(my_move, their_move, my_history, their_history),
                   their_history_strategy(their_move, my_move, their_history, my_history),
                   our_history_strategy(my_move, their_move, my_history, their_history),
                   our_history_strategy(their_move, my_move, their_history, my_history)]
        guesses2 = [(g+1)%3 for g in guesses]
        guesses3 = [(g+2)%3 for g in guesses]
        guesses.extend(guesses2)
        guesses.extend(guesses3)
        guesses.append(random.randint(0,2))
        return guesses

    metastrategy_scores = []
    m_last_guesses = []
    def add_metastrategy(m) :
        metastrategies.append(m)
        metastrategy_scores.append(0)
        m_last_guesses.append(None)

    def get_best(guesses, scores) :
        best_score = -1000
        best_guess = None
        for g, s in zip(guesses, scores) :
            if s > best_score :
                best_score = s
                best_guess = g
        return g

    class Metastrategy1 :
        def __init__(self) :
            self.scores = None
        def get_best_move(self, their_move, last_guesses, guesses) :
            if not self.scores :
                self.scores = [0 for g in guesses]
            scores = self.scores
            for i in xrange(0, len(last_guesses)) :
                if last_guesses[i] is not None :
                    if (last_guesses[i]-1)%3 == their_move : # got it!
                        scores[i] += 2
                    elif last_guesses[i] == their_move : # at least didn't lose...
                        scores[i] = max(scores[i]-1, 0)
                    else : # whoops...
                        scores[i] = scores[i]//2
            return get_best(guesses, scores)

    add_metastrategy(Metastrategy1())

    class MetastrategyLinearFalloff :
        def __init__(self) :
            self.scores = None
        def get_best_move(self, their_move, last_guesses, guesses) :
            if not self.scores :
                self.scores = [0 for g in guesses]
            scores = self.scores
            for i in xrange(0, len(guesses)) :
                if last_guesses[i] is not None :
                    if (last_guesses[i]-1)%3 == their_move : # got it!
                        scores[i] += 1
                    elif last_guesses[i] == their_move : # at least didn't lose...
                        pass
                    else : # whoops...
                        scores[i] = max(0, scores[i]-1)
            return get_best(guesses, scores)

    add_metastrategy(MetastrategyLinearFalloff())

    class MetastrategyTryThingsOnce :
        def __init__(self) :
            self.scores = None
            self.j = -1
        def get_best_move(self, their_move, last_guesses, guesses) :
            if not self.scores :
                self.scores = [[10,0] for g in guesses]
            scores = self.scores
            for i in xrange(0, len(guesses)) :
                if last_guesses[i] is not None :
                    if (last_guesses[i]-1)%3 == their_move : # got it!
                        scores[i][0] += 1
                        if i == self.j :
                            scores[i][1] += 1
                    elif last_guesses[i] == their_move : # at least didn't lose...
                        pass
                    else : # whoops...
                        scores[i][0] = 0
                        scores[i][1] = 0
                        if i == self.j :
                            self.j = -1
            return get_best(guesses, scores)

    add_metastrategy(MetastrategyTryThingsOnce())

    def get_best_move(my_move, their_move, my_history, their_history) :
        global last_guesses, m_last_guesses
        guesses = get_guesses(my_move, their_move, my_history, their_history)
        if not last_guesses :
            last_guesses = [None for g in guesses]
        m_guesses = []
        for i in xrange(0, len(metastrategies)) :
            m_guess = metastrategies[i].get_best_move(their_move, last_guesses, guesses)
            if m_last_guesses[i] is not None :
                if (m_last_guesses[i]-1)%3 == their_move : # got it!
                    metastrategy_scores[i] += 2
                elif m_last_guesses[i] == their_move : # tie
                    metastrategy_scores[i] = max(0, metastrategy_scores[i]-1)
                else :
                    metastrategy_scores[i] = metastrategy_scores[i]//2
            m_guesses.append(m_guess)
        last_guesses = guesses
        m_last_guesses = m_guesses
        to_try = zip(m_guesses, metastrategy_scores, xrange(0, len(metastrategies)))
        to_try.sort(key=lambda x : x[1])
        return to_try[-1][0]

    
    # at the beginning, we don't know what to do.  so be random!
    output = num_to_rps[random.randint(0, 2)]
    my_history = []
    their_history = []
else :
    my_move = rps_to_num[output]
    their_move = rps_to_num[input]
    output = num_to_rps[get_best_move(my_move, their_move, my_history, their_history)]
    my_history.append(my_move)
    their_history.append(their_move)