Hello I am practicing working with 2d arrays in c++ and my question is for example if I want to check if 4 has either 0 or 11 to the north , east, south, west as neighbour it should return false.
this is my function to test
Code:
bool testG(int grid[ROW][COL], int row, int col) {
if (row < 0 || col < 0 || row >= ROW || col >= COL)
return false;
return grid[row][col] == 0 || grid[row][col] == 11;
}
//then this is where I check if north, west, south , east are not 0 or 11
if(testG(grid,-1,0) && testG(grid,0,-1) && testG(grid,1,0) && testG(grid,0,1))
{
return false;
}
if I remove (testG(grid,-1,0) && testG(grid,0,-1) then it works and that is because west and north of the node 4 is out of bounds.
Now my problem is since west of 4 and north of four is out of bounds it will never return false.
How could I optimise my if condition to make it return false?
This is my 2d array
This should return false
Code:
int grid[ROW][COL] = {{ 4, 11, 1, 1 },
{ 0, 0, 1, 0 },
{ 0, 1, 5, 0},
{ 0, 5, 0,0 } };
This should return true
Code:
int grid[ROW][COL] = {{ 4, 11, 1, 1 },
{ 1, 1, 1, 0 },
{ 0, 1, 5, 0},
{ 0, 5, 0,0 } };
I hope my question was clear , thank you in advance.