Skip to content

Instantly share code, notes, and snippets.

@beaucarnes
Last active September 28, 2017 06:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save beaucarnes/1c22fc5e10b15a1f2bf338bf7d09b1b9 to your computer and use it in GitHub Desktop.
Save beaucarnes/1c22fc5e10b15a1f2bf338bf7d09b1b9 to your computer and use it in GitHub Desktop.
Part of the code for my game of life React project.
play = () => {
let g = this.state.gridFull;
let g2 = arrayClone(this.state.gridFull);
for (let i = 0; i < this.rows; i++) {
for (let j = 0; j < this.cols; j++) {
let count = 0;
if (i > 0) if (g[i - 1][j]) count++;
if (i > 0 && j > 0) if (g[i - 1][j - 1]) count++;
if (i > 0 && j < this.cols - 1) if (g[i - 1][j + 1]) count++;
if (j < this.cols - 1) if (g[i][j + 1]) count++;
if (j > 0) if (g[i][j - 1]) count++;
if (i < this.rows - 1) if (g[i + 1][j]) count++;
if (i < this.rows - 1 && j > 0) if (g[i + 1][j - 1]) count++;
if (i < this.rows - 1 && this.cols - 1) if (g[i + 1][j + 1]) count++;
if (g[i][j] && (count < 2 || count > 3)) g2[i][j] = false;
if (!g[i][j] && count === 3) g2[i][j] = true;
}
}
this.setState({
gridFull: g2,
generation: this.state.generation + 1
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment