Skip to content

Instantly share code, notes, and snippets.

@Spittie
Last active August 29, 2015 13:56
Show Gist options
  • Select an option

  • Save Spittie/9079381 to your computer and use it in GitHub Desktop.

Select an option

Save Spittie/9079381 to your computer and use it in GitHub Desktop.
Ghetto game of life
from blessings import Terminal
import numpy
import time
import sys
t = Terminal()
with t.fullscreen():
#h = t.height
#w = t.width
w = 40
h = 40
board = numpy.zeros((w, h))
changes = numpy.zeros((w, h))
numpy.set_printoptions(threshold=numpy.nan)
board[30][30] = 1
board[30][31] = 1
board[31][31] = 1
board[31][30] = 1
board[2][4] = 1
board[3][2] = 1
board[4][3] = 1
board[3][4] = 1
board[4][4] = 1
iterations = 0
while 1:
time.sleep(0.1)
iterations += 1
with t.location(0, 0):
#print numpy.array_str(board, max_line_width=100000)
for x in range(w):
for y in range(h):
if board[x][y] == 1:
sys.stdout.write("@")
else:
sys.stdout.write(" ")
print
print 'Tick: ' + str(iterations)
for x in range(w):
for y in range(h):
neighbours = 0
try:
for nx in [-1, 0, 1]:
for ny in [-1, 0, 1]:
if !(nx == 0 and xy == 0):
if board[x + nx][y + ny] == 1:
neighbours += 1
except IndexError:
pass
changes[x][y] = board[x][y]
if board[x][y] == 1:
if neighbours < 2:
changes[x][y] = 0
if neighbours > 3:
changes[x][y] = 0
if board[x][y] == 0:
if neighbours == 3:
changes[x][y] = 1
board = changes
changes = numpy.zeros((w, h))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment