Tuesday, December 14, 2021

Excel Game of Life

In this post we see how to create the Game of Life in Excel using VBA macros. The universe of the game of life is a 2D grid of cells that can either be dead or alive. The Excel worksheet is therefore the ideal layout to create the game. We will use VBA loops and arrays to apply the rules of the game and evolve the initial configuration.

The Game of Life

The Game of Life was introduced by the mathematician John Conway in 1970. It is a cellular automaton that consists of an infinite, two-dimensional grid of squared cells that can have two possible states: dead or alive. Each cell interacts with its eight surrounding neighbours, and evolves according to the following four rules:

  1. Any live cell with fewer than two live neighbours dies, as if by underpopulation.
  2. Any live cell with two or three live neighbours lives on to the next generation.
  3. Any live cell with more than three live neighbours dies, as if by overpopulation.
  4. Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.

 

 

Since its publication, the Game of Life has attracted much interest because of the surprising ways in which the patterns can evolve. There is even a classification of the types of patterns that occur in the game of life. Have a look at the Wikipedia article for more information: Conway’s Game of Life.

 

Initial Configuration

This version of Excel Game of Life uses a numerical notation to determine the state of a cell. A live cell has the value of 1, a dead cell has no value (is empty). We use the Worksheet_SelectionChange event procedure to allow selecting cells and create the initial configuration. When selecting any cell, it assigns the value of 1 if empty (or dead), or removes the value if not empty (or alive).

 
  Private Sub Worksheet_SelectionChange(ByVal Target As Range)
      If ActiveCell.Value = "" Then
           ActiveCell.Value = "1"
      Else
           ActiveCell.Value = ""
      End If
  End Sub
 


The initial configuration using the numerical notation may look like the image below.


The game of life is supposed to span throughout an infinite universe. In this Excel version of the game, we use a limited range instead (GameRng as per code below). We apply conditional formatting to that range in order to highlight the cells equal to 1 with a black interior (live cells).

 
  Dim GameRng As Range
  Set GameRng = ActiveSheet.Range("B2:AZ34")
  With GameRng
      .FormatConditions.Delete
      .FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, Formula1:="=1"
      .FormatConditions(1).Interior.Color = vbBlack
  End With
 


The initial configuration of the Excel Game of Life applying the conditional formatting may look like the image below.

 

Next Generation

The initial configuration evolves with each generation, thus changing the state of certain cells as per the rules of the game explained earlier. The macro ‘AddGeneration” first assigns the values of each cell to an array (GameArr). That creates a two-dimensional array where each item contains either the value 1 or nothing. We do that simply by declaring the array variable as a Variant, and assigning the value of the range to the array (see code below).

 
  Dim GameArr() As Variant
  GameArr = GameRng.Value
 


Then we loop through every cell in the range (GameRng) with a for each loop. Every time we loop, we define another range of surrounding neighbour cells (cellrng), and count the number of live cells using the ConutA worksheet function. Depending on the number of live cells around, we set the state of each cell by following the main rules of the game of life. The changes are made to the array (GameArr), not yet to the worksheet.

     
  If cell.Value = "1" Then
      live = WorksheetFunction.CountIf(cellrng, "1") - 1
      If live < 2 Or live > 3 Then
          GameArr(r - 1, c - 1) = ""
      End If
  Else
      live = WorksheetFunction.CountIf(cellrng, "1")
      If live = 3 Then
          GameArr(r - 1, c - 1) = "1"
      End If
  End If
 

 

When the loop ends, we assign the array values to the worksheet range as indicated below:  

 
  GameRng.Value = GameArr
 

 

The process repeats all over if the game was started (Start button) or when clicking Next. The Next button moves the configuration to the next generation, but just one at a time. The Start button starts the process to add one generation after another. There is a 1 second delay between each generation. If started, it only stops if either all living cells die, or we press the Stop button. Some configurations lead to infinite living (e.g. Oscillators). In such case, the loop does not stop and keeps counting the number of generations in the game of life.

 

 

 Download Excel Game of Life

 

No comments:

Post a Comment

Popular Posts