Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random as r
- class Universe:
- def __init__(self, x, y, progenitor=[], deviation=[]):
- print "Hello, Universe!"
- print "x:" + str(x) + " y:" + str(y)
- self.univ = [[0 for i in range(y)] for j in range(x)] #init universe grid to 0
- self.timeBuffer = [[[None,0] for i in range(y)] for j in range(x)]
- print "\nInit univ:\n"
- for i in self.univ:
- print i
- print "\nInit timeBuffer:\n"
- for i in self.timeBuffer:
- print i
- if progenitor and deviation:
- univ = [[progenitor[i][j] + deviation[i][j] for i in range(x)] for j in range(y)]
- elif progenitor:
- univ = [[progenitor[i][j] for i in range(x)] for j in range(y)]
- #else:
- # pass
- self.x = x-1
- self.y = y-1
- self.branches = []
- def getNextState(self,x,y):
- if x == self.x or x == 0 or y == self.y or y == 0: # we should implement dynamic sizing soon
- return 0
- sum = r.randint(0,8) #[sum(i) for i in self.univ[y-1:y+1]]# :( this offends my determinist sensibilities
- if sum == 3:
- return 1
- elif sum == 2 and (self.univ[x][y] == 1):
- return 1
- elif sum > 6:
- self.branch(sum - 6)
- return 0
- else:
- return 0
- def update(self,univ):
- print "Update..."
- temp = univ
- self.univ = [[self.getNextState(i,j) for i in range(self.y)] for j in range(self.x)]
- print "\nCGoL Update:\n"
- for i in self.univ:
- print i
- if self.branches:
- print "Do theRealBranch"
- theRealBranch(self.timeBuffer, self.branches)
- #for i in range(univ.y): # What's this?
- # for j in range(univ.x):
- # print "UpdateXY:" + str(j) + ", " + str(i)
- branches = []
- print "Updated!"
- def branch(self, sum):
- print "Branch!"
- self.branches.append([self.x,self.y,sum - 6])
- self.timeBuffer[self.x][self.y] = (0, sum - 6)
- ## Code Transplant ##
- def theRealBranch(self,timeBuffer, branches):
- creating = True
- while creating:
- counter = 0
- list = [0 for i in branches]
- for i in list:
- print "Creating branch", str(counter)
- tempTimeBuffer = self.timeBuffer # There are just temporary variables here
- tempDevi = self.template # while their universe is being created
- counter += 1 # keep track of where we are
- tempDevi[branches[counter][x]][branches[counter][1]] = list[counter]
- for i in branches: # updates timeBuffer to stop weird stuff from happening in the new universe
- tempTimeBuffer[branches[1]][branches[2]] = [list[counter], branches[2]]
- EverythingInExistence.append(Universe(self.x, self.y, self.univ, tempDevi))
- myCreation = EverythingInExistence(len(EverythingInExistence)-1)
- myCreation.timeBuffer = tempTimeBuffer
- list[counter] += 1
- if list[counter] == 2:
- for i in range(list): # list is represented as a binary number, and incremented
- j = len(list) - i
- if list[j] == 2 and not j == 0:
- list[j] -= 1
- list[j-1]+=1
- elif j == 0 and list[j] == 2:
- creating = False
- break
- else:
- continue
- else:
- continue
- if __name__ == "__main__":
- EverythingInExistence = []
- ux = 6
- uy = 6
- ut = 3
- uinput = raw_input(" > (Int) [" + str(ux) + "] Universe X dimension: ")
- if uinput != "":
- ux = int(uinput)
- uinput = raw_input(" > (Int) [" + str(uy) + "] Universe Y dimension: ")
- if uinput != "":
- uy = int(uinput)
- uinput = raw_input(" > (Int) [" + str(ut) + "] Universe Timesteps to run: ")
- if uinput != "":
- ut = int(uinput)
- print "\nRun grid [" + str(ux) + ", " + str(uy) + "] for " + str(ut) + " steps.\n"
- EverythingInExistence.append(Universe(ux,uy))
- for t in range(ut):
- for i in EverythingInExistence:
- print "Run all universes (" + str(t) + ", " + str(i) + ") "
- i.update(i)
- # print '[%s]' % ', '.join(map(str, EverythingInExistence))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement