This is the challenge of the Game of Life by Elias Waly ba in a tweet on X.
The Game of Life, also known simply as Life, is a cellular automaton devised by the British mathematician John Horton Conway in 1970. It is a zero-player game, meaning that its evolution is determined by its initial state, requiring no further input. One interacts with the Game of Life by creating an initial configuration and observing how it evolves. It is Turing complete and can simulate a universal constructor or any other.
Source of this definition : Wikipédia
The game follows simple rules:
-
- Underpopulation: A live cell with fewer than two live neighbors dies.
-
- Survival: A live cell with two or three live neighbors survives.
-
- Overpopulation: A live cell with more than three live neighbors dies.
-
- Reproduction: A dead cell with exactly three live neighbors becomes alive.
- The script initializes variables for grid elements, buttons, and input fields.
- There's a function to initialize a matrix to represent the cell states.
- The
updateCellClasses
function updates the visual representation of cells based on their state in the matrix.
- The
createCellsElements
function dynamically creates cell elements in the grid and populates theallCells
array.
- The
newRound
function initializes a new round of the game. - It sets up the matrix, creates cell elements, updates the grid size, sets random alive cells, and updates the cell classes.
- The
UpdateCell
function implements the game logic, checking the rules and updating the cell matrix accordingly. - It is called in intervals to create the animation effect.
- Click events are set for each cell to toggle their state.
- Buttons have click events to play, stop, reset, clear, and set random cells.
- There's a function
updateZoom
to handle zoom changes in function of the inputZoom.
- Adjust rows, columns, speed, rate of random cells, and zoom using the input controls.
- Click on cells to toggle their state.
- Use play, stop, reset, clear, and random buttons to control the simulation.
Here a quick simulation :