Skip to content

Instantly share code, notes, and snippets.

@christopherhill
Created March 1, 2014 14:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save christopherhill/9290801 to your computer and use it in GitHub Desktop.
Save christopherhill/9290801 to your computer and use it in GitHub Desktop.
class Automata():
'Represents a cellular automata with configurable params:'
' - number of columns, defaults to 64'
' - number of iterations, defaults to 32'
' - true symbol, defaults to *'
' - false symbol, defaults to -'
entries = []
rows = 0
cur_row = 0
def __init__(self, cols=64, iters=32, t_symbol="*", f_symbol="-"):
print "Beginning Cellular Automata Simulation"
print "Press CTRL+C to quit."
self.cols = cols
self.iterations = iters
self.true_symbol = t_symbol
self.false_symbol = f_symbol
try:
while self.cur_row < self.iterations:
self.add_row()
self.print_row()
self.cur_row += 1
except KeyboardInterrupt:
print "Exiting."
return False
def set_first_row_position(self):
self.first_row_position = (self.cols / 2) - 1
return self.first_row_position
def get_previous_row_column(self, col):
if self.cur_row > 0:
previous_row = self.entries[self.cur_row - 1]
return previous_row[col]
else:
return False
def print_row(self):
return_val = ""
for i in range(0, self.cols - 1):
col = self.entries[self.cur_row][i]
if col == True:
return_val += self.true_symbol
elif col == False:
return_val += self.false_symbol
else:
return_val += self.false_symbol
print return_val
return return_val
def add_row(self):
self.entries.append([])
self.entries[self.cur_row] = [False] * self.cols
if self.cur_row == 0:
automata_start = self.set_first_row_position()
self.entries[self.cur_row][automata_start] = True
else:
for i in range(self.cols):
pos_left = i - 1
pos_right = i + 1
if (pos_left > 0):
prev_pos_left = self.get_previous_row_column(pos_left)
else:
prev_pos_left = False
if (pos_right < self.cols):
prev_pos_right = self.get_previous_row_column(pos_right)
else:
prev_pos_right = False
if prev_pos_right == True and prev_pos_left == False:
return_val = True
elif prev_pos_left == True and prev_pos_right == False:
return_val = True
else:
return_val = False
self.entries[self.cur_row][i] = return_val
return self.entries[self.cur_row]
Automata()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment