RunDMC

This program has been disqualified.


AuthorSean
Submission date2016-02-09 01:33:53.738542
Rating5000
Matches played0
Win rate0

Source code:

import random                                                                   
                                                                                
class MarkovChain:                                                              
    def __init__(self, counts = None):                                          
        self.visits = 0                                                         
        if counts is None:                                                      
            self.counts = [1, 1, 1]                                             
        else:                                                                   
            self.counts = counts                                                
        self.children = [self, self, self]                                      
                                                                                
    def split_edge(self, i):                                                    
        old = self.children[i]                                                  
        new = MarkovChain(old.counts)                                           
        self.children[i] = new                                                  
        for i in xrange(3):                                                     
            new.children[i] = old.children[i]                                   
                                                                                
    def unnormalized_probabilities(self):                                       
        return [x + 1 for x in self.counts]                                     
                                                                                
    def transition(self, i):                                                    
        self.visits += 1                                                        
        self.counts[i] += 2                                                     
        if self.counts[i] >= 128:                                               
            for i in xrange(3):                                                 
                self.counts[i] = self.counts[i] / 2 + 1                         
        if self.children[i].visits >= 2:                                        
            self.split_edge(i)                                                  
        return self.children[i]                                                 
                                                                                
index = {"R": 0,                                                                
         "P": 1,                                                                
         "S": 2}                                                                
                                                                                
beat = {0: "P",                                                                 
        1: "S",                                                                 
        2: "R"}                                                                 
                                                                                
if input == "":                                                                 
    model = MarkovChain()                                                       
else:                                                                           
    i = index[input]                                                            
    j = index[output]                                                           
    model = model.transition(i)                                                 
    model = model.transition(j)                                                 
probs = model.unnormalized_probabilities()                                      
t = sum(probs)                                                                  
print([x / float(t) for x in probs])                                            
r = random.randint(0, t)                                                        
x = 0                                                                           
for i, p in enumerate(probs):                                                   
    x += p                                                                      
    if r < x:                                                                   
        output = beat[i]                                                        
        break