beat_nth

Authorjlb
Submission date2011-06-26 18:29:53.244058
Rating7071
Matches played4854
Win rate69.84

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

Source code:

# simple bot I used for testing purposes.

import random
HISTLEN=17
DECAY=0.87

beat = {"R": "P", "S": "R", "P": "S"}
# beat the move that was played n moves ago
if input == "": # initialize variables for the first round
    output = random.choice("RPS")
    history = []
    score = [-1] * HISTLEN
    predictor = [0] * HISTLEN
else:
    history.append(input)
    if len(history) > HISTLEN:
        active = HISTLEN
    else:
        active = len(history)

    # score predictors
    best = None
    best_idx = None
    for i,p in enumerate(predictor):
        if p != 0:
            score[i] *= DECAY
            if beat[input] == p:
                score[i] += 1
            else:
                score[i] -= 1
            if best is None or score[i] > best:
                best = score[i]
                best_idx = i

    # make predictions
    for i in range(active):
        if len(history) > i:
            nth = history[-1 * (i + 1)]
            predictor[i] = beat[nth]

    if best < 0:
        # fallback to randbeat
        output = beat[ random.choice(history) ]
    else:
        output = predictor[ best_idx ]