Last active
May 4, 2017 09:42
-
-
Save orsinium/05ac9858066e61618ecff960d2d167bf to your computer and use it in GitHub Desktop.
Conway's 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
#generator: https://gist.github.com/orsinium/eb2f601e11803333a036ff55e9904250 | |
#author: Gram ([email protected]) | |
#description: Conway's Game of Life | |
from time import sleep | |
import curses | |
this_g = open('in', 'r').read() | |
stdscr = curses.initscr() | |
curses.curs_set(0) | |
stdscr.addstr(0,0, this_g) | |
stdscr.refresh() | |
curses.start_color() | |
curses.use_default_colors() | |
for i in range(0, curses.COLORS): | |
curses.init_pair(i + 1, i, -1) | |
this_g = [list(i) for i in this_g.split('\n')][:-1] | |
n = len(this_g) | |
while 1: | |
sleep(0.2) | |
#Копирование текущего поколение в предыдущее | |
prev_g = [i[:] for i in this_g] | |
for i in range(n): | |
for j in range(n): | |
#Число живых соседей | |
live_neg = 0 | |
for k in (i-1, i, i+1): | |
for l in (j-1, j, j+1): | |
if k == i and l == j: | |
continue | |
if prev_g[k % n][l % n] == '#': | |
live_neg += 1 | |
#Изменение состояния клетки | |
this_point = prev_g[i][j] | |
if this_point == '.' and live_neg == 3: | |
this_g[i][j] = '#' | |
elif this_point == '#' and (live_neg < 2 or live_neg > 3): | |
this_g[i][j] = '.' | |
#Вывод предыдущего поколения. | |
if this_point == '#': | |
stdscr.addstr(i,j, this_point, curses.color_pair(3)) | |
else: | |
stdscr.addstr(i,j, this_point) | |
#Вывод числа соседей для каждой клетки предыдущего поколения | |
if live_neg: | |
if this_point == '#': | |
stdscr.addstr(i,j+n+1, str(live_neg), curses.color_pair(3)) | |
else: | |
stdscr.addstr(i,j+n+1, str(live_neg)) | |
else: | |
stdscr.addstr(i,j+n+1, ' ') | |
stdscr.refresh() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment