SkipSwitch9/3

This program has been disqualified.


AuthorSean
Submission date2016-02-22 14:54:08.129743
Rating6250
Matches played31
Win rate67.74

Source code:

if input == "":

    import collections
    import random
    gamma = random.gammavariate
    third = 1.0 / 3

    class MarkovTree:
        def __init__(self, counts = None):
            self.counts = [0 for _ in xrange(3)]
            self.children = None
            self.total = 0

        def select_move(self):
            r = self.counts[0] + third
            p = self.counts[1] + third
            s = self.counts[2] + third
            scores = [s - p, r - s, p - r]
            s = max(scores)
            return (s, scores.index(s))

        def update_helper(self, h, i, p, d, skips):
            stop = False
            for j in xrange(p, len(h)):
                k = h[j]
                self.counts[i] += 2
                self.total += 2
                if stop or d >= 9 or skips >= 3:
                    return
                d += 1
                if self.children is None:
                    self.children = [None for _ in xrange(4)]
                    self.children[3] = MarkovTree()
                if self.children[k] is None:
                    self.children[k] = MarkovTree()
                    stop = True
                self.children[3].update_helper(h, i, j + 1, d, skips + 1)
                self = self.children[k]

        def update(self, h, i):
            self.update_helper(h, i, 0, 0, 0)

        def predict_helper(self, h, p, s, m):
            for j in xrange(p, len(h)):
                k = h[j]
                s1, m1 = self.select_move()
                if s1 >= s:
                    s = s1
                    m = m1
                if self.children is None:
                    return (s, m)
                s, m = self.children[3].predict_helper(h, j + 1, s, m)
                child = self.children[k]
                if child is None:
                    return (s, m)
                self = child
            return (s, m)

        def predict(self, h):
            s = 0
            m = random.randrange(0, 3)
            s, m = self.predict_helper(h, 0, s, m)
            return m

    R, P, S = 0, 1, 2
    index = {"R": R, "P": P, "S": S}
    name = ("R", "P", "S")

    tree = MarkovTree()

    history = collections.deque([])

else:

    i = index[input]
    j = index[output]

    tree.update(history, i)
    history.appendleft(i)
    history.appendleft(j)

output = name[tree.predict(history)]