Created
March 7, 2018 00:19
-
-
Save ChristopherKing42/e3efe122ca0c7e048052735c4dc45e65 to your computer and use it in GitHub Desktop.
This file contains 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
# -*- 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