Alexander Bass

Game of Life in Python

I’d like to show off the implementation of Conway’s Game of Life I made in Python. It’s a little unusual so please bear with me.

import time,os
from itertools import product as P
W=90;H,I=W//2,range;W,H,S=I(W),I(H),I(-1,2)
G=[[ord(os.urandom(1))<30for a in W]for b in H];B=G[:]
while[time.sleep(.03)]:
 G=[k[:]for k in B];print("\033c")
 for i in G:print(*map(lambda h:" █"[h]*2,i),sep='')
 for x,y in P(W,H):
  j=-G[y][x]
  for X,Y in P(S,S):
   try:j+=G[Y+y][X+x]
   except:0
  B[y][x]=0xC08>>j>>G[y][x]+8&1

It’s unreadable but at 383 bytes, the code runs the Game of Life. Run it in your terminal and it will show cells living and dying. I don’t blame you if you do not want to run random obfuscated code in your terminal, so you can instead try it below


How does one play the Game of Life? The rules are simple

  1. Start with a grid of square cells which can either be alive or dead
  2. At each cycle, check each cell in grid
  3. Count alive neighbors for each cell
  4. If an alive cell has exactly 2 or 3 alive neighbors it remains alive, otherwise it dies.
  5. If a dead cell has exactly 3 alive neighbors it becomes alive, otherwise it remains dead.
  6. Draw the grid with alive cells contrasting dead cells.
  7. Repeat cycle

I’ll leave it up to you to figure out how the code works.