Conway’s game of life

Today we’ll talking about cells and a zero-player game, meaning that its evolution is determined by its initial state, requiring no further input. One interacts with the Game of Life by creating an initial configuration and observing how it evolves.

Bogdan Tudorache
3 min readApr 20, 2023

We will implement it in python and observe the outcome.

The Rules:

  1. Any live cell with fewer than two live neighbors dies, as if by underpopulation.
  2. Any live cell with two or three live neighbors lives on to the next generation.
  3. Any live cell with more than three live neighbors dies, as if by overpopulation.
  4. Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction

The Code:

import numpy as np
import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from PIL import Image

# Set up the initial grid with a glider pattern
grid_size = 50
grid = np.zeros((grid_size, grid_size))
grid[1, 2] = 1
grid[2, 3] = 1
grid[3, 1:4] = 1

# Define the update function for each generation
def evolve(frame_num, grid, grid_size):
new_grid = np.zeros((grid_size, grid_size))
for i in range(grid_size):
for j in range(grid_size):
# Count the number of live neighbors for each cell
neighbors = (grid[i-1:i+2, j-1:j+2].sum()) - grid[i,j]
# Apply the Game of Life rules to determine the new state of each cell
if grid[i,j] == 1 and (neighbors == 2 or neighbors == 3):
new_grid[i,j] = 1
elif grid[i,j] == 0 and neighbors == 3:
new_grid[i,j] = 1
# Update the grid for the next generation
grid[:] = new_grid[:]
# Update the plot with the new grid
mat.set_data(grid)
return mat,

# Set up the animation plot
fig, ax = plt.subplots()
mat = ax.matshow(grid)
ani = animation.FuncAnimation(fig, evolve, frames=100, fargs=(grid, grid_size), interval=50, save_count=50)
plt.show()

The Explanation:

We set up an initial grid with a glider pattern, simulates the Game of Life for 100 generations, and then animates the resulting grid states using Matplotlib. Finally, it saves the animation as an animated GIF file. Here’s a step-by-step walkthrough of the code:

  1. Import necessary libraries:
  • numpy is used for efficient array operations.
  • matplotlib is used for creating the animation and displaying the grid at each generation.
  • PIL (or Pillow, a fork of PIL) is used to save the animation as a GIF.

2. Set up the initial grid:

  • grid_size is set to 50, creating a 50x50 grid.
  • grid is initialized as a 2D NumPy array filled with zeros.
  • A glider pattern is added to the initial grid.

3. Define the evolve function:

  • This function takes three arguments: frame_num (the current frame number), grid (the current grid state), and grid_size (the size of the grid).
  • A new grid (new_grid) is initialized as a 2D NumPy array filled with zeros.
  • The function iterates through all cells in the grid and counts the number of live neighbors.
  • The Game of Life rules are applied to determine the new state of each cell.
  • The new grid state is copied into the original grid.
  • The function updates the plot with the new grid state and returns the updated plot.

4. Set up the animation plot:

  • A Matplotlib figure and axis are created.
  • The initial grid state is displayed using ax.matshow(grid).
  • The animation.FuncAnimation function is called to create an animation with 100 frames, invoking the evolve function for each frame.

5. Display the animation:

  • plt.show() is called to display the animation in a window.

The Video:

Bogdan Tudorache | Founder @ berrynews.org

If you like the article and would like to support me, make sure to:

👏 Clap for the story (50 Claps) to help this article be featured

🔔 Follow me Bogdan Tudorache

📰 Read more coding content on Coding Challenge

🔔 Connect w/ me: LinkedIn | Reddit

--

--

Bogdan Tudorache
Bogdan Tudorache

Written by Bogdan Tudorache

Consistency and Continuity. You can expect weekly tech articles from me. I am a developer, founder and integration engineer

No responses yet