So, I have a 4x4 2D array (it will always be these dimensions). Starting with a location on the array, some row and column, I want to find all of its valid neighbors. So far, I have a really clunky implementation.
//add row
if ( !((row + 1) > 3)) {
//do stuff
}
//sub row
if ( !((row - 1) < 0)) {
//do stuff
}
//add col
if ( !((col + 1) > 3)) {
//do stuff
}
//sub col
if ( !((col - 1) < 0)) {
//do stuff
}
... and so on
This is brutal. I feel like I do not need to check every single neighbor when I start by knowing the location of the element. Any ideas?
See Question&Answers more detail:os