PhiLho wrote on Jun 2nd, 2009, 12:44pm:To be exact, these variables are references to the data: the assignments doesn't move data but just change where the references point to.
Ah yes thank you very much for reminding me of that, I had forgotten. I understand that there is swapping going on, but what I don't understand is why it's necessary at the end to swap the values stored in temp[][] into futureGrid[][].
Quote: int[][] temp = grid;
grid = futureGrid;
futureGrid = temp;
I was under the impression that the following chunk of code would assign new values to the futureGrid[][] array in the next iteration of draw(), thereby immediately replacing whatever values were assigned via futureGrid = temp in the previous iteration of draw():
for (int x = 1; x < width-1; x++) {
for (int y = 1; y < height-1; y++) {
int nb = neighbors(x, y);
if ((grid[x][y] == 1) && (nb < 2)) {
futureGrid[x][y] = 0;
set(x, y, color(0));
}
else if ((grid[x][y] == 1) && (nb > 3)) {
futureGrid[x][y] = 0;
set(x, y, color(0));
}
else if ((grid[x][y] == 0) && (nb == 3)) {
futureGrid[x][y] = 1;
set(x, y, color(255));
}
else {
futureGrid[x][y] = grid[x][y];
}
}
}
But I'm obviously wrong because if I remove the line that says futureGrid = temp then the program doesn't work properly. I just can't figure out why.