shlogg · Early preview
Rakesh Reddy Peddamallu @rakesh678219

Cell State Mapping In Game Of Life Algorithm

Cell State Mapping in Game of Life algorithm: Alive (1), Dead (0), Transitional Values (2, 3). Rules: Cell remains alive with 2 or 3 neighbors, dies with < 2 or > 3. Dead cell becomes alive with exactly 3 neighbors.

Cell State Mapping:


Original State
New State
Transitional Value
Meaning


1 (Alive)
0 (Dead)
2
Cell was alive but will die.

0 (Dead)
1 (Alive)
3
Cell was dead but will become alive.

1 (Alive)
1 (Alive)
1
Cell remains alive.

0 (Dead)
0 (Dead)
0
Cell remains dead.


This clearly shows how each original state transitions using the intermediate values.

const countNeighbours = (r, c, board) => {
    const ROWS = board.length;
    const COLS = board[0].length;
    let count = 0;
    const directions = [
        [0, 1], [1, 0], [0, -1], [-1, 0],  // Horizontal and vertical directions
        [1...