Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
CellAuto.py - Python terminal cellular automata.
#! /usr/bin/env python3
from shutil import get_terminal_size
from random import choice
columns = get_terminal_size().columns - 4
rows = get_terminal_size().lines
options = [' ', '']
def ca_rule(row, options):
new_row = ""
for i in range(0, len(row)):
score = 0
if i == 0:
# At the left of the row; check left to the furthest right
if row[i + 1] == options[1]:
score += 1
if row[len(row) - 1] == options[1]:
score += 1
elif i == len(row) - 1:
# At the right of the row; check right to the furthest left
if row[0] == options[1]:
score += 1
if row[i - 1] == options[1]:
score += 1
else:
# in the middle of the row.
if row[i + 1] == options[1]:
score += 1
if row[i - 1] == options[1]:
score += 1
if score == 0 or score == 2:
new_row += options[0]
else:
new_row += options[1]
return new_row
row = ""
for _ in range(0, columns):
row += choice(options)
for linenum in range(0, rows):
print("{:03} {}".format(linenum, row))
row = ca_rule(row, options)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment