# Counting Adjacent -1s In A 2d Array For A Given Row/column Without Checking Positions Out Of Bounds

## 12 February 2022 - 1 answer

Need help figuring out a different way to write count(), it's something that will add to my code here:

``````public class Grid
{
private int [][] array;
private int max;

public Grid(int max)
{
array = new int;
this.max = max;
setRandom();

}

public void setRandom()
{
int i = 0;
while(i < max)
{
int r = (int)(Math.random() * 9) + 0;
int c = (int)(Math.random() * 9) + 0;
if(array[r][c] != -1)
{
array[r][c] = -1;
i++;
}
}
}

public void print()
{
for(int r = 0; r < array.length; r++)
{
for(int c = 0; c < array[r].length; c++)
{
System.out.print(array[r][c] + " ");
}
System.out.println();
}
}

public int count(int row, int col)
{
// method here
}

public static void main(String[] args)
{
Grid a = new Grid(20);
a.count(5, 5);
a.print();
}
}
``````

I want `count()` to return the number of -1s found in the 2d array surrounding the input position, and it also must not check a position "outside the bounds" (more specifically, the positions that aren't adjacent for a given row/column). Essentially it's like Minesweeper in a sense: let's say I pass in `count(5, 5)` (`5` being the row and column respectively), it will check all adjacent positions surrounding `(5, 5)`.

In this case, the highlighted blue is the position being checked and the yellow are adjacent positions. The white area is out of bounds and won't be checked. The position `(5, 5)` gets a value of 3 since there are 3 -1's surrounding the position. Visual here: https://imgur.com/a/0KCZdne

I came up with this code:

``````    public int count(int row, int col)
{
int value = 0;
for(int r = -1; r < 2; r++)
{
for(int c = -1; c < 2; c++)
{
if(c == 0 & r == 0)
continue;
if(array[row + r][col + c] == -1)
{
value++;
}
}
}
return value;
}
``````

but when I pass in `a.count(4, 7)` in `main()`, nothing happens to the grid and it stays the same. I also want to find a different way to go about this without using `continue` This is the output:

``````0 0 -1 -1 0 -1 0 0 0 0
-1 0 0 -1 -1 0 -1 0 0 0
0 0 0 0 0 0 0 0 -1 0
0 0 0 0 0 0 0 0 0 0
-1 0 -1 0 0 0 0 -1 -1 0
0 0 -1 -1 0 0 0 0 0 0
0 0 -1 0 -1 0 0 0 -1 0
0 0 -1 0 0 0 0 -1 0 0
0 0 -1 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
``````

and I want my expected output to look something like this (if I passed in `a.count(4, 7)` - `4` being the row, `7` being the column)

``````0 0 -1 -1 0 -1 0 0 0 0
-1 0 0 -1 -1 0 -1 0 0 0
0 0 0 0 0 0 0 0 -1 0
0 0 0 0 0 0 0 0 0 0
-1 0 -1 0 0 0 0 -1 -1 0
0 0 -1 -1 0 0 0 0 0 0
0 0 -1 0 -1 0 0 0 -1 0
0 0 -1 **4** 0 0 0 -1 0 0
0 0 -1 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
``````

(the 4 is the value in the position I passed in - since there are 4 -1s surrounding it, `(3, 7)` has a value of 4)

You should check both the indices of row and column that you are trying to access is valid or not Just add a check to prevent ArrayIndexOutOfBoundsException.

``````public int count(int row, int col) {
int value = 0;
for(int r = -1; r < 2; r++) {
for(int c = -1; c < 2; c++) {
if(c == 0 & r == 0)
continue;
int newR = row + r;
int newC = col + c;

if(newR < 0 || newR >= array.length || newC < 0 || newC >= array.length)
continue;

if(array[newR][newC] == -1)
value++;
}
}
return value;
}
``````

This is what I could come up with to avoid continue

``````     public int count(int row, int col) {
int value = 0;
value += isValidIndex(row - 1, col - 1)? array[row - 1][col - 1]: 0;
value += isValidIndex(row - 1, col)? array[row - 1][col]: 0;
value += isValidIndex(row - 1, col + 1)? array[row - 1][col + 1]: 0;
value += isValidIndex(row, col - 1)? array[row][col - 1]: 0;
value += isValidIndex(row, col + 1)? array[row][col + 1]: 0;
value += isValidIndex(row + 1, col - 1)? array[row + 1][col - 1]: 0;
value += isValidIndex(row + 1, col)? array[row + 1][col]: 0;
value += isValidIndex(row + 1, col + 1)? array[row + 1][col + 1]: 0;
return -value;
}

public boolean isValidIndex(int row, int col) {
return !(row < 0 || col < 0 || row >= array.length || col >= array.length);
}
``````