Le jeu de la vie

Jeu de la vie

Vidéo de Science Étonnante : Le jeu de la Vie

from copy import deepcopy
import numpy as np
import matplotlib.pyplot as plt

N=80

def print_grid(l):
    plt.clf()
    plt.imshow(l,cmap='magma')
    plt.draw()

def voisins(l,i,j):
    if i==0 and j==0:
        return [(i+1,j),(i+1,j+1),(i,j+1)]
    elif i==N-1 and j==N-1:
        return [(i-1,j-1),(i-1,j),  (i,j-1)]
    elif i==N-1 and j==0:
        return [(i-1,j),(i-1,j+1),(i,j+1)]
    elif i==0 and j==N-1:
        return [(i+1,j-1),(i+1,j),(i,j-1)]
    elif i==0:
        return [(i+1,j-1),(i+1,j),(i+1,j+1),(i,j-1),(i,j+1)]
    elif i==N-1:
        return [(i-1,j-1),(i-1,j),(i-1,j+1),(i,j-1),(i,j+1)]
    elif j==0:
        return [(i+1,j),(i+1,j+1),(i-1,j),(i-1,j+1),(i,j+1)]
    elif j==N-1:
        return [(i+1,j-1),(i+1,j),(i-1,j-1),(i-1,j),(i,j-1)]
    else:
        return [(i+1,j-1),(i+1,j),(i+1,j+1),(i-1,j-1),(i-1,j),(i-1,j+1),(i,j-1),(i,j+1)]

def nbr_voisins(l,i,j):
    c=0
    for u,v in voisins(l,i,j):
        if l[u,v]==0:
            c+=1
    return c

def etape_jeu(l):
    ll=deepcopy(l)
    for i in range(N):
        for j in range(N):
            v=nbr_voisins(ll,i,j)
            if ll[i,j]==0:
                if not (v==2 or v==3):
                    l[i,j]=1
            else:
                if v==3:
                    l[i,j]=0

def jeu(l,n,t=0.01):
    print_grid(l)
    plt.show()
    for i in range(n):
        print_grid(l)
        etape_jeu(l)
        plt.pause(t)

def glider():
    l = np.ones([N,N])
    centre=N//2
    l[centre,centre+1]=0
    l[centre+1,centre]=0
    l[centre-1,centre-1]=0
    l[centre,centre-1]=0
    l[centre+1,centre-1]=0
    return(l)

def spaceship():
    l = np.ones([N,N])
    c=N//2
    l[c,c-2]=0
    l[c-3,c-2]=0
    l[c-3,c]=0
    l[c-2,c+1]=0
    l[c-1,c+1]=0
    l[c,c+1]=0
    l[c+1,c+1]=0
    l[c+1,c]=0
    l[c+1,c-1]=0
    return(l)

def canon():
    l = np.ones([N,N])
    c=N//2
    i,j=0,N-1
    l[i,j-4]=0
    l[i,j-5]=0
    l[i+1,j-4]=0
    l[i+1,j-5]=0
    l[i+10,j-4]=0
    l[i+10,j-5]=0
    l[i+10,j-6]=0
    l[i+11,j-3]=0
    l[i+11,j-7]=0
    l[i+12,j-2]=0
    l[i+12,j-8]=0
    l[i+13,j-2]=0
    l[i+13,j-8]=0
    l[i+14,j-5]=0
    l[i+15,j-3]=0
    l[i+15,j-7]=0
    l[i+16,j-4]=0
    l[i+16,j-5]=0
    l[i+16,j-6]=0
    l[i+17,j-5]=0
    l[i+20,j-2]=0
    l[i+20,j-3]=0
    l[i+20,j-4]=0
    l[i+21,j-2]=0
    l[i+21,j-3]=0
    l[i+21,j-4]=0
    l[i+22,j-1]=0
    l[i+22,j-5]=0
    l[i+24,j]=0
    l[i+24,j-1]=0
    l[i+24,j-5]=0
    l[i+24,j-6]=0
    l[i+34,j-2]=0
    l[i+34,j-3]=0
    l[i+35,j-2]=0
    l[i+35,j-3]=0
    return l

def oscil2():
    l = np.ones([N,N])
    c=N//2
    l[c,c]=0
    l[c,c-1]=0
    l[c,c+1]=0
    return(l)

def carre():
    l = np.ones([N,N])
    c=N//2
    l[c,c]=0
    l[c+1,c]=0
    l[c,c+1]=0
    l[c+1,c+1]=0
    return l

def rU():
    l = np.ones([N,N])
    c=N//2
    l[c,c+1]=0
    l[c-1,c-1]=0
    l[c-1,c]=0
    l[c-1,c+1]=0
    l[c+1,c-1]=0
    l[c+1,c]=0
    l[c+1,c+1]=0
    return l

def pulsar():
    l = np.ones([N,N])
    c=N//2
    l[c-1,c-2]=0
    l[c-1,c-3]=0
    l[c-1,c-4]=0
    l[c-1,c+2]=0
    l[c-1,c+3]=0
    l[c-1,c+4]=0
    l[c+1,c-2]=0
    l[c+1,c-3]=0
    l[c+1,c-4]=0
    l[c+1,c+2]=0
    l[c+1,c+3]=0
    l[c+1,c+4]=0
    l[c-6,c-2]=0
    l[c-6,c-3]=0
    l[c-6,c-4]=0
    l[c-6,c+2]=0
    l[c-6,c+3]=0
    l[c-6,c+4]=0
    l[c+6,c-2]=0
    l[c+6,c-3]=0
    l[c+6,c-4]=0
    l[c+6,c+2]=0
    l[c+6,c+3]=0
    l[c+6,c+4]=0
    l[c-4,c+1]=0
    l[c-3,c+1]=0
    l[c-2,c+1]=0
    l[c+4,c+1]=0
    l[c+3,c+1]=0
    l[c+2,c+1]=0
    l[c-4,c-1]=0
    l[c-3,c-1]=0
    l[c-2,c-1]=0
    l[c+4,c-1]=0
    l[c+3,c-1]=0
    l[c+2,c-1]=0
    l[c-4,c+6]=0
    l[c-3,c+6]=0
    l[c-2,c+6]=0
    l[c+4,c+6]=0
    l[c+3,c+6]=0
    l[c+2,c+6]=0
    l[c-4,c-6]=0
    l[c-3,c-6]=0
    l[c-2,c-6]=0
    l[c+4,c-6]=0
    l[c+3,c-6]=0
    l[c+2,c-6]=0
    return l

def pentadecathlon():
    l = np.ones([N,N])
    c=N//2
    l[c,c+3]=0
    l[c,c+1]=0
    l[c,c]=0
    l[c,c-1]=0
    l[c,c-2]=0
    l[c,c-4]=0
    l[c-1,c+3]=0
    l[c-1,c+2]=0
    l[c-1,c+1]=0
    l[c-1,c+0]=0
    l[c-1,c-1]=0
    l[c-1,c-2]=0
    l[c-1,c-3]=0
    l[c-1,c-4]=0
    l[c+1,c+3]=0
    l[c+1,c+2]=0
    l[c+1,c+1]=0
    l[c+1,c+0]=0
    l[c+1,c-1]=0
    l[c+1,c-2]=0
    l[c+1,c-3]=0
    l[c+1,c-4]=0
    return l

def puffer():
    l = np.ones([N,N])
    c=N//2
    l[c-2,c+2]=0
    l[c-2,c+1]=0
    l[c-2,c-2]=0
    l[c-1,c+2]=0
    l[c-1,c-1]=0
    l[c,c+2]=0
    l[c,c-1]=0
    l[c,c-2]=0
    l[c+1,c]=0
    l[c+2,c+2]=0
    l[c+2,c]=0
    l[c+2,c-1]=0
    l[c+2,c-2]=0
    return l

def doublecanon():
    return np.logical_and(canon(),np.transpose(canon()))

def quadcanon():
    return np.logical_and(doublecanon(),np.flip(doublecanon(),1))