Curso de introducción a la programación con Python¶

    Autor: Luis Fernando Apáez Álvarez
    -Curso PyM-
    Clase extra: Juego de la vida (Patrones)
    Fecha: 10 de Septiembre del 2022





  • La rana
  • El deslizador
  • El lanzador de abeja reina

Veamos algunos otros patrones interesantes:

Captura7.PNG

Patrón de la rana ¶

In [3]:
# dimensiones de la matriz
filas  =  10
columnas = 10

# Creamos una lista vacía
tablero  =  []

# agregamos el valor de False en todas las entradas
for  i  in  range(filas):
    tablero.append ([False] * columnas)

# asignaremos el valor de True a las siguientes entradas para dibujar el patrón de la rana
    
tablero[4][3] = True
tablero[4][4] = True
tablero[4][5] = True
tablero[5][4] = True
tablero[5][5] = True
tablero[5][6] = True
    
# Tablero y pulsos

pulsos = 6

for t in range(pulsos):
    
    # Preparamos un nuevo tablero
    
    tablero2 = []
    for i in range(filas):
        tablero2.append([0] * columnas)
    
    # mostramos los pulsos
    
    print("Pulso: ", t + 1)
    
    # Actualizamos el tablero
    
    for y in range(filas):
        for x in range(columnas):
            
            # número de vecinos 

            n = 0
            
            if y > 0 and x > 0 and tablero[y-1][x-1]:
                n += 1
            if x > 0 and tablero[y][x-1]:
                n += 1
            if y < filas - 1 and x > 0 and tablero[y+1][x-1]:
                n += 1
            if y > 0 and tablero[y-1][x]:
                n += 1
            if y < filas - 1 and tablero[y+1][x]:
                n += 1
            if y > 0 and x < columnas - 1 and tablero[y-1][x+1]:
                n += 1
            if x < columnas - 1 and tablero[y][x+1]:
                n += 1
            if y < filas - 1 and x < columnas - 1 and tablero[y+1][x+1]:
                n += 1
            
                # Supervivencia
              
            if tablero[y][x] and (n == 2 or n ==3):
                tablero2[y][x] = True
            
                # Nacimiento
            
            elif not tablero[y][x] and n == 3:
                tablero2[y][x] = True
            
                # Superpoblacíon y aislamiento
            
            else:
                tablero2[y][x] = False

    # Actualizamos el tablero

    tablero = tablero2
    
    # mostramos el tablero
            
    for y in range(filas):
        for x in range(columnas):
            if tablero[y][x]:
                print("*", end = " ")
            else:
                print(".", end = " ")
        print()
Pulso:  1
. . . . . . . . . . 
. . . . . . . . . . 
. . . . . . . . . . 
. . . . * . . . . . 
. . . * . . * . . . 
. . . * . . * . . . 
. . . . . * . . . . 
. . . . . . . . . . 
. . . . . . . . . . 
. . . . . . . . . . 
Pulso:  2
. . . . . . . . . . 
. . . . . . . . . . 
. . . . . . . . . . 
. . . . . . . . . . 
. . . * * * . . . . 
. . . . * * * . . . 
. . . . . . . . . . 
. . . . . . . . . . 
. . . . . . . . . . 
. . . . . . . . . . 
Pulso:  3
. . . . . . . . . . 
. . . . . . . . . . 
. . . . . . . . . . 
. . . . * . . . . . 
. . . * . . * . . . 
. . . * . . * . . . 
. . . . . * . . . . 
. . . . . . . . . . 
. . . . . . . . . . 
. . . . . . . . . . 
Pulso:  4
. . . . . . . . . . 
. . . . . . . . . . 
. . . . . . . . . . 
. . . . . . . . . . 
. . . * * * . . . . 
. . . . * * * . . . 
. . . . . . . . . . 
. . . . . . . . . . 
. . . . . . . . . . 
. . . . . . . . . . 
Pulso:  5
. . . . . . . . . . 
. . . . . . . . . . 
. . . . . . . . . . 
. . . . * . . . . . 
. . . * . . * . . . 
. . . * . . * . . . 
. . . . . * . . . . 
. . . . . . . . . . 
. . . . . . . . . . 
. . . . . . . . . . 
Pulso:  6
. . . . . . . . . . 
. . . . . . . . . . 
. . . . . . . . . . 
. . . . . . . . . . 
. . . * * * . . . . 
. . . . * * * . . . 
. . . . . . . . . . 
. . . . . . . . . . 
. . . . . . . . . . 
. . . . . . . . . . 

Deslizador ¶

In [12]:
# dimensiones de la matriz
filas  =  10
columnas = 10

# Creamos una lista vacía
tablero  =  []

# agregamos el valor de False en todas las entradas
for  i  in  range(filas):
    tablero.append ([False] * columnas)

# asignaremos el valor de True a las siguientes entradas para dibujar el patrón del deslizador
    
tablero[4][3] = True
tablero[4][5] = True
tablero[5][4] = True
tablero[5][5] = True
tablero[6][4] = True
    
# Tablero y pulsos

pulsos = 10

for t in range(pulsos):
    
    # Preparamos un nuevo tablero
    
    tablero2 = []
    for i in range(filas):
        tablero2.append([0] * columnas)
    
    # mostramos los pulsos
    
    print("Pulso: ", t + 1)
    
    # Actualizamos el tablero
    
    for y in range(filas):
        for x in range(columnas):
            
            # número de vecinos 

            n = 0
            
            if y > 0 and x > 0 and tablero[y-1][x-1]:
                n += 1
            if x > 0 and tablero[y][x-1]:
                n += 1
            if y < filas - 1 and x > 0 and tablero[y+1][x-1]:
                n += 1
            if y > 0 and tablero[y-1][x]:
                n += 1
            if y < filas - 1 and tablero[y+1][x]:
                n += 1
            if y > 0 and x < columnas - 1 and tablero[y-1][x+1]:
                n += 1
            if x < columnas - 1 and tablero[y][x+1]:
                n += 1
            if y < filas - 1 and x < columnas - 1 and tablero[y+1][x+1]:
                n += 1
            
                # Supervivencia
              
            if tablero[y][x] and (n == 2 or n ==3):
                tablero2[y][x] = True
            
                # Nacimiento
            
            elif not tablero[y][x] and n == 3:
                tablero2[y][x] = True
            
                # Superpoblacíon y aislamiento
            
            else:
                tablero2[y][x] = False

    # Actualizamos el tablero

    tablero = tablero2
    
    # mostramos el tablero
            
    for y in range(filas):
        for x in range(columnas):
            if tablero[y][x]:
                print("*", end = " ")
            else:
                print(".", end = " ")
        print()
Pulso:  1
. . . . . . . . . . 
. . . . . . . . . . 
. . . . . . . . . . 
. . . . . . . . . . 
. . . . . * . . . . 
. . . * . * . . . . 
. . . . * * . . . . 
. . . . . . . . . . 
. . . . . . . . . . 
. . . . . . . . . . 
Pulso:  2
. . . . . . . . . . 
. . . . . . . . . . 
. . . . . . . . . . 
. . . . . . . . . . 
. . . . * . . . . . 
. . . . . * * . . . 
. . . . * * . . . . 
. . . . . . . . . . 
. . . . . . . . . . 
. . . . . . . . . . 
Pulso:  3
. . . . . . . . . . 
. . . . . . . . . . 
. . . . . . . . . . 
. . . . . . . . . . 
. . . . . * . . . . 
. . . . . . * . . . 
. . . . * * * . . . 
. . . . . . . . . . 
. . . . . . . . . . 
. . . . . . . . . . 
Pulso:  4
. . . . . . . . . . 
. . . . . . . . . . 
. . . . . . . . . . 
. . . . . . . . . . 
. . . . . . . . . . 
. . . . * . * . . . 
. . . . . * * . . . 
. . . . . * . . . . 
. . . . . . . . . . 
. . . . . . . . . . 
Pulso:  5
. . . . . . . . . . 
. . . . . . . . . . 
. . . . . . . . . . 
. . . . . . . . . . 
. . . . . . . . . . 
. . . . . . * . . . 
. . . . * . * . . . 
. . . . . * * . . . 
. . . . . . . . . . 
. . . . . . . . . . 
Pulso:  6
. . . . . . . . . . 
. . . . . . . . . . 
. . . . . . . . . . 
. . . . . . . . . . 
. . . . . . . . . . 
. . . . . * . . . . 
. . . . . . * * . . 
. . . . . * * . . . 
. . . . . . . . . . 
. . . . . . . . . . 
Pulso:  7
. . . . . . . . . . 
. . . . . . . . . . 
. . . . . . . . . . 
. . . . . . . . . . 
. . . . . . . . . . 
. . . . . . * . . . 
. . . . . . . * . . 
. . . . . * * * . . 
. . . . . . . . . . 
. . . . . . . . . . 
Pulso:  8
. . . . . . . . . . 
. . . . . . . . . . 
. . . . . . . . . . 
. . . . . . . . . . 
. . . . . . . . . . 
. . . . . . . . . . 
. . . . . * . * . . 
. . . . . . * * . . 
. . . . . . * . . . 
. . . . . . . . . . 
Pulso:  9
. . . . . . . . . . 
. . . . . . . . . . 
. . . . . . . . . . 
. . . . . . . . . . 
. . . . . . . . . . 
. . . . . . . . . . 
. . . . . . . * . . 
. . . . . * . * . . 
. . . . . . * * . . 
. . . . . . . . . . 
Pulso:  10
. . . . . . . . . . 
. . . . . . . . . . 
. . . . . . . . . . 
. . . . . . . . . . 
. . . . . . . . . . 
. . . . . . . . . . 
. . . . . . * . . . 
. . . . . . . * * . 
. . . . . . * * . . 
. . . . . . . . . . 

El lanzador de abeja reina ¶

In [13]:
# dimensiones de la matriz
filas  =  15
columnas = 15

# Creamos una lista vacía
tablero  =  []

# agregamos el valor de False en todas las entradas
for  i  in  range(filas):
    tablero.append ([False] * columnas)

# asignaremos el valor de True a las siguientes entradas para dibujar el patrón del 
# lanzador de abeja reina
    
tablero[5][3] = True
tablero[5][4] = True
tablero[5][5] = True
tablero[6][6] = True
tablero[7][6] = True
tablero[8][6] = True
tablero[9][5] = True
tablero[10][4] = True
tablero[10][3] = True
    
# Tablero y pulsos

pulsos = 7

for t in range(pulsos):
    
    # Preparamos un nuevo tablero
    
    tablero2 = []
    for i in range(filas):
        tablero2.append([0] * columnas)
    
    # mostramos los pulsos
    
    print("Pulso: ", t + 1)
    
    # Actualizamos el tablero
    
    for y in range(filas):
        for x in range(columnas):
            
            # número de vecinos 

            n = 0
            
            if y > 0 and x > 0 and tablero[y-1][x-1]:
                n += 1
            if x > 0 and tablero[y][x-1]:
                n += 1
            if y < filas - 1 and x > 0 and tablero[y+1][x-1]:
                n += 1
            if y > 0 and tablero[y-1][x]:
                n += 1
            if y < filas - 1 and tablero[y+1][x]:
                n += 1
            if y > 0 and x < columnas - 1 and tablero[y-1][x+1]:
                n += 1
            if x < columnas - 1 and tablero[y][x+1]:
                n += 1
            if y < filas - 1 and x < columnas - 1 and tablero[y+1][x+1]:
                n += 1
            
                # Supervivencia
              
            if tablero[y][x] and (n == 2 or n ==3):
                tablero2[y][x] = True
            
                # Nacimiento
            
            elif not tablero[y][x] and n == 3:
                tablero2[y][x] = True
            
                # Superpoblacíon y aislamiento
            
            else:
                tablero2[y][x] = False

    # Actualizamos el tablero

    tablero = tablero2
    
    # mostramos el tablero
            
    for y in range(filas):
        for x in range(columnas):
            if tablero[y][x]:
                print("*", end = " ")
            else:
                print(".", end = " ")
        print()
Pulso:  1
. . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . 
. . . . * . . . . . . . . . . 
. . . . * * . . . . . . . . . 
. . . . * . * . . . . . . . . 
. . . . . * * * . . . . . . . 
. . . . . * * . . . . . . . . 
. . . . * * . . . . . . . . . 
. . . . * . . . . . . . . . . 
. . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . 
Pulso:  2
. . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . 
. . . . * * . . . . . . . . . 
. . . * * . . . . . . . . . . 
. . . . * . . * . . . . . . . 
. . . . * . . * . . . . . . . 
. . . . . . . * . . . . . . . 
. . . . * . * . . . . . . . . 
. . . . * * . . . . . . . . . 
. . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . 
Pulso:  3
. . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . 
. . . * * * . . . . . . . . . 
. . . * . . . . . . . . . . . 
. . . . * * . . . . . . . . . 
. . . . . . * * * . . . . . . 
. . . . . * * * . . . . . . . 
. . . . * . * . . . . . . . . 
. . . . * * . . . . . . . . . 
. . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . 
Pulso:  4
. . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . 
. . . . * . . . . . . . . . . 
. . . * * . . . . . . . . . . 
. . . * . . . . . . . . . . . 
. . . . * * * * . . . . . . . 
. . . . * . . . * . . . . . . 
. . . . . . . . * . . . . . . 
. . . . * . . * . . . . . . . 
. . . . * * . . . . . . . . . 
. . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . 
Pulso:  5
. . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . 
. . . * * . . . . . . . . . . 
. . . * * . . . . . . . . . . 
. . . * . . * . . . . . . . . 
. . . * * * * * . . . . . . . 
. . . . * . * . * . . . . . . 
. . . . . . . * * . . . . . . 
. . . . * * . . . . . . . . . 
. . . . * * . . . . . . . . . 
. . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . 
Pulso:  6
. . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . 
. . . * * . . . . . . . . . . 
. . * . . * . . . . . . . . . 
. . * . . . * * . . . . . . . 
. . . * . . . . . . . . . . . 
. . . * * . . . * . . . . . . 
. . . . * . * * * . . . . . . 
. . . . * * * . . . . . . . . 
. . . . * * . . . . . . . . . 
. . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . 
Pulso:  7
. . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . 
. . . * * . . . . . . . . . . 
. . * . * * * . . . . . . . . 
. . * * . . * . . . . . . . . 
. . * * * . . * . . . . . . . 
. . . * * * . . * . . . . . . 
. . . . . . * . * . . . . . . 
. . . * . . . . . . . . . . . 
. . . . * . * . . . . . . . . 
. . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . .