Skip to content

Instantly share code, notes, and snippets.

@ytaki0801
Created February 1, 2023 12:17
Show Gist options
  • Save ytaki0801/02c89f9ec9cb86f14e4c9ebe399d6528 to your computer and use it in GitHub Desktop.
Save ytaki0801/02c89f9ec9cb86f14e4c9ebe399d6528 to your computer and use it in GitHub Desktop.
Conway's Game of Life in Python
from random import randint
from os import system
from time import sleep
from copy import deepcopy
xs, ys = 210, 100
c = [[randint(0,1) for _ in range(ys)] for _ in range(xs)]
cn = [[0 for _ in range(ys)] for _ in range(xs)]
while True:
system('clear')
for j in range(ys):
print(''.join(['*' if c[i][j] else ' ' for i in range(xs)]))
for j in range(ys):
for i in range(xs):
t = 0
for k in [-1,0,1]:
for l in [-1,0,1]:
t += 0 if k==0 and l==0 else c[(i+l)%xs][(j+k)%ys]
if c[i][j]: cn[i][j] = 1 if t==2 or t==3 else 0
else: cn[i][j] = 1 if t==3 else 0
c = deepcopy(cn)
sleep(0.1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment