Last active
June 3, 2024 15:48
-
-
Save beaucarnes/1c22fc5e10b15a1f2bf338bf7d09b1b9 to your computer and use it in GitHub Desktop.
Part of the code for my game of life React project.
This file contains 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
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
Greate work