firsttry

AuthorMars
Submission date2014-10-11 12:56:16.582787
Rating3328
Matches played572
Win rate32.69

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

Source code:

import random
from collections import defaultdict

MAX_LENGTH = 20
RPS = "RPS"

def InputToInt(x):
  if x == "R":
    return 0
  elif x == "P":
    return 1
  elif x == "S":
    return 2

if input == "": # initialize variables for the first round
  o_history = ""
  o_moves = defaultdict(lambda: [0, 0, 0])
else:
  for i in range(2, min(len(o_history) + 1, MAX_LENGTH)):
    s = o_history[-i:]
    o_moves[s][InputToInt(input)] += 1
  
  o_history += input

best_confidence = 0
best_result = random.choice(["R","P","S"])
for i in range(2, min(len(o_history) + 1, MAX_LENGTH)):
  s = o_history[-i:]
  if s in o_moves:
    h = o_moves[s]
    for c in range(3):
      remaining = [h[x] for x in range(3) if x != c]
      assert len(remaining) == 2
      min_difference = min([h[c] - remaining[x] for x in range(2)])
      if min_difference < 0:
        continue
      confidence = (min_difference + 1) * i * i
      if confidence > best_confidence:
        best_confidence = confidence
        best_result = RPS[c]

output = best_result