master 5

AuthorLefa
Submission date2019-02-18 11:04:04.884953
Rating2103
Matches played247
Win rate18.62

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

Source code:

#Imports
import random
#import queue



#Creating a node
#This node takes on a character object and array of nodes as arguments
class node(object):
    def __init__(self,object,children= [],path=""): #Creating a node with an emtpy array of children
        self.object=object
        self.children=children
        self.path=path
        
    def haschildren(self):
       return len(self.children)!=0
   
    def isleaf(self):
       return len(self.childre)==0
        
"""Task1: Create a BST that take a depth as and argument,
The tree must be recursively built
 """       

class tree(object): 
    def __init__(self):
        self.root=node("Start")
        #self.children=[]
        #self.insert1()
        
    def haschildren(self,currentnode):
       return currentnode.haschildren()
    
    def insert1(self,currentnode):
            
        index=0
        while index!=3 :
            if index==0:
                currentnode.children.append(node("R",[]))
                #self.children[0](depth-1)
            if index == 1:
                currentnode.children.append(node("P",[]))
            if index== 2:
                currentnode.children.append(node("S",[]))
            index +=1
        
    def _insert(self,currentnode):
        
        if currentnode.haschildren():
            i=0
            while i!=3:
                self._insert(currentnode.children[i])
                #print id(currentnode.children[i])
               # print i
                i+=1
        else: 
           self.insert1(currentnode)
          
           
#______________________________________________________________________        
#Task1

def buildtree(depth):
    tree1=tree()
    i=0
    while i<depth:
        tree1._insert(tree1.root);
        i+=1
    return tree1

#buildtree(3)
    
 #______________________________________________________________________       
 
    
#task2
"""Design a bfs and dfs alrogithm that will be used to transverse
and return the sequence of nodes it visits in the tree
Basically you must just keep track of of the nodes visited for each node.

I'm going to need a variable for visted and sequence(dynamic path) at least.

"""

def BFS(depth,paths="x"):
    tree= buildtree(depth)
    visited=[]
    visited_val=[]
    temp=tree.root
    queue=[temp]
    
    #Transverssal
    while  len(queue)!=0:
        node=queue.pop(0)
        #print id(node)
        
        if node in visited:
            print "visited"
            break
        else: 
            visited.append(node)
            
            # Add node to path
           # visited_val.append(path +node.object )
            #print id(node)
            if node.object == "Start":
                    node.path+=""
            else:
                    node.path+=node.object
                    visited_val.append(node.path ) 
            if node.haschildren(): #add variables to queue
                
                    
                i=0
                if(node.path==paths):
                       # print "found: " + paths
                        print visited_val
                        return visited_val
                
                while i!=3: 
                    #print node.children[i].object
                    
                    node.children[i].path=node.path
                    
                    #visited_val.append(node.path +node.children[i].object ) 
                   # visited_val.append([visited_val])
                    #print id(node.children[i])
                    queue.append(node.children[i])
                    i+=1
    return visited_val
    #print visited_val 
            
#print BFS(3)[15][2]

def DFS(depth):
    tree=buildtree(depth)
    visited=[]
    visited_val=[]
    temp=tree.root
    stack=[temp]
    
    while len(stack)!=0:
        #stack.reverse()
        node=stack.pop()
        
        if node in visited:
            print visited
            break
        else:
            #Add node to visted nodes
            
            visited.append(node)
            #path=visited.reverse()
            if node.object == "Start":
                    node.path+=""
            else:
                    node.path+=node.object
                    visited_val.append(node.path)
            
            if node.haschildren():
                
            
                i=0
                while i!=3:
                    #add to stack 
                    stack.append(node.children[i])
                    if node.object !="Start":
                        node.children[i].path=node.path
                    i+=1
    visited_val.reverse()
            
   # print visited_val
   # print visited_val
    return visited_val
    
    
#DFS(2)

#Task 3
    
#This code must run a the codes until repetion is found
#When this happens you must note the sequence
#use this sequence to trigger the repetion and win the game
#Rinput("enter")
#PC=input()


def win(prev):
     if prev=="R":
            return "P"
     elif prev=="P":
        return "S"
     elif prev=="S":
            return "R"
def selectgame(bfs_dfs,d):
    
    
    return BFS(d)

#print selectgame(1,3)[4][2]
#variables




counter=0


global i
i=3
global  j
j=0
global repeat
repeat=""
global winner
winner=0
#gm=input()
if input=="":
    #bfs_dfs=random.randint(0,1)
    play=random.choice(["R","P","S"])
    #play="R" 
    sequence=[]
    
  
   
    
else:
    
    
    bfs_dfs=1
    d=4
    #prev=input()
    prev=input
    #print 1
    #game starts
    if repeat=="": 
        #tranverse through the tree starting at i
        play= selectgame(bfs_dfs,d)[i][j]
        
        j+=1       
    elif repeat==input:
        winner=1
        sequence=(play[i])
        play=win(prev)
        
        doubletest=0
        #counter=0
    else: 
        #not repeated but sequence hasnt been found
        
        if winner==0:
            if j<len(selectgame(bfs_dfs,d)[i]):
             
                play= (selectgame(bfs_dfs,d)[i][j])
                j+=1
                
            else:
                i+=1
                #counter=0
              
                j=0 #stat again to the intial index before going out of bound
                play=selectgame(bfs_dfs,d)[i][j]
                #counter+1
                j+=1
                
        else:# break sequence has been found
            
            #play=sequence[a]
            #a+=1
            
            if doubletest == 1:
                winner = 0
            if j < len(selectgame(bfs_dfs,d)[i]):
                play = selectgame(bfs_dfs,d)[i][j]
                j += 1
                if j == len(selectgame(bfs_dfs,d)[i]):
                    doubletest = 1
            else:
                j = 0
                play = selectgame(bfs_dfs,d)[i][j]
                j += 1
    repeat=input    

output= play 
#print play           
            
    
    #intialization stage