Master 3

AuthorLefa
Submission date2019-02-17 19:58:24.310493
Rating2196
Matches played237
Win rate19.83

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

Source code:

#Imports
import random
#import queue

import traceback


#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(p):
     if prev=="R":
            return "S"
     elif prev=="P":
        return "R"
     elif prev=="S":
            return "P"
def selectgame(bfs_dfs,depth):
    
    
    return BFS(d)

#variables

i=0
#print selectgame(1,3)[2][1]
if input=="":
    bfs_dfs=random.randint(0,1)
    play=random.choice(["R","P","S"])
    repeat=""
else:
    #prev=input()
    prev=input
    bfs_dfs=0
    repeat="" #not repeating
    play=""
    d=2
    
    
    #game starts
    if repeat=="":
        #intiallise repeat
        repeat=input
        
        #tranverse through the tree
        #starting at i
        a=0
        #length=len(selectgame(bfs_dfs,d)[i])
        temp=selectgame(bfs_dfs,d)
        length=len(temp[i])
        play= selectgame(bfs_dfs,d)[i][length-1]
        i+=1
        
        
    elif repeat==input:
        winner=1
        play=win(prev)
#repeat=input
output= play 
#print play           
            
    
    #intialization stage