Салют! Окей, вот примерчик кода для 'Игры жизни' на Python с использованием библиотеки numpy.
Программный код:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
ON = 255
OFF = 0
vals = [ON, OFF]
def random_grid(N):
return np.random.choice(vals, N*N, p=[0.2, 0.8]).reshape(N, N)
def update_grid(frameNum, img, grid, N):
newGrid = grid.copy()
for i in range(N):
for j in range(N):
total = sum([grid[i, (j-1)%N],
grid[i, (j+1)%N],
grid[(i-1)%N, j],
grid[(i+1)%N, j],
grid[(i-1)%N, (j-1)%N],
grid[(i-1)%N, (j+1)%N],
grid[(i+1)%N, (j-1)%N],
grid[(i+1)%N, (j+1)%N]])
if grid[i, j] == ON:
if (total < 2) or (total > 3):
newGrid[i, j] = OFF
else:
if total == 3:
newGrid[i, j] = ON
img.set_data(newGrid)
grid[:] = newGrid[:]
return img,
def main():
N = 100
updateInterval = 50
grid = random_grid(N)
fig, ax = plt.subplots()
img = ax.imshow(grid, interpolation='nearest')
ani = animation.FuncAnimation(fig, update_grid, fargs=(img, grid, N,),
frames=10, interval=updateInterval,
save_count=50)
plt.show()
if __name__ == '__main__':
main()
Библиотеки numpy и matplotlib обязательны. Удачи!)