NonRepeatReverse

AuthorJustinF
Submission date2011-06-15 06:33:50.012998
Rating4372
Matches played5107
Win rate46.52

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

Source code:

import random

def countBest(history):
  best = [[0, 'R'],[0, 'P'],[0, 'S']]
  for x in xrange(len(history)-1, -1, -1):
    key = history[x:]
    countRock = history.count(key + 'R')
    countScissor = history.count(key + 'P')
    countPaper = history.count(key + 'S')
    if countRock + countScissor + countPaper == 0:
      break
    else:
      best[0][0] += countRock
      best[1][0] += countScissor
      best[2][0] += countPaper
  return best

if not input:
  output = 'P'
  stack = ''
else:
  choices = sorted(countBest(stack))
  if choices[0][0] == choices[1][0] == choices[2][0]:
    output = random.choice(('R', 'P', 'S'))
  else:
    output = {'R': 'S', 'P': 'R', 'S': 'P'}[choices[2][1]]

stack += output