Concepts / Counting Neighbors on a Grid

Counting Neighbors on a Grid

Each cell's next state depends on whether it is alive or dead in the current time step.

  • Programming

Why Neighbors Decide a Cell's Fate

Conway's Game of Life is a grid of cells, where each cell is either alive or dead. The game does not involve a player making moves. Instead, every cell follows a fixed set of rules that decides whether it will be alive or dead in the next time step. Each new generation is built entirely from those rules applied to the current one.

The central idea of this article is that a cell's next state depends on whether it is alive or dead right now, together with how many living neighbors it has. Once you can count those neighbors correctly for a cell, the rules tell you what happens to that cell next. Before reading the rules, try the question below.

What do you think happens?

A living cell has exactly 1 living neighbor in the current time step. Does it stay alive in the next time step?

  • Yes, it survives

  • No, it dies

Reveal answer
Answer:

No, it dies.

A living cell survives only with 2 or 3 living neighbors. A count of 1 falls in the loneliness condition, which covers 0 or 1 living neighbor, so the cell dies.

Following One Cell Through a Time Step

To decide the fate of a cell, you look at the cell itself and the eight cells surrounding it. Those eight are the cells directly above, below, left, right, and diagonal to it. The cell being examined is not counted as its own neighbor. Only the surrounding cells that are alive contribute to the count.

Top leftrow minus 1, column minus 1Top middlerow minus 1, column 0Top rightrow minus 1, column plus 1Middle leftrow 0, column minus 1Cell being examinedrow 0, column 0Middle rightrow 0, column plus 1Bottom leftrow plus 1, column minus 1Bottom middlerow plus 1, column 0Bottom rightrow plus 1, column plus 1
Which eight surrounding cells are included in the neighbor count for a given cell, and how do their row and column offsets map to the cell's position?

Read the diagram as a 3 by 3 block centered on the cell. Each neighbor is found by adding a row offset and a column offset to the cell's own position. The center square is the cell you are examining, so it is excluded from the count. Your whole job in counting is to check these eight positions, one at a time, and tally the ones that are alive.

The Survival and Reproduction Rules

The rules form one decision that begins with the cell's current state. If the cell is alive, one rule applies. If the cell is dead, a different rule applies. In both cases the next state is decided by the count of living neighbors. The diagram below shows how the branches connect, so you can follow any cell from its current state to its next state.

Current statealive or deadLiving cellsurvival ruleDead cellreproduction ruleStays alivenext state aliveDiesnext state deadBecomes alivenext state aliveRemains deadnext state deadif alive nowif dead now2 or 3 living neighbors0, 1, or more than 3 living neighborsexactly 3 living neighborsany other count
How does a cell move between alive and dead states from the current time step to the next, and which neighbor counts trigger each change?

Follow any branch from left to right to check a cell. The left side of the diagram sorts cells by whether they are alive now. The right side gives the outcome for the next time step, which depends only on the count of living neighbors.

Working Through a Full Generation

The clearest way to internalize the rules is to apply them to a specific cell. The example below is an original illustration built for this article. It uses two cells, one alive and one dead, and asks only what each one does in the next time step.

Worked example

One living cell and one dead cell

Cell A is alive in the current time step and has 2 living neighbors. Cell B is dead in the current time step and has 3 living neighbors. What is each cell's state in the next time step?

  1. 1
    Identify the current state of Cell A

    Cell A is alive, so the survival rule applies to it.

  2. 2
    Count the living neighbors of Cell A

    Cell A has 2 living neighbors. A count of 2 is one of the two surviving counts.

  3. 3
    Decide the next state of Cell A

    Because a living cell with 2 or 3 living neighbors survives, Cell A stays alive in the next time step.

  4. 4
    Identify the current state of Cell B

    Cell B is dead, so the reproduction rule applies to it.

  5. 5
    Count the living neighbors of Cell B

    Cell B has 3 living neighbors. This is exactly the count that triggers reproduction.

  6. 6
    Decide the next state of Cell B

    A dead cell with exactly 3 living neighbors becomes alive, so Cell B is alive in the next time step.

Result

Cell A stays alive and Cell B becomes alive in the next time step.

A grid-wide step must be computed from the current grid. Every cell's next state is decided by looking at the current generation, not at cells that have already been updated during the same step. If you changed one cell and then used its new value when counting for a neighbor, you would be mixing two generations and the result would be wrong.

Mistakes Beginners Make When Counting

  • Common mistake

    Counting the cell itself as one of its own neighbors

    What it looks like

    A living cell with 2 living surrounding cells is counted as having 3, so it survives when it should not.

    Why it fails

    The neighbor count uses the eight surrounding cells only. The examined cell sits at the center of the 3 by 3 block and is not part of its own neighborhood.

    Do this instead

    Check only the eight surrounding positions and leave out the center square.

  • Common mistake

    Using the survival rule for a dead cell

    What it looks like

    A dead cell with 2 living neighbors is treated as alive because 2 is a surviving count.

    Why it fails

    The survival rule applies only to living cells. A dead cell follows the reproduction rule, which needs exactly 3 living neighbors.

    Do this instead

    First read the current state, then choose the rule that matches it.

  • Common mistake

    Treating a count of 4 or more as survival

    What it looks like

    A living cell with 4 living neighbors is kept alive.

    Why it fails

    Only 2 and 3 are surviving counts. Any count above 3 is overcrowding, which causes death.

    Do this instead

    Compare the count against the surviving pair 2 and 3, and treat everything else as death.

  • Common mistake

    Updating cells one at a time while counting

    What it looks like

    A cell becomes alive early in the step, and a later neighbor counts it as alive.

    Why it fails

    Each next state must come from the current grid. Mixing updated and not-yet-updated cells produces a grid that matches no real generation.

    Do this instead

    Read every cell from the current grid first, then write all the next states.

Practice Deciding Next States

EASY

A living cell has 3 living neighbors. A dead cell has 4 living neighbors. A second dead cell has exactly 3 living neighbors. For each of the three cells, state whether it is alive or dead in the next time step and name the rule or condition that decides it.

Hints
  • Start with each cell's current state to choose between the survival rule and the reproduction rule.
  • For the living cell, check whether 3 is one of the two surviving counts.
  • For the dead cells, only an exact count of 3 leads to becoming alive.

Summary of the Two Rules

  1. 1
    A cell's next state depends on whether it is alive or dead now and on how many of its eight surrounding cells are alive.
  2. 2
    A living cell survives with 2 or 3 living neighbors. Fewer than 2 is loneliness, and more than 3 is overcrowding, and both lead to death.
  3. 3
    A dead cell becomes alive only with exactly 3 living neighbors. Any other count leaves it dead.
  4. 4
    Every next state must be computed from the current grid so that one generation is built entirely from the previous one.

At a glance

  • A cell's next state depends on whether it is alive or dead in the current time step and on its count of living neighbors.
  • A living cell survives with 2 or 3 living neighbors; 0 or 1 is loneliness and more than 3 is overcrowding, and both cause death.
  • A dead cell becomes alive only with exactly 3 living neighbors and otherwise remains dead.
  • Every grid-wide step is computed from the current generation, not from cells already updated in that same step.