fullbayesian

AuthorADL
Submission date2019-11-22 19:13:58.298972
Rating6755
Matches played211
Win rate67.77

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

Source code:

if input=='':
    import random as rnd
    import copy
    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=invconv[rnd.randint(0,2)]
    
 
    
    #  beats[X] returns the move that beats X
    beats=(1,2,0)
    isbeatenby=(2,0,1)
    
    
    backsteps=6     #number of steps back
    
    countboth=defaultdict(lambda:[0,0,0])

    
    def decision(hist):
        counts=countboth[hist]
    
        def win(a,b):
            if a==beats[b]:
                return 1
            elif b==beats[a]:
                return -1
            elif b==a:
                return 0
    
        
        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=invconv[rnd.randint(0,2)]
 
    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:])