Skip to content

Instantly share code, notes, and snippets.

@vyuvalv
Last active November 22, 2020 20:12
Show Gist options
  • Save vyuvalv/58e1a1fe0a5d159131dd59d6b5ca917b to your computer and use it in GitHub Desktop.
Save vyuvalv/58e1a1fe0a5d159131dd59d6b5ca917b to your computer and use it in GitHub Desktop.
Rules of Life
// **
// Any live cell with fewer than two live neighbours dies, as if by underpopulation.
// Any live cell with more than three live neighbours dies, as if by overpopulation.
// Any live cell with two or three live neighbours lives on to the next generation.
// Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.
// **
runRulesOfLife(state, neigbours) {
if (state === 1 && neigbours < 2 || neigbours > 3) {
return 0;
}
else if (state === 1 && (neigbours === 2 || neigbours === 3)) {
return 1;
}
else if (state === 0 && neigbours === 3) {
return 1;
}
else {
return 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment