Просто еще один вариант "Жызни"))) Не благодарите)))
import os
import random
import time
class Colony:
def __init__(self, width, height):
self.width = width
self.height = height
self.field = [
[random.randint(0,1) for i in range(width)] for j in range(height)
]
def live(self):
"""
Check cell's neighbors and modify cell's state.
"""
for row in range(0,self.height):
for col in range(0,self.width):
live_neighbours = self.check_neighbours(row, col)
if live_neighbours < 2:
# die
self.field[row][col] = 0
# elif live_neighbours in (2,3):
# remain as is
elif live_neighbours == 3:
# raise
self.field[row][col] = 1
elif live_neighbours > 3:
# die
self.field[row][col] = 0
def check_neighbours(self, row, col):
live_neighbours = 0
for r in (row - 1, row, row + 1):
for c in (col - 1, col, col + 1):
if c >=0 and c < self.width and r >=0 and r < self.height:
if self.field[r][c] and not (c == col and r == row):
live_neighbours += 1
return live_neighbours
def redraw(self):
"""
Clears screen and draws actual field state.
"""
os.system('clear')
for row in self.field:
for cell in row:
print(cell, end='')
print('', end='\n')
if __name__ == '__main__':
# TODO: check for filename or field dimensions in comand line parameters
# dimensions are hardcoded for now.
start_time = time.time()
first_loop = 1
width = 20
height = 20
colony = Colony(width, height)
while True:
colony.live()
if time.time() - start_time > 0.99 or first_loop:
first_loop = 0
start_time = time.time()
colony.redraw()
print('Ctrl + C to stop')
Куда ходить в Яднекс за своей кучей денег?))))