I personally wanted to write a Lifegame-like program in Python, so I tried it during the New Year holidays. I also have the idea of "at least one article before the dawn of the pine tree", so I will post a rough explanation and code. As in the past, I searched and wrote about sites here and there, muttered a soliloquy saying "I'm not cool" and corrected it, so there are some unsightly points, but please forgive me.
lifegame.py
import time
import matplotlib.pyplot as plt
import random
#Interactive mode on
plt.ion()
#Setting of each variable
#Graph drawing size
sizeX = sizeY = 1000
#Cell spacing when drawing
#The number of cells on each axis is calculated by dividing the drawing size by the cell spacing.
space = 5
#Depopulation conditions
depop = 1
#Overcrowding conditions
overcrow = 4
#Occurrence condition(lower limit)
occurLower = 3
#Occurrence condition(upper limit)
occurUpper = 3
#Cell persistence flag
eternal = False
#Incidence of initial cells
occurInit = 20 # %
#Size factor when drawing cells
sizeFacter = 5
#Initialization of drawing area
fig, ax = plt.subplots()
ax.set_xlim(0, sizeX + space)
ax.set_ylim(0, sizeY + space)
ax.set_aspect('equal')
ax.grid(False)
#Initialization of cells and drawing data
cells =[]
x = []
y = []
color = []
scale = []
for i in range(1, int(sizeX / space) + 1, 1):
tempCells = []
for j in range(1, int(sizeY / space) + 1, 1):
x.append(j * space)
y.append(i * space)
color.append('b')
if random.randint(1, 100) <= occurInit:
temp = random.randint(0,9)
else:
temp = 0
tempCells.append(temp)
scale.append(temp * sizeFacter)
cells.append(tempCells)
# scatter(Scatter plot)Creating an object
sc = ax.scatter(x, y, c=color, s=scale, alpha=0.3, edgecolors='none')
#Setting an event to end with a mouse click
loop = True
def onclick(event):
print('Program is over.')
global loop
loop = False
cid = fig.canvas.mpl_connect('button_press_event', onclick)
#Cell status check
def checkCell(cells, x, y):
#Coordinate calculation of 8 cells around the cell
cellsAxixes = []
for i in range(-1,2,1):
for j in range(-1,2,1):
cellsAxixes.append({'x': x+i, 'y':y+j})
#The center cell is itself and is not included in the calculation
del cellsAxixes[4]
#Top, bottom, left, right edge correction
for i in cellsAxixes:
if i['x'] < 0:
i['x'] = int(sizeX/space) - 1
elif i['x'] > int(sizeX/space) - 1:
i['x'] = 0
if i['y'] < 0:
i['y'] = int(sizeY/space) - 1
elif i['y'] > int(sizeY/space) - 1:
i['y'] = 0
#Count the number of living cells around
check = 0
for i in cellsAxixes:
if cells[i['y']][i['x']] != 0:
check += 1
return check
# (Own)Cell calculation
def calcCells(cells):
afterCells = []
posY = 0
for i in cells:
posX = 0
tempCells = []
for j in i:
status = checkCell(cells, posX, posY)
#Confirmation of cell survival
if j > 0 and j < 9:
#Overcrowding, depopulation judgment
if status >= overcrow or status <= depop:
j = 0
else:
j += 1
elif j == 9:
#Permanent(eternal)If the flag is False. .. ..
if not eternal:
#Die at the end of life
j = 0
else:
#Occurrence condition judgment
if status <= occurUpper and status >= occurLower:
j = 1
tempCells.append(j)
posX += 1
afterCells.append(tempCells)
posY += 1
return afterCells
#Main loop
while loop:
#Sleep when processing is too fast and you are in trouble(Seconds)
#time.sleep(0.01)
newScale = []
newColor = []
cells = calcCells(cells)
#Create drawing settings based on cell status
for j in cells:
for i in j:
#size
newScale.append(i * sizeFacter)
#color
if i > 0 and i < 4:
newColor.append('b') #Blue
elif i < 8:
newColor.append('g') #Green
else:
newColor.append('r') #Red
#Drawing settings and redrawing
sc.set_sizes(newScale)
sc.set_color(newColor)
sc.figure.canvas.draw_idle()
sc.figure.canvas.flush_events()
Recommended Posts