SearchTree

AuthorRonnyBoy
Submission date2019-02-21 10:41:56.812625
Rating4405
Matches played245
Win rate42.86

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

Source code:

bfs_dfs = 0      #BFS =0, DFS = 1

class searchTreeNode:

    def __init__(self, name_, levels_):  # levels is the depth

        self.name = name_
        self.levels = levels_

        if self.levels > 0:
            if self.name != "Start":
                self.left = searchTreeNode(self.name + "R", self.levels - 1)
                self.middle = searchTreeNode(self.name + "P", self.levels - 1)
                self.right = searchTreeNode(self.name + "S", self.levels - 1)
            else:
                self.left = searchTreeNode("R", self.levels - 1)
                self.middle = searchTreeNode("P", self.levels - 1)
                self.right = searchTreeNode("S", self.levels - 1)
            #print "Nodes created"

        #print "Node created"
        #print self.name

    def bfs(self, startNode):
        returnSeq = []
        searchQueue = []
        searchQueue.append(startNode)
        currentNode = startNode

        while searchQueue:
            currentNode = searchQueue.pop(0)
            if currentNode.levels != 0:
                searchQueue.append(currentNode.left)
                returnSeq.append(currentNode.left.name)
                searchQueue.append(currentNode.middle)
                returnSeq.append(currentNode.middle.name)
                searchQueue.append(currentNode.right)
                returnSeq.append(currentNode.right.name)

        return returnSeq


    def dfs(self, startNode, returnList):
        if startNode.levels != 0:
            returnList.append(startNode.left.name)
            self.dfs(startNode.left, returnList)
            returnList.append(startNode.middle.name)
            self.dfs(startNode.middle, returnList)
            returnList.append(startNode.right.name)
            self.dfs(startNode.right, returnList)
        return returnList

    def search(self, startNode):
        if bfs_dfs == 0:
            return self.bfs(startNode)
        else:
            returnSeq = []
            return self.dfs(startNode, returnSeq)



if input == "":
    root = searchTreeNode("Start", 5)

    toPlay = ""
    previousPlayed = input
    possibleNode = 0
    #previousPlayNode = root.name
    #currentPlayNode = root.left.name

    toPlayTokens = root.search(root)

    '''while toPlayTokens:
        toPlay += toPlayTokens.pop(0)

    #print toPlay
    print len(toPlay)'''
    count = 0
    buffer = toPlayTokens[0]
    output = buffer[0]
    buffer = buffer[1:]
    breakFound = False


if input != "":
    if previousPlayed == input:
        #print "Hit!!!!!!!!!!!!!!!!!!"
        possibleNode = count
        #print "Getting a hit on " + str(count) + " which is " + toPlayTokens[count]
        attackMode = True
        #count = count -1
        breakFound = True
    else:
        attackMode = False
    previousPlayed = input


    if attackMode:
        #print "Attacking!!!!!!!!!!!!!"
        if previousPlayed == "R":
            output = "P"
        elif previousPlayed == "S":
            output = "R"
        elif previousPlayed == "P":
            output = "S"
    else:
        if breakFound:
            buffer = toPlayTokens[possibleNode]
            count = possibleNode
            output = buffer[0]
            buffer = buffer[1:]
            breakFound = False
        else:
            if len(buffer) == 0:
                count += 1
                if count >= len(toPlayTokens):
                    count = 0
                buffer = toPlayTokens[count]
                output = buffer[0]
                buffer = buffer[1:]
            else:
                output = buffer[0]
                buffer = buffer[1:]

    #print output