Created
February 4, 2023 05:03
-
-
Save ytaki0801/df252cfad73282dba7dd387bed40ee2d to your computer and use it in GitHub Desktop.
Conway's Game of Life in Python with tput
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from random import randint | |
from copy import deepcopy | |
from os import system | |
a, b = 80, 24; x, y, n = range(a), range(b), [-1,0,1] | |
c = [[randint(0,1) for _ in x] for _ in y]; g = deepcopy(c) | |
while True: | |
t = ''.join([''.join(['*' if c[j][i] else ' ' for i in x])+'\n' for j in y]) | |
system('tput cup 18 0'); print(t, end='') | |
for j in y: | |
for i in x: | |
t = [0 if (k,l) == (0,0) else c[(j+l)%b][(i+k)%a] for k in n for l in n] | |
t = sum(t) | |
g[j][i] = (1 if t in [2,3] else 0) if c[j][i] else (1 if t == 3 else 0) | |
c = deepcopy(g) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment