# secondtry

 Author ADL Submission date 2019-09-18 21:12:45.822734 Rating 4915 Matches played 210 Win rate 49.52

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

## Source code:

``````import random as rnd

# Convert the input and the output to numbers
#
def conv(play):
if play.upper()=='R':
return 0
elif play.upper()=='P':
return 1
elif play.upper()=='S':
return 2
else:
return "NAN"

# The inverse function as a list
invconv=['R','P','S']

#  beats[X] returns the move that beats X
beats=[1,2,0]
isbeatenby=[2,0,1]

if input=='':
inhistory=[]
outhistory=[]
else:
inhistory.append(conv(input))
outhistory.append(conv(output))

prob0=[1/3,1/3,1/3]
prob1=[0.,0.,0.]
prob11=[[0.,0.,0.],[0.,0.,0.],[0.,0.,0.]]
# prob(X) estimates the probability of playing RPS assuming
# the opponent does not use previous info;
# prob1(X|Y) estimates the probability of X conditional on the previous play being Y
# prob11(X|Y,Z) estimates the probability of X conditional on the previous play of the opponent being Y and our previous play being Z
prob = prob0
for j in range(3):
prob1[j]=prob0
for k in range(3):
prob11[j][k]=prob0

def decision(inh,outh):
global prob, prob1, prob11, pl
def win(a,b):
if a==beats[b]:
return 1
elif b==beats[a]:
return -1
elif b==a:
return 0

pl=len(inh)+1
if pl<3:
return invconv[rnd.randint(0,2)]

count=[0,0,0]
count1=[0,0,0]
count11=[0,0,0]
current1=inh[pl-2] # the last play of the op
current11=outh[pl-2] # the last play of the bot
for n,k in enumerate(inh[:-1]):
count[k]=count[k]+1 # just count the previous occurrences, disregarding correlations
if k==current1:
nextpl=inh[n+1]
count1[nextpl]=count1[nextpl]+1 #counts the occurrences of a play,
#given that the previous play of the op equals the current play
if current11==outh[n]:
count11[nextpl]=count11[nextpl]+1

count[current1]=count[current1]+1 # adds the last play to the unconditional count

prob=[(count+1)/(len(inh)+3),(count+1)/(len(inh)+3),(count+1)/(len(inh)+3)]
expectedgain=[prob*win(i,0)+prob*win(i,1)+prob*win(i,2) for i in range(3)]
maxlist=[i for i,j in enumerate(expectedgain) if j==max(expectedgain)]
move=maxlist[rnd.randint(0,len(maxlist)-1)]
# The next move based on the unconditional freq,. of previous plays by the opp.

prob1[current1]=[(count1+1)/(count1+count1+count1+3),\
(count1+1)/(count1+count1+count1+3),(count1+1)/(count1+count1+count1+3)]
expectedgain1=[prob1[current1]*win(i,0)+prob1[current1]*win(i,1)+prob1[current1]*win(i,2) for i in range(3)]
maxlist1=[i for i,j in enumerate(expectedgain1) if j==max(expectedgain1)]
move1=maxlist1[rnd.randint(0,len(maxlist1)-1)]

prob11[current1][current11]=[(count11+1)/(count11+count11+count11+3),\
(count11+1)/(count11+count11+count11+3),(count11+1)/(count11+count11+count11+3)]
expectedgain11=[prob11[current1][current11]*win(i,0)+prob11[current1][current11]*win(i,1)+prob11[current1][current11]*win(i,2) for i in range(3)]
maxlist11=[i for i,j in enumerate(expectedgain11) if j==max(expectedgain11)]
move11=maxlist11[rnd.randint(0,len(maxlist11)-1)]

movelist=[move,move1,move11]
maxmax=max([max(expectedgain),max(expectedgain1),max(expectedgain11)])
maxpos=[i for i,j in enumerate([max(expectedgain),max(expectedgain1),max(expectedgain11)]) if j==maxmax]

return invconv[movelist[maxpos]]

output=decision(inhistory,outhistory)``````