I am trying to write a program that implements Conway\'s game of life on a 20x60 cell board. The grid will wrap around so the left side will be connected to (neig
First, I would change the grid to be 0-based instead of 1-based.
Then you can write a simple loop:
int count = 0;
for (i = row - 1; i <= row + 1; i++) {
for (j = col - 1; j <= col + 1; j++) {
if(i != row && j != col) {
count += grid[(i + maxrow)%maxrow][(j + maxcol)%maxcol];
}
}
}
The + maxrow
is to make sure the index is positive.