Last active
August 29, 2015 14:01
-
-
Save ecounysis/8fa0e85043f05cce995e to your computer and use it in GitHub Desktop.
Conway's Game of Life stuff for kicks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time, random, sys | |
myrules = [2, # <2 life->death, >=2 life->life | |
4, # <=4 life->life, >4 life-> death | |
3] # =3 death->life (REPRODUCE) | |
conwayrules = [2,3,3] | |
rules = conwayrules | |
startprob = float(sys.argv[1]) # a random selection of all cells will begin alive | |
def zeroes(k): | |
return [0 for i in range(k)] | |
def grid(r,c): | |
return [zeroes(c) for i in range(r)] | |
def show(g): | |
for j in g: | |
print j | |
def render(g): | |
for row in g: | |
nr=[] | |
for el in row: | |
if (el==0): | |
nr.append(" ") | |
else: | |
nr.append("O") | |
print "".join(nr) | |
def countNeighbors(r, c, grid): | |
c0 = max(c-1, 0) | |
r0 = max(r-1, 0) | |
c1 = min(c+1, len(grid[0])-1) | |
r1 = min(r+1, len(grid)-1) | |
k = 0 | |
for i in range(r0, r1+1): | |
for j in range(c0, c1+1): | |
if (i!=r) or (j!=c): | |
k += grid[i][j] | |
return k | |
rows=35 | |
cols=120 | |
g = grid(rows,cols) | |
for row in range(len(g)): | |
for col in range(len(g[row])): | |
if random.random()<startprob: | |
g[row][col]=1 | |
def loop(g): | |
g2=grid(rows,cols) | |
for row in range(len(g)): | |
for col in range(len(g[0])): | |
g2[row][col]=g[row][col] | |
i=countNeighbors(row,col,g) | |
if i<rules[0]: | |
g2[row][col]=0 | |
if i>=rules[0] and i<=rules[1] and g[row][col]==1: | |
g2[row][col]=1 | |
if i>rules[1]: | |
g2[row][col]=0 | |
if i==rules[2]: | |
g2[row][col]=1 | |
return g2 | |
while True: | |
render(g) | |
time.sleep(1./7.5) | |
g=loop(g) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment