Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static int neighbors2 (board b, int i, int j)
- {
- int n = 0;
- int i_left = max(0,i-1);
- int i_right = min(HEIGHT, i+2);
- int j_left = max(0,j-1);
- int j_right = min(WIDTH, j+2);
- int ii, jj;
- for (jj = j_left; jj < j_right; ++jj) {
- for (ii = i_left; ii < i_right; ii++) {
- n += b[ii][jj];
- }
- }
- return n - b[i][j];
- }
- //Iterates through the first row of the 3x3 area
- static int first_row(board b, int i, int j) {
- int f = 0;
- int i_left = max(0,i-1);
- int j_left = max(0,j-1);
- int j_right = min(WIDTH, j+2);
- int jj;
- for (jj = j_left; jj < j_right; ++jj) {
- f += b[i_left][jj];
- }
- return f;
- }
- //Iterates and adds up the second row of the 3x3 area
- static int second_row(board b, int i, int j) {
- int g = 0;
- int i_right = min(HEIGHT, i+2);
- int j_left = max(0,j-1);
- int j_right = min(WIDTH, j+2);
- int jj;
- if (i_right != i) {
- for (jj = j_left; jj < j_right; ++jj) {
- g += b[i][jj];
- }
- }
- return g;
- }
- //iterates and adds up the third row of the 3x3 area.
- static int third_row(board b, int i, int j) {
- int h = 0;
- int i_right = min(HEIGHT, i+2);
- int j_left = max(0,j-1);
- int j_right = min(WIDTH, j+2);
- int jj;
- for (jj = j_left; jj < j_right; ++jj) {
- h += b[i_right][jj];
- }
- return h;
- }
- //adds up the surrounding spots
- //subtracts the spot we're looking at.
- static int addUp(board b, int i, int j) {
- int n = first_row(b, i, j) + second_row(b, i, j) + third_row(b, i, j);
- return n - b[i][j];
- }
Add Comment
Please, Sign In to add comment