Have you ever wanted to create your own simulation of life using Python? Look no further than Conway’s Game of Life! It’s a fascinating cellular automaton that simulates the life and death of cells based on a few simple rules. In this article, I’ll guide you through the process of creating your very own Game of Life using Python.
Understanding Conway’s Game of Life
Before we dive into coding, let’s understand the basic principles behind Conway’s Game of Life. The game takes place on a grid of cells, where each cell can be in one of two states: alive or dead. The state of each cell is determined by its neighboring cells. Here are the rules:
- Any live cell with fewer than two live neighbors dies, as if by underpopulation.
- Any live cell with two or three live neighbors lives on to the next generation.
- Any live cell with more than three live neighbors dies, as if by overpopulation.
- Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.
With just these four simple rules, we can create infinitely complex patterns and simulate the emergence of life-like behavior.
Setting Up the Environment
First, we need to set up our programming environment. Make sure you have Python installed on your computer, and then create a new Python file to work with. I’ll be using the terminal and a text editor, but you can use any Python IDE of your choice.
Once you have your environment ready, let’s start by importing the necessary modules:
import numpy as np
import matplotlib.pyplot as plt
We’ll be using the NumPy module to handle the grid of cells, and the Matplotlib module to visualize the simulation.
Creating the Grid
Next, we’ll define a function to create the initial grid of cells. We’ll represent the grid as a 2D NumPy array, where each element represents the state of a cell (0 for dead, 1 for alive).
def create_grid(size):
grid = np.zeros((size, size))
return grid
Here, the size
parameter determines the dimensions of the grid. Feel free to adjust it to your liking.
Random Initialization
Now that we have our grid, let’s randomly initialize the cells’ states. We’ll define another function for this:
def init_grid(grid):
for i in range(grid.shape[0]):
for j in range(grid.shape[1]):
grid[i][j] = np.random.choice([0, 1])
This function iterates over each cell in the grid and assigns a random state (dead or alive) to it.
Updating the Grid
Now comes the exciting part: updating the grid based on the rules of the game. We’ll define a function that takes in the current grid and returns the next generation of cells:
def update_grid(grid):
new_grid = np.zeros_like(grid)
for i in range(grid.shape[0]):
for j in range(grid.shape[1]):
num_neighbors = count_neighbors(grid, i, j)
if grid[i][j] == 1:
if num_neighbors < 2 or num_neighbors > 3:
new_grid[i][j] = 0
else:
new_grid[i][j] = 1
else:
if num_neighbors == 3:
new_grid[i][j] = 1
return new_grid
This function creates a new grid called new_grid
and applies the rules of the game to each cell in the current grid. The count_neighbors
function counts the number of live neighbors for a given cell.
Counting Neighbors
Let’s implement the count_neighbors
function:
def count_neighbors(grid, i, j):
count = 0
for x in range(max(0, i-1), min(i+2, grid.shape[0])):
for y in range(max(0, j-1), min(j+2, grid.shape[1])):
if not (x == i and y == j):
count += grid[x][y]
return count
This function checks the eight neighbors surrounding a given cell and counts the number of alive cells among them.
Running the Simulation
Now that we have all the necessary functions, let’s run the simulation and visualize the results. We’ll create a main function to tie everything together:
def main(size, generations):
grid = create_grid(size)
init_grid(grid)
for _ in range(generations):
plt.imshow(grid, cmap='binary')
plt.title('Conway\'s Game of Life')
plt.pause(0.1)
grid = update_grid(grid)
plt.show()
Here, the size
parameter determines the size of the grid, and the generations
parameter sets the number of iterations to run the simulation for.
Finally, we call the main function with the desired grid size and number of generations:
size = 50
generations = 100
main(size, generations)
Conclusion
And there you have it! You’ve just created your very own Conway’s Game of Life simulation in Python. Have fun experimenting with different initial states and observing the mesmerizing patterns that emerge.
This project is a great opportunity to explore the concepts of cellular automata and practice your Python skills. I hope you enjoyed this journey into the world of Conway’s Game of Life!