Initially, there is a grid with some cells which may be alive or dead. Our task to generate the next generation of cells based on the following rules:
- Any live cell with fewer than two live neighbors dies, as if caused by under population.
- 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.
Examples:
The ‘*’ represent an alive cell and the ‘.’ represent a dead cell.
Input : .......... ...**..... ....*..... .......... .......... Output: .......... ...**..... ...**..... .......... .......... .......... Input : .......... ...**..... ....*..... .......... .......... ...**..... ..**...... .....*.... ....*..... .......... Output: .......... ...**..... ...**..... .......... .......... ..***..... ..**...... ...**..... .......... ..........
Here is a simple Java implementation of the Game Of Life. Grid in initialized with 0’s representing the dead cells and 1’s representing alive cells. The generate() function loops through every cell and counts it’s neighbors. Based on that values, the aforementioned rules are implemented. The following implementation ignores the edge cells as it supposed to be played on an infinite plane.
Java
// A simple Java program to implement Game of Life public class GameOfLife { public static void main(String[] args) { int M = 10 , N = 10 ; // Intiliazing the grid. int [][] grid = { { 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 }, { 0 , 0 , 0 , 1 , 1 , 0 , 0 , 0 , 0 , 0 }, { 0 , 0 , 0 , 0 , 1 , 0 , 0 , 0 , 0 , 0 }, { 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 }, { 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 }, { 0 , 0 , 0 , 1 , 1 , 0 , 0 , 0 , 0 , 0 }, { 0 , 0 , 1 , 1 , 0 , 0 , 0 , 0 , 0 , 0 }, { 0 , 0 , 0 , 0 , 0 , 1 , 0 , 0 , 0 , 0 }, { 0 , 0 , 0 , 0 , 1 , 0 , 0 , 0 , 0 , 0 }, { 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 } }; // Displaying the grid System.out.println( "Original Generation" ); for ( int i = 0 ; i < M; i++) { for ( int j = 0 ; j < N; j++) { if (grid[i][j] == 0 ) System.out.print( "." ); else System.out.print( "*" ); } System.out.println(); } System.out.println(); nextGeneration(grid, M, N); } // Function to print next generation static void nextGeneration( int grid[][], int M, int N) { int [][] future = new int [M][N]; // Loop through every cell for ( int l = 1 ; l < M - 1 ; l++) { for ( int m = 1 ; m < N - 1 ; m++) { // finding no Of Neighbours that are alive int aliveNeighbours = 0 ; for ( int i = - 1 ; i <= 1 ; i++) for ( int j = - 1 ; j <= 1 ; j++) aliveNeighbours += grid[l + i][m + j]; // The cell needs to be subtracted from // its neighbours as it was counted before aliveNeighbours -= grid[l][m]; // Implementing the Rules of Life // Cell is lonely and dies if ((grid[l][m] == 1 ) && (aliveNeighbours < 2 )) future[l][m] = 0 ; // Cell dies due to over population else if ((grid[l][m] == 1 ) && (aliveNeighbours > 3 )) future[l][m] = 0 ; // A new cell is born else if ((grid[l][m] == 0 ) && (aliveNeighbours == 3 )) future[l][m] = 1 ; // Remains the same else future[l][m] = grid[l][m]; } } System.out.println( "Next Generation" ); for ( int i = 0 ; i < M; i++) { for ( int j = 0 ; j < N; j++) { if (future[i][j] == 0 ) System.out.print( "." ); else System.out.print( "*" ); } System.out.println(); } } } |
C#
// A simple C# program to implement // Game of Life using System; public class GFG { public static void Main() { int M = 10, N = 10; // Intiliazing the grid. int [,] grid = { { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 1, 1, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 1, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 1, 1, 0, 0, 0, 0, 0 }, { 0, 0, 1, 1, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 1, 0, 0, 0, 0 }, { 0, 0, 0, 0, 1, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } }; // Displaying the grid Console.WriteLine( "Original Generation" ); for ( int i = 0; i < M; i++) { for ( int j = 0; j < N; j++) { if (grid[i,j] == 0) Console.Write( "." ); else Console.Write( "*" ); } Console.WriteLine(); } Console.WriteLine(); nextGeneration(grid, M, N); } // Function to print next generation static void nextGeneration( int [,]grid, int M, int N) { int [,] future = new int [M,N]; // Loop through every cell for ( int l = 1; l < M - 1; l++) { for ( int m = 1; m < N - 1; m++) { // finding no Of Neighbours // that are alive int aliveNeighbours = 0; for ( int i = -1; i <= 1; i++) for ( int j = -1; j <= 1; j++) aliveNeighbours += grid[l + i,m + j]; // The cell needs to be subtracted // from its neighbours as it was // counted before aliveNeighbours -= grid[l,m]; // Implementing the Rules of Life // Cell is lonely and dies if ((grid[l,m] == 1) && (aliveNeighbours < 2)) future[l,m] = 0; // Cell dies due to over population else if ((grid[l,m] == 1) && (aliveNeighbours > 3)) future[l,m] = 0; // A new cell is born else if ((grid[l,m] == 0) && (aliveNeighbours == 3)) future[l,m] = 1; // Remains the same else future[l,m] = grid[l,m]; } } Console.WriteLine( "Next Generation" ); for ( int i = 0; i < M; i++) { for ( int j = 0; j < N; j++) { if (future[i,j] == 0) Console.Write( "." ); else Console.Write( "*" ); } Console.WriteLine(); } } } // This code is contributed by Sam007. |
Output:
Original Generation .......... ...**..... ....*..... .......... .......... ...**..... ..**...... .....*.... ....*..... .......... Next Generation .......... ...**..... ...**..... .......... .......... ..***..... ..**...... ...**..... .......... ..........
The above implementation is very basic. Try coming up with a more efficient implementation and be sure to comment it below. Also for fun try creating your own rule for cellular Automata.
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..
This article is contributed by Vaibhav Mehta. If you like GeeksforGeeks and would like to contribute, you can also mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.