void ofApp::update(){
// Calculate the next generation based on the rules
for (int i = 0; i < gridSize; i++) {
for (int j = 0; j < gridSize; j++) {
int neighbors = countNeighbors(i, j);
if (grid[i][j]) {
nextGrid[i][j] = (neighbors == 2 || neighbors == 3);
// the same meaning ⬇️
// if (neighbors == 2 || neighbors == 3) {
// nextGrid[i][j] = true;
// } else {
// nextGrid[i][j] = false;
// }
} else {
nextGrid[i][j] = neighbors == 3;
// the same meaning ⬇️
// if (neighbors == 3) {
// nextGrid[i][j] = true;
// } else {
// nextGrid[i][j] = false;
// }
}
}
}
// Copy the next generation to the current grid
for (int i = 0; i < gridSize; i++) {
for (int j = 0; j < gridSize; j++) {
//update the changes to show them!
grid[i][j] = nextGrid[i][j];
}
}
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter