Create a gist now

Instantly share code, notes, and snippets.

@nekosan /lifegame.py
Last active Aug 29, 2015

What would you like to do?
import sys
import time
class Field:
def __init__(self, size_x, size_y):
self.size = (size_x, size_y)
self.field = []
for i in range(self.size[0] * self.size[1]):
self.field.append(0)
def display(self):
for y in range(self.size[1]):
sys.stdout.write("\n")
for x in range(self.size[0]):
if self.field[y * self.size[0] + x] == 0:
sys.stdout.write(' ')
else:
sys.stdout.write('#')
def dot(self, x, y, value):
if value == 0 or value == 1:
self.field[y * self.size[0] + x] = value
return 0
else :
return 1
def next(self):
ref = self.field[:]
for y in range(1, self.size[1] - 1):
for x in range(1, self.size[0] - 1):
num = 0
for j in [-1, 0, 1]:
for i in [-1, 0, 1]:
num += ref[(y + j) * self.size[0] + (x + i)]
if ref[y * self.size[0] + x] == 0:
if num == 3:
self.dot(x, y, 1)
else:
if num <= 2 or num >= 5:
self.dot(x, y, 0)
def beacon(master, x, y):
master.dot(x + 0, y + 0, 1)
master.dot(x + 1, y + 0, 1)
master.dot(x + 0, y + 1, 1)
master.dot(x + 1, y + 1, 1)
master.dot(x + 2, y + 2, 1)
master.dot(x + 3, y + 2, 1)
master.dot(x + 2, y + 3, 1)
master.dot(x + 3, y + 3, 1)
def glider(master, x, y):
master.dot(x + 0, y + 0, 1)
master.dot(x + 1, y + 0, 1)
master.dot(x + 2, y + 0, 1)
master.dot(x + 2, y + 1, 1)
master.dot(x + 1, y + 2, 1)
if __name__ == '__main__':
field = Field(20, 20)
generation = 0
glider(field, 5, 15)
field.display()
while True:
field.next()
print ' '
field.display()
generation += 1
print 'Generation : ' + str(generation)
time.sleep(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment