dirk3

Authordirknbr
Submission date2019-03-16 20:12:44.793128
Rating4364
Matches played241
Win rate43.15

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

Source code:

# keep track 
# http://www.rpscontest.com/authorSearch?name=dirknbr

import random

rps = ['R', 'P', 'S']
best = {'R': 'P', 'P': 'S', 'S': 'R'}

hist_me = []
hist_opp = []
a = {'R': 1, 'P': 1, 'S': 1}
b = {'R': 1, 'P': 1, 'S': 1}

def choose(x, probs):
  # normalise
  s = sum(probs)
  probs = [i / s for i in probs]
  u = random.random()
  if u < probs[0]:
  	return 'R'
  elif u < sum(probs[0:2]):
  	return 'P'
  else:
  	return 'S'

def score(a, b):
  if a == b:
  	return -1
  ab = [a, b]
  if set(ab) == set(['R', 'P']):
  	return ab.index('P')
  if set(ab) == set(['R', 'S']):
  	return ab.index('R')
  if set(ab) == set(['P', 'S']):
  	return ab.index('S')

# input = 'R'
# hist_me = ['P']

if input == '' or len(hist_me) == 0:
  output = random.choice(rps)
else:
  hist_opp.append(input)
  lastscore = score(hist_me[-1], hist_opp[-1])
  if lastscore == 0: # win
    a[hist_me[-1]] += 1
  elif lastscore == 1: # lose
    b[hist_me[-1]] += 1
  probs = [random.betavariate(a[i], b[i]) for i in rps]
  output = choose(rps, probs)
  # print(probs, output)


hist_me.append(output)