Skip to content

Instantly share code, notes, and snippets.

@nekosan
Last active August 29, 2015 14:03
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 nekosan/988c4c43104d77c3d861 to your computer and use it in GitHub Desktop.
Save nekosan/988c4c43104d77c3d861 to your computer and use it in GitHub Desktop.
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