Skip to content

Instantly share code, notes, and snippets.

@pablovv72
Last active April 14, 2020 17:05
Show Gist options
  • Save pablovv72/dc3627656b7c27a87ad1f0a241c42587 to your computer and use it in GitHub Desktop.
Save pablovv72/dc3627656b7c27a87ad1f0a241c42587 to your computer and use it in GitHub Desktop.
# John Horton Conway Tribute 1937 - 2020
try:
from console import clear # Define "clear" para iOS
except:
import os
if os.name == 'posix':
def clear(): # Unix, Linux, Mac o
os.system('clear')
else:
def clear(): # Windows
os.system('cls')
from random import randint
ANCHO = 53 # Tamaño del mundo.
ALTO = 27
SW, SH = 5, 5 # Define area de siembra.
SX, SY = (ANCHO - SW) // 2, (ALTO - SH) // 2
POBLACION = 15
ESTABLE = 10 # Número de ciclos para determinar si la célula es estable.
mundo = [[0 for y in range(ALTO)] for x in range(ANCHO)]
for _ in range(POBLACION): # Siembra.
while True:
x = randint(SX, SX + SW - 1)
y = randint(SY, SY + SH - 1)
if mundo[x][y]:
continue
mundo[x][y] = 1
break
while True: # El ciclo de la vida.
pantalla = ''
for y in range(ALTO):
for x in range(ANCHO):
if mundo[x][y]:
pantalla += '🔴' if mundo[x][y] < ESTABLE else '🟢'
else:
pantalla += '✖️'
pantalla += '\n'
clear()
print(pantalla[:-1]) # Muestra el mundo.
for y in range(ALTO):
for x in range(ANCHO):
vivas = 0
for yy in range(y - 1, y + 2):
for xx in range(x - 1, x + 2):
if xx >= ANCHO or xx < 0 or yy >= ALTO or yy < 0:
continue
if mundo[xx][yy]:
vivas += 1
if mundo[x][y]: # Si la célula está viva
if vivas < 3 or vivas > 4: # y tiene menos de 2 o más de 3
mundo[x][y] = 0 # vecinas entonces muere.
elif mundo[x][y] < ESTABLE:
mundo[x][y] += 1
else: # Si está muerta
if vivas == 3: # y tiene 3 vecinas vivas entonces
mundo[x][y] = 1 # vive.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment