Author | ADL |
Submission date | 2019-11-25 12:44:51.803006 |
Rating | 6163 |
Matches played | 206 |
Win rate | 60.68 |
Use rpsrunner.py to play unranked matches on your computer.
if input=='':
import random as rnd
from collections import defaultdict
toth=''
currenthist=''
pl=1
# Convert the input and the output to numbers
convdic = {'R':0,'P':1,'S':2}
# The inverse function as a list
invconv=('R','P','S')
output=rnd.choice(invconv)
backsteps=7 #number of steps back
countboth=defaultdict(lambda:[0,0,0])
def decision(hist):
counts=countboth[hist]
srtcounts=enumerate(counts)
minpos=[i for i,j in srtcounts if j==min(counts)]
if len(minpos)==3:
return rnd.choice(invconv)
elif len(minpos)==2:
counts[minpos[0]]+=1./2.
counts[minpos[1]]+=1./2.
else:
counts[minpos[0]]+=1
gain=[counts[2]-counts[1],counts[0]-counts[2],counts[1]-counts[0]]
maxgainlist=[i for i,j in enumerate(gain) if j==max(gain)]
return invconv[maxgainlist[rnd.randint(0,len(maxgainlist)-1)]]
else:
if pl<=backsteps:
pl+=1
toth+=output+input
output=rnd.choice(invconv)
else:
# choose the output before receiving the input
toth+=output+input
both=toth[-2*backsteps-2:-2]
countboth[both][convdic[toth[-1]]] +=1
output = decision(toth[-2*backsteps:])