This repository contains Python scripts demonstrating two main concepts:
- Conway’s Game of Life – a classic cellular automaton where cells evolve based on neighbor counts.
- Random Walk – a simple demonstration of cells moving at random on a grid.
All scripts target Python 3 and use NumPy and matplotlib for array operations and animations.
The Game of Life scripts allow you to configure and visualize various initial patterns (random, beacon, glider, glider gun, etc.) and watch them evolve for a specified number of iterations.
Key files:
-
gol.py:neighbours(i, j, world): Determines the next state of a cell based on how many neighbors it has.gen(world): Creates the next iteration of the entire grid.animate(world, nt, rep): Usesmatplotlib.animationto produce an animation overntiterations, optionally repeating.start(): Interactively prompts for grid size, pattern type, and iteration count, then runs the animation.
-
grid.py:MakeGridclass with methods (randomGrid,beacon,glider,gliderGun, etc.) to create different initial states of the grid.
- Python 3.x
- NumPy
- matplotlib
-
Install dependencies (if needed):
pip install numpy matplotlib
-
Run the script:
python gol.py- Respond to prompts: Number of columns, number of rows Grid type (0 for random, 1 for beacon, 2 for glider, 3 for glider gun, etc.) Number of iterations Whether to repeat the animation (yes/no)
A window opens showing the grid evolving. Note: Large grids or large iteration counts can slow down the animation.
The Random Walk script shows a cell with value 1 moving around on a 2D grid, leaving a trail of 2s. Each time step, the script randomly picks a direction (up, down, left, or right) and moves that cell accordingly.
Key points:
makeGrid(): Creates a 60×60 grid with one cell set to1in the center.gen(world): Picks a random direction for any1cell, moves it, and marks previous positions with2.animate(world, nt): Animatesntframes usingmatplotlib.animation.
- Python 3.x
- NumPy
- matplotlib
-
Install dependencies (if not already done):
pip install numpy matplotlib
-
Run the script:
python randwalk.py- The script will:
- Create a 60×60 grid (one cell set to 1)
- Animate 80 steps (or however many you configure), updating the grid each frame
- Display the result as a simple random movement pattern
- A window will open showing the random walk in progress.