Last active
June 1, 2017 15:53
-
-
Save odashi/9dd635b70a145d996618f1cf7e00f70c to your computer and use it in GitHub Desktop.
The console game of life
This file contains hidden or 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
#!/usr/bin/env python3 | |
import curses | |
from random import random | |
from time import sleep | |
def main(scr): | |
curses.init_pair(1, curses.COLOR_BLACK, curses.COLOR_BLACK) | |
curses.init_pair(2, curses.COLOR_BLACK, curses.COLOR_BLACK) | |
curses.init_pair(3, curses.COLOR_WHITE, curses.COLOR_WHITE) | |
curses.init_pair(4, curses.COLOR_GREEN, curses.COLOR_GREEN) | |
while True: | |
h, w = hh, ww = scr.getmaxyx() | |
w = w // 2 - 1 | |
data = [[(0 if random() < 0.5 else 1) for _ in range(h)] for __ in range(w)] | |
for s in range(256): | |
if scr.getmaxyx() != (hh, ww): | |
break | |
for x in range(w): | |
for y in range(h): | |
n = 0 | |
for i in [-1, 0, 1]: | |
for j in [-1, 0, 1]: | |
n += data[(x + i) % w][(y + j) % h] & 1 | |
if n == 3 or (n == 4 and data[x][y] & 1): | |
data[x][y] += 2 | |
scr.clear() | |
for x in range(w): | |
for y in range(h): | |
p = data[x][y] | |
data[x][y] = p >> 1 | |
scr.addstr(y, 2 * x, '@@', curses.color_pair(p + 1)) | |
scr.refresh() | |
sleep(0.3) | |
try: | |
curses.wrapper(main) | |
except: | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment