HashSkip3

AuthorSean
Submission date2018-08-31 05:13:57.764430
Rating6798
Matches played274
Win rate68.25

Use rpsrunner.py to play unranked matches on your computer.

Source code:

if input == "":
    
    import collections
    import random

    gamma = random.gammavariate
    hash_code = [random.randint(0, 0xFFFFFFFF) for _ in range(27)]
    table = {}
    names = ("R", "P", "S")
    index = {"R": 0, "P": 1, "S": 2}
    history = collections.deque([])
    def toggle(h, i):
        return h ^ hash_code[3 * i + history[i]]
    best_move = random.randrange(3)
    output = random.choice(names)
    rot_table = [[1.] * 3 for _ in range(3)]
    rot = random.randrange(3)
    prev_rot = random.randrange(3)
    ctxts = []
else:
    inp = index[input]
    rot_table[prev_rot][(inp - best_move) % 3] += 1
    out = index[output]
    for ctxt in ctxts:
        ctxt[inp] += 1
    history.appendleft(inp)
    history.appendleft(out)
    stats = [0] * 3
    max_score = 0
    best_move = 0
    m = min(len(history), 7)
    ctxts = []
    def check(h):
        if h in table:
            ctxt = table[h]
            global max_score, best_move
            counts = [gamma(n, 1) for n in ctxt]
            scores = [counts[(i + 2) % 3] - counts[(i + 1) % 3] for i in range(3)]
            best_score = max(scores) / sum(counts)
            if best_score >= max_score:
                max_score = best_score
                best_move = scores.index(max(scores))
        else:
            table[h] = ctxt = [1.] * 3
        ctxts.append(ctxt)
    h = 0
    check(h)
    for i in range(m):
        h = toggle(h, i)
        check(h)
        for j in range(i):
            h1 = toggle(h, j)
            check(h1)
            for k in range(j):
                h2 = toggle(h1, k)
                check(h2)
            rot_context = rot_table[rot]
    rot_counts = [gamma(n, 1) for n in rot_context]
    rot_scores = [rot_counts[(i + 2) % 3] - rot_counts[(i + 1) % 3] for i in range(3)]
    prev_rot = rot
    rot = rot_scores.index(max(rot_scores))
    output = names[(best_move + rot) % 3]