bayes12

Authorpyfex
Submission date2011-07-05 12:32:40.295010
Rating6341
Matches played4271
Win rate66.21

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

Source code:

# See http://overview.cc/RockPaperScissors for more information about rock, paper, scissors
# bayes 11 extension. Add strategies, which play according to the last chosen strategy by
# the opponent and the current score.

from collections import defaultdict
import operator
import random

if input == "":
  score = {'RR': 0, 'PP': 0, 'SS': 0, 'PR': 1, 'RS': 1, 'SP': 1,'RP': -1, 'SR': -1, 'PS': -1,}
  p_add = {'RR': 1, 'PP': 1, 'SS': 1, 'PR': 0, 'RS': 0, 'SP': 0,'RP': 0, 'SR': 0, 'PS': 0,}
  beat = {'P': 'S', 'S': 'R', 'R': 'P'}
  cede = {'P': 'R', 'S': 'P', 'R': 'S'}
  rps = ['R', 'P', 'S']
  
  def counter_prob(probs):
    weighted_list = []
    for h in ['R', 'P', 'S']:
      weighted = 0
      for p in probs.keys():
        points = score[h+p]
        prob = probs[p]
        weighted += points * prob
        weighted_list.append((h, weighted))

    return max(weighted_list, key=operator.itemgetter(1))[0]

  patterndict = defaultdict(str)
  played_probs = defaultdict(lambda: 1)

  prediction = []
  performance = [1] * 36

  hist = "" 
  my = opp = ""

  output = random.choice(["R", "P", "S"])

else:
  played_probs[input] += 1
  for i, p in enumerate(prediction):
    performance[i] += p_add[p+input]

  for length in range(min(14, len(hist)), 0, -2):
    pattern = patterndict[hist[-length:]]
    patterndict[hist[-length:]] += output + input

  hist += output + input
  my = opp = ""

  for length in range(min(14, len(hist)), 0, -2):
    pattern = patterndict[hist[-length:]]
    if pattern != "":
      my = pattern[-2]
      opp = pattern[-1]
      break

  probs = {}
  for hand in rps:
    probs[hand] = played_probs[hand]
        
  if my and opp:
    if prediction:
      played_strats = [i for i, p in enumerate(prediction) if p == input]
    else:
      played_strats = []
    prediction = [opp, beat[opp], cede[opp], my, cede[my], beat[my]]
    if len(played_strats) == 2:
      s1 = prediction[played_strats[0]]
      s2 = prediction[played_strats[1]]
      pred1 = [s1, s2, beat[s1], beat[s2], cede[s1], cede[s2]]
      pred2 = [beat[p] for p in pred1]
      pred3 = [cede[p] for p in pred1]
      sc = score[input+output]
      if sc == 1:
        pred4 = pred1
        pred5 = pred1
        pred6 = pred1
        pred7 = pred1
      elif sc == 0:
        pred4 = pred1
        pred5 = pred1
        pred6 = pred2
        pred7 = pred3
      elif sc == -1:
        pred4 = pred2
        pred5 = pred3
        pred6 = pred2
        pred7 = pred3
      prediction += pred1 + pred4 + pred5 + pred6 + pred7
    for hand in rps:
      probs[hand] *= reduce(operator.mul, [performance[i] for i, p in enumerate(prediction) if p == hand])
  else:
    prediction = []
                
  output = counter_prob(probs)