Advertisement
Altiumbe

[Python] CGoL with Time Travel 2: Temporal Boogaloo

Aug 19th, 2015
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import random as r
  2.  
  3. class Universe:
  4.  
  5.     def __init__(self, x, y, progenitor=[], deviation=[]):
  6.         print "Hello, Universe!"
  7.         print "x:" + str(x) + " y:" + str(y)
  8.  
  9.         self.univ = [[0 for i in range(y)] for j in range(x)] #init universe grid to 0
  10.         self.timeBuffer = [[[None,0] for i in range(y)] for j in range(x)]
  11.  
  12.         print "\nInit univ:\n"
  13.         for i in self.univ:
  14.             print i
  15.  
  16.         print "\nInit timeBuffer:\n"
  17.         for i in self.timeBuffer:
  18.             print i
  19.  
  20.         if progenitor and deviation:
  21.             univ = [[progenitor[i][j] + deviation[i][j] for i in range(x)] for j in range(y)]
  22.         elif progenitor:
  23.             univ = [[progenitor[i][j] for i in range(x)] for j in range(y)]
  24.         #else:
  25.         #    pass
  26.         self.x = x-1
  27.         self.y = y-1
  28.         self.branches = []
  29.  
  30.     def getNextState(self,x,y):
  31.         if x == self.x or x == 0 or y == self.y or y == 0: # we should implement dynamic sizing soon
  32.             return 0
  33.  
  34.         sum = r.randint(0,8) #[sum(i) for i in self.univ[y-1:y+1]]# :( this offends my determinist sensibilities
  35.  
  36.         if sum == 3:
  37.             return 1
  38.         elif sum == 2 and (self.univ[x][y] == 1):
  39.             return 1
  40.         elif sum > 6:
  41.             self.branch(sum - 6)
  42.             return 0
  43.         else:
  44.             return 0
  45.  
  46.     def update(self,univ):
  47.         print "Update..."
  48.         temp = univ
  49.  
  50.         self.univ = [[self.getNextState(i,j) for i in range(self.y)] for j in range(self.x)]
  51.  
  52.         print "\nCGoL Update:\n"
  53.         for i in self.univ:
  54.             print i
  55.  
  56.         if self.branches:
  57.             print "Do theRealBranch"
  58.             theRealBranch(self.timeBuffer, self.branches)
  59.  
  60.         #for i in range(univ.y):   # What's this?
  61.         #    for j in range(univ.x):
  62.         #        print "UpdateXY:" + str(j) + ", " + str(i)
  63.  
  64.         branches = []
  65.         print "Updated!"
  66.  
  67.     def branch(self, sum):
  68.         print "Branch!"
  69.         self.branches.append([self.x,self.y,sum - 6])
  70.         self.timeBuffer[self.x][self.y] = (0, sum - 6)
  71.     ## Code Transplant ##
  72.  
  73.     def theRealBranch(self,timeBuffer, branches):
  74.         creating = True
  75.         while creating:
  76.             counter = 0
  77.             list = [0 for i in branches]
  78.             for i in list:
  79.  
  80.                 print "Creating branch", str(counter)
  81.  
  82.                 tempTimeBuffer = self.timeBuffer # There are just temporary variables here
  83.                 tempDevi = self.template         # while their universe is being created
  84.  
  85.                 counter += 1        # keep track of where we are
  86.  
  87.                 tempDevi[branches[counter][x]][branches[counter][1]] = list[counter]
  88.  
  89.                 for i in branches:       # updates timeBuffer to stop weird stuff from happening in the new universe
  90.                     tempTimeBuffer[branches[1]][branches[2]] = [list[counter], branches[2]]
  91.  
  92.                 EverythingInExistence.append(Universe(self.x, self.y, self.univ, tempDevi))
  93.                 myCreation = EverythingInExistence(len(EverythingInExistence)-1)
  94.                 myCreation.timeBuffer = tempTimeBuffer
  95.  
  96.             list[counter] += 1
  97.  
  98.             if list[counter] == 2:                                                
  99.  
  100.                 for i in range(list):                  # list is represented as a binary number, and incremented
  101.  
  102.                     j = len(list) - i
  103.  
  104.                     if list[j] == 2 and not j == 0:      
  105.  
  106.                         list[j] -= 1
  107.                         list[j-1]+=1
  108.  
  109.                     elif j == 0 and list[j] == 2:
  110.  
  111.                         creating = False
  112.  
  113.                         break
  114.  
  115.                     else:
  116.  
  117.                         continue
  118.  
  119.             else:
  120.  
  121.                 continue
  122.  
  123. if __name__ == "__main__":
  124.  
  125.      EverythingInExistence =  []
  126.  
  127.      ux = 6
  128.      uy = 6
  129.      ut = 3
  130.  
  131.      uinput = raw_input("  > (Int) [" + str(ux) + "] Universe X dimension: ")
  132.      if uinput != "":
  133.         ux = int(uinput)
  134.  
  135.      uinput = raw_input("  > (Int) [" + str(uy) + "] Universe Y dimension: ")
  136.      if uinput != "":
  137.         uy = int(uinput)
  138.  
  139.      uinput = raw_input("  > (Int) [" + str(ut) + "] Universe Timesteps to run: ")
  140.      if uinput != "":
  141.         ut = int(uinput)
  142.  
  143.      print "\nRun grid [" + str(ux) + ", " + str(uy) + "] for " + str(ut) + " steps.\n"
  144.  
  145.      EverythingInExistence.append(Universe(ux,uy))
  146.  
  147.      for t in range(ut):
  148.         for i in EverythingInExistence:
  149.            print "Run all universes (" + str(t) + ", " + str(i) + ") "
  150.            i.update(i)
  151.  
  152. #    print '[%s]' % ', '.join(map(str, EverythingInExistence))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement