level4

Authormiaou
Submission date2014-10-11 08:09:25.085707
Rating5771
Matches played545
Win rate57.8

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

Source code:

import random

beat = {"R": "P", "P": "S", "S": "R"}
rps = ["R", "P", "S"]    

if input == "":
  seq_in = ["R", "P", "S"]
  seq_out = ["R", "P", "S"]
  output = random.choice(["R", "P", "S"])
  seq_out.append(output)

  couples = ["RR", "RP", "RS", "PR", "PP", "PS", "SR", "SP", "SS"]
  predictors = dict(zip(couples, [[0, 0, 0] for k in couples]))
else:
  seq_in.append(input)
  if len(seq_in) < 2:
    output = random.choice(["R", "P", "S"])
    seq_out.append(output)
  else:
    predictors[seq_out[-2]+seq_in[-2]][rps.index(input)] += 1

    move_predictor = predictors[seq_out[-1]+input]
    best_prediction = rps[move_predictor.index(max(move_predictor))]

    output = beat[best_prediction]

    seq_out.append(output)