[Python] life game(in 1-dimention)

#!/usr/bin/env python
import random
import time

CNT=151
delay=0.2
empty='.'
fill='#'

"""get console's size"""
import os
rows,cols=os.popen('stty size','r').read().split()
CNT=int(cols)
del rows,cols
"""get console's size end"""


def cnt(index):
	global before
	num=0
	for i in range(index-1,index+2):
		if before[i%CNT]==fill:
			num+=1
	return num

#lst=[random.choice(['#','.']) for i in range(CNT)]
lst=[empty for i in range(CNT)]
for i in random.sample(range(CNT),5):
	lst[i]=fill
before=[]
while True:
	print ''.join(lst)
	before=lst[:]
	for i in range(CNT):
		if cnt(i) in [1,2]:
			lst[i]=fill
		else:
			lst[i]=empty
	time.sleep(delay)

kjwon15

I'm a hacker, I want to improve life.

Leave a Reply

Your email address will not be published. Required fields are marked *

 

This site uses Akismet to reduce spam. Learn how your comment data is processed.