from visual.graph import *
from random import *

scene.width = 800
scene.height = 800

##graphone = gcurve(color=color.cyan)
##graphtwo = gcurve(color=color.red)
##graphthree = gcurve(color=color.yellow)

newlist1 = list()
newlist2 = list()

##graphpos = list()
##graphipos = list()
##graphdpos = list()

thesum = 0
total = 0

maxN = 80
maxRun = 1

run = 0

v = 0
l = -v


def ci(element1,element2):
    if element1 == "T" and element2 == "T":
        return('T')
    if element1 == "T" and element2 == "F":
        return('C')
    if element1 == "T" and element2 == "O":
        return('T')
    if element1 == "T" and element2 == "C":
        return('F')

        
    if element1 == "F" and element2 == "T":
        return('C')
    if element1 == "F" and element2 == "F":
        return('F')
    if element1 == "F" and element2 == "O":
        return('F')
    if element1 == "F" and element2 == "C":
        return('T')
        
        
    if element1 == "O" and element2 == "T":
        return('T')
    if element1 == "O" and element2 == "F":
        return('F')
    if element1 == "O" and element2 == "O":
        return('O')
    if element1 == "O" and element2 == "C":
        return('O')

    if element1 == "C" and element2 == "T":
        return('F')
    if element1 == "C" and element2 == "F":
        return('T')
    if element1 == "C" and element2 == "O":
        return('O')  
    if element1 == "C" and element2 == "C":
        return('C')

def su(element1,element2):
    if element1 == "T" and element2 == "T":
        return('T')
    if element1 == "T" and element2 == "F":
        return('O')
    if element1 == "T" and element2 == "O":
        return('T')
    if element1 == "T" and element2 == "C":
        return('T')

        
    if element1 == "F" and element2 == "T":
        return('O')
    if element1 == "F" and element2 == "F":
        return('F')
    if element1 == "F" and element2 == "O":
        return('F')
    if element1 == "F" and element2 == "C":
        return('F')
        
        
    if element1 == "O" and element2 == "T":
        return('T')
    if element1 == "O" and element2 == "F":
        return('F')
    if element1 == "O" and element2 == "O":
        return('O')
    if element1 == "O" and element2 == "C":
        return('C')

    if element1 == "C" and element2 == "T":
        return('T')
    if element1 == "C" and element2 == "F":
        return('F')
    if element1 == "C" and element2 == "O":
        return('C')  
    if element1 == "C" and element2 == "C":
        return('C')
        
def product(newlist):
    product = "O"
    for m in xrange(len(newlist)-1):
        product = su(newlist[m],product)
    return product

def graphlist(givenlist):
    total = 0.0
    outlist = list()
    for n in xrange(maxN-1):
        if givenlist[n] == "T":
            total += 1
        if givenlist[n] == "F":
            total -= 1
        average = total/(n+1)
        outlist.append(average)
    return outlist

def makeRandomString(listtowrite):
    l = 0
    while l < maxN:  
        l += 1
        
        U = random()
        if 0 < U < .25:
            W = "F"
        if .25 < U < .5:
            W = "T"
        if .5 < U < .75:
            W = "O"
        if .75 < U < 1:
            W = "C"
            
        listtowrite.append(W)

def multiplySU(listone,listtwo):
    output = list()
    for m in xrange(maxN-1):
        output.append(su(listone[m],listtwo[m]))
    return output

def multiplyCI(listone,listtwo):
    output = list()
    for m in xrange(maxN-1):
        output.append(ci(listone[m],listtwo[m]))
    return output

def reverseMap(inputlist):
    output = list()
    for m in xrange(maxN-1):
        element = inputlist[m]
        if element == "T":
            elementout = "F"
        if element == "F":
            elementout = "T"
        if element == "O":
            elementout = "C"
        if element == "C":
            elementout = "O"
        output.append(elementout)
    return output

def sectionProduct(length,inputlist):
    output = list()
    for m in xrange(maxN-1):
        mini = list()
        for l in xrange(length):
            if (m+l) > maxN-2:
                mini.append(inputlist[m+l-maxN])
            else:
                mini.append(inputlist[m+l])
        element = product(mini)
        output.append(element)
    return output

def sectionProductBehind(length,inputlist):
    output = list()
    for m in xrange(maxN-1):
        mini = list()
        for l in xrange(length):
            if (m-l) < 0:
                mini.append(inputlist[m-l+maxN])
            else:
                mini.append(inputlist[m-l])
        element = product(mini)
        output.append(element)
    return output

makeRandomString(newlist1)
makeRandomString(newlist2)

continual = newlist1

boxes = [None]*(maxN-1)
for n in xrange(maxN-1):
    boxes[n] = [None]*(maxN-1)
    
    for m in xrange(maxN-1):
        xpos = n-maxN/2.0
        ypos = m-maxN/2.0

        element = continual[m]
        
        if element == "T":
            boxcolor = color.blue
        if element == "F":
            boxcolor = color.red
        if element == "O":
            boxcolor = color.white
        if element == "C":
            boxcolor = color.black
            
        boxes[n][m] = box(pos=(xpos,ypos,0.0),length=1.0,height=1.0,width=1.0,color=boxcolor)

    randomList = list()
    makeRandomString(randomList)
    continual = multiplySU(multiplySU(sectionProduct(n,newlist1),sectionProductBehind(n,newlist2)),continual)


##for n in xrange(maxN-1):
##    newlistone.append(su(newlist[n],newlist[n+1]))
##    newlisttwo.append(ci(newlist[n],newlist[n+1]))
##        
##print("New List",newlist)
##print("Integral",newlistone)
##print("Derivative",newlisttwo)
##
##graphpositions = graphlist(newlist)
##graphipositions = graphlist(newlistone)
##graphdpositions = graphlist(newlisttwo)
##    
##for n in xrange(maxN-1):
##    graphone.plot(pos=(n,graphpositions[n]))
##    graphtwo.plot(pos=(n,graphipositions[n]))
##    graphthree.plot(pos=(n,graphdpositions[n]))    

