#Java

Game of Life

2 years ago

127

Conway's Game of Life is a cellular automaton developed by British mathematician John Horton Conway. It's fun to look at as an animation and a popular programming example.

Game Rules

The Games Rules are pretty simple:

  • on each step, count the number of living neighbours for each cell
  • if a cell lives, and has more than 3 or less than 2 neighbours, it dies
  • if a cell is not alive, but has exactly 3 living neighbours, it becomes a live cell

or in code:


int livingNeighbours=numberOfLivingNeighbours(w,h);
if (this.grid[w][h]) {
    if(livingNeighbours<2){
        changed=true;
        grid[w][h]=false;
    }else if(livingNeighbours>3){
        changed=true;
        grid[w][h]=false;
    }
}else if(livingNeighbours==3){
    changed=true;
    grid[w][h]=true;
}


The Result

The result can look rather impressive for such a simple set of rules:



There are several stable and repeating self-sufficient patterns that can form such as "cannons", "glider" and much more.

see the full code on Github