Home >Web Front-end >JS Tutorial >Conway's 'Game of Life”

Conway's 'Game of Life”

尊渡假赌尊渡假赌尊渡假赌
尊渡假赌尊渡假赌尊渡假赌Original
2025-02-22 08:49:09391browse

This article explores John Conway's "Game of Life," a classic cellular automaton, and demonstrates how to implement a minimalist version using HTML, CSS, and JavaScript within a web browser. Traditionally a programming exercise, this approach leverages modern browser capabilities for a streamlined experience.

Core Concepts:

The Game of Life simulates life forms on a grid. Each cell is either "alive" (populated) or "dead" (empty). The next generation's state is determined by simple rules based on each cell's neighbors:

  • Birth: A dead cell with exactly three live neighbors becomes alive.
  • Survival: A live cell with two or three live neighbors survives.
  • Death: A live cell with fewer than two or more than three live neighbors dies.

This leads to diverse outcomes: extinction, stable populations, oscillating patterns, or unpredictable evolution.

Implementation:

A basic web-based implementation uses an HTML canvas to display the grid. A button triggers the calculation and display of the next generation. The core logic involves iterating through the grid, counting neighbors, and applying the rules to determine each cell's next state. The code uses JavaScript arrays to manage cell states.

Conway's “Game of Life”

Example Code Snippet (JavaScript):

<code class="language-javascript">adjacent = countAdjacent(i, j);
switch (generationThis[i][j]) {
  case 0: // Dead cell
       if (adjacent == 3) {
         generationNext[i][j] = 1; // Birth
       }
       break;
  case 1: // Live cell
       if (adjacent == 2 || adjacent == 3) {
         generationNext[i][j] = 1; // Survival
       }
       break;
}</code>

User Interface:

The simple UI consists of:

  • A <canvas></canvas> element to render the grid.
  • A button ("generate next") to advance to the next generation.

HTML Example:

<code class="language-html"><button type="button" value="generate next" id="btnNext">Generate Next</button>
<canvas id="gameboard"></canvas></code>

The JavaScript code handles the game logic, including initializing the grid (seed generation), calculating subsequent generations, and updating the canvas display. Detecting stable states (extinction, steady state, oscillation) can be implemented by comparing successive generations.

Further Exploration:

While this article presents a minimalist version, more complex implementations could incorporate features like user input for initial grid configuration, multiple life forms, and more sophisticated rules. The Game of Life's simplicity belies its surprising complexity and continues to fascinate programmers and mathematicians alike.

The above is the detailed content of Conway's 'Game of Life”. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn