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 Turing machine.
- Any live cell with fewer than two live neighbours dies, as if by underpopulation.
- Any live cell with two or three live neighbours lives on to the next generation.
- Any live cell with more than three live neighbours dies, as if by overpopulation.
- Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.
algorithim play()
input: no input required
output: changes the current state to the next state after making changings (if present)
g <- this.state.gridFull
g2 <- arrayClone(this.state.gridFull)
for i <- 0 to this.rows - 1
for j <- 0 to this.cols - 1
count <- 0
if i > 0 <- count++ if g[i - 1][j]
if i > 0 and j > 0 <- count++ if g[i - 1][j - 1]
if i > 0 and j < this.cols - 1 <- count++ if g[i - 1][j + 1]
if j < this.cols - 1 <- count++ if g[i][j + 1]
if j > 0 <- count++ if g[i][j - 1]
if i < this.rows - 1 <- count++ if g[i + 1][j]
if i < this.rows - 1 and j > 0 <- count++ if g[i + 1][j - 1]
if i < this.rows - 1 and this.cols - 1 <- count++ if g[i + 1][j + 1]
if g[i][j] and (count < 2 or count > 3) <- g2[i][j] <- false
if not g[i][j] and count == 3 <- g2[i][j] <- true
this.setState prev_state <-
gridFull <- g2
generation <- prev_state.generation + 1
Key function is nested loops hence the time complexity is O(n^2)
Since a 2d grid is being initialized and used in every state generation, the space complexity is also O(n^2)
React [html+css+javascript]