I am quite stuck on trying to figure out how to write my code for counting neighbors in this Game of Life program I am attempting to write.
Requirements are as follow: Given two arguments that indicate the row and column of a particular cell in the matrix, this function returns the number of neighbors that have the value "true". Most positions in the grid have 8 neighbors like the center square in a tic-tac-toe game. The four corner positions have only 3 neighbors each. The remaining positions around the edge of the grid have 5 neighbors each.
Additional Requirement: In this function you must use your "get()" function to access the matrix, instead of accessing your 2D array data member directly. So, if your data member is named "m", you'll say "get(row, col)" instead of "m[row][col]". This will be a safer programming practice, since the get() function will do range checking for you (i.e., it will make sure that row and col are not out-of-bounds of the 2D array).
It seems pretty straightforward.
What exactly is the problem?
1 2 3 4 5 6
int count = 0;
if (row > 0) count += get(row - 1, col );
if (row < NUM_ROWS - 1) count += get(row + 1, col );
if (col > 0) count += get(row , col - 1);
if (col < NUM_COLS - 1) count += get(row , col + 1);
// etc.
Often when programming something like the game of life we put a border around the board so that we don't need to check the bounds. The border would always be false so it would never add to the count.
I could not think of how to begin the code, I had a general idea of how to setup an if else somewhat. When it comes to visualizing boundaries and a grid I think I struggle with that the most oppose to regular outputting and storing information like my past projects. Thank you for your help.