Last active
November 22, 2020 20:12
-
-
Save vyuvalv/58e1a1fe0a5d159131dd59d6b5ca917b to your computer and use it in GitHub Desktop.
Rules of Life
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ** | |
// 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