Skip to content

Instantly share code, notes, and snippets.

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 ChristopherKing42/e3efe122ca0c7e048052735c4dc45e65 to your computer and use it in GitHub Desktop.
Save ChristopherKing42/e3efe122ca0c7e048052735c4dc45e65 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
import random
born = [3,6,7,8]
die = [0,1,2,5]
size = 30
def neighbors((x,y),g):
for newx in [x-1,x,x+1]:
for newy in [y-1,y,y+1]:
if (newx == x) and (newy == y):
continue
else:
if newx % size == newx: yield g[newx % size][newy % size]
else: yield not g[newx % size][(size - newy) % size]
def randomGrid():
grid = []
for x in xrange(size):
row = []
grid.append(row)
for y in xrange(size):
row.append(random.choice([False,True]))
return grid
def showGrid(g):
rep = ''
for row in g:
for cell in row:
if cell:
rep += '■ '
else:
rep += '□ '
rep += '\n'
return rep
def step(g):
newg = []
for x in xrange(size):
row = []
newg.append(row)
for y in xrange(size):
if g[x][y]:
if len([alive for alive in neighbors((x,y),g) if alive]) in die: row.append(False)
else: row.append(True)
else:
if len([alive for alive in neighbors((x,y),g) if alive]) in born: row.append(True)
else: row.append(False)
return newg
g = randomGrid()
while True:
print showGrid(g)
ans = raw_input()
if ans == 'q': break
g = step(g)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment