Conway’s Game Of Life (Python Implementation)
Conways’s Game Of Life is a Cellular Automation Method created by John Conway. This game was created with Biology in mind but has been applied in various fields such as Graphics, terrain generation,etc..
The “game” is 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, or, for advanced “players”, by creating patterns with particular properties.
How the game works
Because the Game of Life is built on a grid of nine squares, every cell has eight neighboring cells,as shown in the given figure. A given cell (i, j) in the simulation is accessed on a grid [i][j], where i and j are the row and column indices, respectively. The value of a given cell at a given instant of time depends on the state of its neighbors at the previous time step. Conway’s Game of Life has four rules.
- If a cell is ON and has fewer than two neighbors that are ON, it turns OFF
- If a cell is ON and has either two or three neighbors that are ON, it remains ON.
- If a cell is ON and has more than three neighbors that are ON, it turns OFF.
- If a cell is OFF and has exactly three neighbors that are ON, it turns ON.
So since we know how it works, the next thing we need to figure it out that how to make it work.
- Initialize the cells in the grid.
- At each time step in the simulation, for each cell (i, j) in the grid, do the following:
- Update the value of cell (i, j) based on its neighbors, taking into account the boundary conditions.
- Update the display of grid values.
After we done here lets get our hands on code.
Implementation: Now lets get it started
Without passing any command line arguments.
Now lets turn up things a little, let’s see what happens if add updates the animation every 500 milliseconds and setting up the dimensions 32X32 and also using the initial glider pattern.
python 'filename.py' --grid-size 32 --interval 500 --glide
You can try manipulating this code to create different simulation using this.
- Github Code for this article
- Book: Python Playground: Geeky Projects for the Curious Programmer
- docs matplotlib
This article is contributed by Subhajit Saha. If you like GeeksforGeeks and would like to contribute, you can also mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.