# -*- coding: utf-8 -*-
"""
Created on Thu Jan 15 21:20:26 2026
@author: bazoulefoufou
"""
# -*- coding: utf-8 -*-
from random import uniform
import matplotlib.pyplot as plt
import matplotlib.animation as animation
def init_vide(size):
"""
renvoie une liste 2D de taille (size*size) remplie de 0
"""
grid = []
for i in range(size):
ligne = []
for j in range(size):
ligne.append(0)
grid.append(ligne)
return grid
def init_aleatoire(size, proba):
"""
renvoie une liste 2D de taille (size*size)
remplie de 0 (avec probabilité 1 - proba) ou de 1(avec probabilité proba)
"""
grid = []
for i in range(size):
ligne = []
for j in range(size):
if uniform(0, 1) < proba:
ligne.append(1)
else:
ligne.append(0)
grid.append(ligne)
return grid
def affichage_monde(monde):
"""
affiche sur la console le monde ligne par ligne
"""
size = len(monde)
for i in range(size):
for j in range(size):
print(monde[i][j], end=" ")
print()
def voisin(grid, x, y):
"""
compte le nombre de voisins vivants de la cellule de coordonnées (x, y)
"""
somme = 0
for i in range(-1, 2):
for j in range(-1, 2):
somme += grid[x + i][y + j]
return (somme - grid[x][y])
def copie_liste_2D(grid):
"""
renvoie une copie de la liste 2D grid
"""
new_grid = []
size = len(grid)
for i in range(size):
ligne = []
for j in range(size):
ligne.append(grid[i][j])
new_grid.append(ligne)
return new_grid
########### INITIALISATION DE L’ANIMATION
def init_visu() :
'''
Création initiale de l'animation
'''
fig, ax = plt.subplots()
ims = []
plt.axis("off")
return [fig, ax, ims]
########### AJOUT D’UN ETAT A L’ANIMATION
def add_visu(grid, visu) :
'''
Ajout d'un état de l'automate grid à l'animation visu
'''
fig, ax, ims = visu[0], visu[1], visu[2]
im = ax.imshow(grid, animated=True, cmap="ocean")
ims.append([im])
return [fig, ax, ims]
########### AFFICHAGE DE L’ANIMATION
def show_anim(visu) :
'''
lance l'affichage de l'animation dans une nouvelle fenêtre
'''
fig, ims = visu[0], visu[2]
ani = animation.ArtistAnimation(fig, ims, interval=100, blit=True, repeat_delay=1000)
plt.show()
visu.append(ani)
########## PARTIE PRINCIPALE
size = 90
proba = 0.3
nb_round = 500
# Initialisation du monde
grid = init_aleatoire(size, proba)
# Création de l'animation ajout du monde initial
anim = init_visu()
anim = add_visu(grid, anim)
# Pour chaque génération
for n in range(nb_round):
new_grid = init_vide(size)
for i in range(1, size - 1):
for j in range(1, size - 1):
nb = voisin(grid, i, j)
if grid[i][j] == 1:
if nb == 2 or nb == 3:
new_grid[i][j] = 1
else:
if nb > 3:
new_grid[i][j] = 1
grid = copie_liste_2D(new_grid)
# Ajout dans l'animation du nouvel état du monde
anim = add_visu(grid, anim)
# Visualisation de l'animation
show_anim(anim)
To embed this project on your website, copy the following code and paste it into your website's HTML: