SequenceFinder17

AuthorPietHugo
Submission date2019-02-14 16:00:25.513787
Rating3994
Matches played254
Win rate38.58

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

Source code:

depth = 2

from Queue import * 
                                    
class node:                                                 
    def __init__(self, name, played):
        self.name = name
        self.played = played
        
def populatechildren(l1):                                   
        l1.children=[node("R", 0), node("P", 0), node("S", 0)] 
        for x in range(3):
            l1.children[x].parent=l1
            l1.children[x].layer=l1.layer + 1

def populatetree(start) :
    if start.layer<depth :
        populatechildren(start)
        for x in range(3):
            populatetree(start.children[x])

def putparents(childnode):
    helper=childnode
    Stack=LifoQueue()
    for x in range(childnode.layer-1):
        Stack.put(helper.parent)
        helper=helper.parent
    for x in range(childnode.layer-1):
        q.put(Stack.get())
        

def putchildren(nde):
    if (nde.layer<depth):
        for x in range(3):
            putparents(nde.children[x])
            q.put(nde.children[x])

def setzero():
    if(q.qsize()!=0):
        temp=q.get()
        temp.played=0
        putchildrenwithoutparent(temp)
        setzero()
"""
def putchildrenwithoutparent(nde):
    if (nde.layer<depth):
        for x in range(3):
            q.put(nde.children[x])
"""            
def putbrokensequence():
    del brokensequence[:]
    for x in range(len(previoussequence)):
        brokensequence.append(previoussequence[x])
    
def putprevioussequence(nde):
    putbrokensequence()
    del previoussequence[:]
    
    helper=nde
    Stack=LifoQueue()
    for x in range(nde.layer-1):
        Stack.put(helper.parent)
        helper=helper.parent
    for x in range(nde.layer-1):
        previoussequence.append(Stack.get().name) 
    previoussequence.append(nde.name)
        
##############################################################################  
##############################################################################
        

if input == "":       
    tree = node("Start", 0)
    tree.layer = 0
    populatetree(tree)
    q=Queue()
    q.put(tree)
    temp=q.get()
    putchildren(temp)
    previousinput=input
    broken=0
    brokensequence=[]
    previoussequence=[]
    index=0

    
if previousinput==input:
    broken=1
    index=0
    if previousinput=="R":
        output="P"
    elif previousinput=="P":
        output="S"
    else:
        output="R"  
else: 
    if broken==1:
        if len(brokensequence)==index:
            broken=0
            output="S"
        else:
            output=brokensequence[index]
            index+=1
    else:
        
        if(q.qsize()==0):
            q.put(tree)
            temp=q.get()
            putchildren(temp)
            #setzero()
             
        temp=q.get()
        if temp.played==0:
            temp.played=1
            putprevioussequence(temp)
            putchildren(temp)
        
        output=temp.name
    
previousinput=input