Okay, I'm writing a program for class. It creates a random boolean array of a given size, then loops through the array and on each value, it tests all the values around it. If it has a certain number of 'true' around it, it executes some code. My issue is it starts at the top left of the array, and the program tests values that are outside of that array. How can I remedy this?
Here is the code:
for (int t = 0; t < N; t++) for (int j = 0; j < N; j++) { if (isLive[t][j] == true) { if (isLive[t][j+1]==true) livecount ++; if (isLive[t][j-1]==true) livecount ++; if (isLive[t+1][j+1]==true) livecount ++; if (isLive[t-1][j+1]==true) livecount ++; if (isLive[t-1][j-1]==true) livecount ++; if (isLive[t+1][j]==true) livecount ++; if (isLive[t-1][j]==true) livecount ++; if (isLive[t+1][j-1]==true) livecount ++; if (livecount == 1) isLive[t][j] = false; if (livecount > 3) isLive[t][j] = false; } else { if (isLive[t][j+1]==true) livecount ++; if (isLive[t][j-1]==true) livecount ++; if (isLive[t+1][j+1]==true) livecount ++; if (isLive[t-1][j+1]==true) livecount ++; if (isLive[t-1][j-1]==true) livecount ++; if (isLive[t+1][j]==true) livecount ++; if (isLive[t-1][j]==true) livecount ++; if (isLive[t+1][j-1]==true) livecount ++; if (livecount == 3) isLive[t][j] = true; } }