firsttry_ADL

AuthorADL
Submission date2019-09-11 19:22:44.384404
Rating4601
Matches played229
Win rate44.1

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

Source code:

import random as rnd

prob0=[1/3,1/3,1/3]
prob1=[0.,0.,0.]
prob11=[[0.,0.,0.],[0.,0.,0.],[0.,0.,0.]]
# 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))


# 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

score=[0,0]
pl=1




def decision(inh,outh):
    if pl<2:
       return invconv[rnd.randint(0,2)]

    global prob, prob1, prob11
    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[0]+1)/(len(inh)+3),(count[1]+1)/(len(inh)+3),(count[2]+1)/(len(inh)+3)]
    expectedgain=[prob[0]*win(i,0)+prob[1]*win(i,1)+prob[2]*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[0]+1)/(count1[0]+count1[1]+count1[2]+3),\
         (count1[1]+1)/(count1[0]+count1[1]+count1[2]+3),(count1[2]+1)/(count1[0]+count1[1]+count1[2]+3)]
    expectedgain1=[prob1[current1][0]*win(i,0)+prob1[current1][1]*win(i,1)+prob1[current1][2]*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[0]+1)/(count11[0]+count11[1]+count11[2]+3),\
         (count11[1]+1)/(count11[0]+count11[1]+count11[2]+3),(count11[2]+1)/(count11[0]+count11[1]+count11[2]+3)]
    expectedgain11=[prob11[current1][current11][0]*win(i,0)+prob11[current1][current11][1]*win(i,1)+prob11[current1][current11][2]*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)]

    return invconv[move11]

# The first two rounds are random

output=decision(inhistory,outhistory)
pl=pl+1