dimanche 1 janvier 2017

Adding up values around a specific point in an array in Java

So I have a 2D array which is filled with 1's and 0's. I want to check the neighbours of a specific index in the array, and add their values up.

The first and last rows and columns (aka the 'bordering' values) are special cases as they are not completely surrounded with neighbouring values which means I have to put lots of conditionals to take them into account.

If I only do the first if statement, I get the problem of arrayIndexOutOfBounds. Which makes sense to me as its trying to go to position integerGeneration[-1][-1], for example.

What I have done below works, but its really ugly and I feel there is a "neater" approach to this.

Is there a better way than doing all the special cases on the outer borders of the array in their own else if statements?

if ((x > 0 & x < rows-1) & (y > 0 & y < columns-1) ) {      // checks the inside box
    for (int i = x - 1; i < x + 2; i++) {
        for (int j = y - 1; j < y + 2; j++) {
            filled = integerGeneration[i][j] + filled;
        }
    }
    filled = filled - integerGeneration[x][y];
    return filled;
}
else if (x == 0 & y < columns-1 & y > 0) {                  // checks the top edge
    for (int i = x; i < x + 2; i++) {
        for (int j = (y - 1); j < y + 2; j++) {
            filled = integerGeneration[i][j] + filled;
        }
    }
    filled = filled - integerGeneration[x][y];
    return filled;
}
else if (y == 0 & x < rows-1 & x > 0) {                     // checks the left edge
    for (int i = x - 1; i < x + 2; i++) {
        for (int j = y; j < y + 2; j++) {
            filled = integerGeneration[i][j] + filled;
        }
    }
    filled = filled - integerGeneration[x][y];
    return filled;
}
else if (x == 0 & y == 0) {                                 // checks the top left corner
    for (int i = x; i < x + 2; i++) {
        for (int j = y; j < y + 2; j++) {
            filled = integerGeneration[i][j] + filled;
        }
    }
    filled = filled - integerGeneration[x][y];
    return filled;
}
else if (x == rows-1 & y < columns-1 & y > 0) {             // checks the bottom edge
    for (int i = x - 1; i < x + 1; i++) {
        for (int j = y - 1; j < y + 2; j++) {
            filled = integerGeneration[i][j] + filled;
        }
    }
    filled = filled - integerGeneration[x][y];
    return filled;
}
else if (y == columns-1 & x < rows-1 & x > 0) {             // checks the right edge
    for (int i = x - 1; i < x + 2; i++) {
        for (int j = y - 1; j < y + 1; j++) {
            filled = integerGeneration[i][j] + filled;
        }
    }
    filled = filled - integerGeneration[x][y];
    return filled;
}
else if (y == columns-1 & x == rows-1) {                    // checks the bottom right corner
    for (int i = x - 1; i < x + 1; i++) {
        for (int j = y - 1; j < y + 1; j++) {
            filled = integerGeneration[i][j] + filled;
        }
    }
    filled = filled - integerGeneration[x][y];
    return filled;
}
else if (x == 0 & y == columns-1) {                         // checks the top right corner
    for (int i = x; i < x + 2; i++) {
        for (int j = y - 1; j < y + 1; j++) {
            filled = integerGeneration[i][j] + filled;
        }
    }
    filled = filled - integerGeneration[x][y];
    return filled;
}
else if (x == rows-1 & y == 0) {                            // checks the bottom left corner
    for (int i = x - 1; i < x + 1; i++) {
        for (int j = y; j < y + 2; j++) {
            filled = integerGeneration[i][j] + filled;
        }
    }
    filled = filled - integerGeneration[x][y];
    return filled;
}
else {
    System.out.println("Error, point out of bounds");
    return -1;
}

}

Aucun commentaire:

Enregistrer un commentaire