conway.py

Created by florian-allard

Created on July 22, 2020

1012 Bytes

Automate cellulaire ou Jeu de la vie, créé par John Conway, qui simule l’évolution d’une population de cellules. Le plateau se comporte comme un tore, les bords se recollent.


from kandinsky import draw_string
from random import randint
a=18 #222//12
b=26 #320//12
Liste=[[0 for col in range(b)] for lig in range(a)]
for lig in range(a):
  for col in range(b):
    Liste[lig][col]=randint(1,100)%2
    if Liste[lig][col] == 1:
      draw_string("o",12*col+3,204-12*lig,'blue')
Voisins=[[0 for col in range(b)] for lig in range(a)]
Nouv=[[0 for col in range(b)] for lig in range(a)]

for gen in range(50):
  for i in range(a):
    for j in range(b):
      Voisins[i][j]=0
      for k in [-1,0,1]:
        for l in [-1,0,1]:
          if (k,l)!=(0,0):
            Voisins[i][j]+=Liste[(i+k)%a][(j+l)%b]

  for m in range(a):
    for n in range(b):
      if ( Liste[m][n]==0 and Voisins[m][n]==3 ) or ( Liste[m][n]==1 and Voisins[m][n] in [2,3] ):
        Nouv[m][n]=1
        couleur='blue'
      else:
        Nouv[m][n]=0
        couleur='white'
      if Nouv[m][n]!=Liste[m][n] or Nouv[m][n]==1:
        draw_string("o",12*n+3,204-12*m,couleur)
      Liste[m][n]=Nouv[m][n]

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.