Conway's Game of Life

Sun Jul 21 2024

Created By JokeySmurf at en.wikipedia

By JokeySmurf at en.wikipedia - Created myself using the tool at www.conwaylife.com, Public Domain, https://commons.wikimedia.org/w/index.php?curid=12854582

Conway's Game of Life, also called the Game of Life, is a type of cellular automaton. It was created by British mathematician John Horton Conway in 1970. The Game of Life is a zero-player game, meaning its evolution is determined by its initial state, needing no further input. The game consists of a grid of cells that can either live, die, or multiply based on simple rules.

The game of life and how to play It

The Game of Life is played on a grid of square cells, each of which can be alive or dead. The state of each cell in the next step is determined by its current state and the states of its eight neighbors (the cells directly horizontally, vertically, or diagonally adjacent).

To understand the game of life and how to play it:

Set Up the Grid: Start with a grid of cells, where each cell is either alive (1) or dead (0).

Apply the Rules: Determine the next state of each cell based on the current state and its neighbors.

Update the Grid: Replace the current grid with the new grid.

Game of life rules

The state of each cell in the grid changes according to these game of life rules:

  • Underpopulation: A live cell with fewer than two live neighbors dies.
  • Survival: A live cell with two or three live neighbors stays alive.
  • Overpopulation: A live cell with more than three live neighbors dies.
  • Reproduction: A dead cell with exactly three live neighbors becomes a live cell.
  • These game of life rules are applied to all cells at the same time, leading to the next generation of the grid.

Sample input

A 5x5 grid where:

  • 1 represents a live cell.
  • 0 represents a dead cell.
python
1initial_state = [
2    [0, 1, 0, 0, 0],
3    [0, 0, 1, 0, 0],
4    [1, 1, 1, 0, 0],
5    [0, 0, 0, 0, 0],
6    [0, 0, 0, 0, 0]
7]

Sample output

The state of the grid after applying the game of life rules for one generation:

python
1next_state = [
2    [0, 0, 0, 0, 0],
3    [1, 0, 1, 0, 0],
4    [0, 1, 1, 0, 0],
5    [0, 1, 0, 0, 0],
6    [0, 0, 0, 0, 0]
7]

Idea to solve

To solve the Game of Life problem, follow these steps:

  • Initialize the Grid: Create a two-dimensional array to represent the grid.
  • Iterate Through the Grid: For each cell in the grid, count the number of live neighbors.
  • Apply the Rules: Based on the number of live neighbors, determine the next state of each cell using the game of life rules.
  • Update the Grid: Create a new grid to store the next state of each cell and update the original grid.

Code implementation

Here is a Python implementation of the Game of Life:

python
1def game_of_life(grid):
2    def count_live_neighbors(x, y):
3        directions = [(-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 1), (1, -1), (1, 0), (1, 1)]
4        count = 0
5        for dx, dy in directions:
6            nx, ny = x + dx, y + dy
7            if 0 <= nx < len(grid) and 0 <= ny < len(grid[0]):
8                count += grid[nx][ny]
9        return count
10
11    next_state = [[0] * len(grid[0]) for _ in range(len(grid))]
12
13    for i in range(len(grid)):
14        for j in range(len(grid[0])):
15            live_neighbors = count_live_neighbors(i, j)
16            if grid[i][j] == 1:
17                if live_neighbors < 2 or live_neighbors > 3:
18                    next_state[i][j] = 0
19                else:
20                    next_state[i][j] = 1
21            else:
22                if live_neighbors == 3:
23                    next_state[i][j] = 1
24
25    return next_state
26
27# Sample Input
28initial_state = [
29    [0, 1, 0, 0, 0],
30    [0, 0, 1, 0, 0],
31    [1, 1, 1, 0, 0],
32    [0, 0, 0, 0, 0],
33    [0, 0, 0, 0, 0]
34]
35
36# Calling the function and printing the result
37next_state = game_of_life(initial_state)
38print("Next State:")
39for row in next_state:
40    print(row)

Explanation of the code

  • count_live_neighbors Function: This helper function counts the number of live neighbors for a given cell by checking all eight possible directions (up, down, left, right, and the four diagonals).
  • Initialize next_state: Create a new grid next_state to store the next state of each cell.
  • Iterate Through the Grid: For each cell in the grid, use count_live_neighbors to determine the number of live neighbors.
  • Apply the Rules: Based on the number of live neighbors and the current state of the cell, determine the next state of the cell using the game of life rules.
  • Update the Grid: Replace the original grid with next_state to reflect the changes.

FAQs

How to play Conway's Game of Life?

What is the rule of Conway's Game of Life?

What is Conway's way of Life?

Why is it called Conway's Game of Life?

Sign-in First to Add Comment

Leave a comment 💬

All Comments

No comments yet.