finding neighbors of a cell in 2d-array
Hello everyone, I'm having trouble with finding the neighbors of an 2d array.
I know where the code fails, but I can't figure out how to fix it.
Here's the code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
|
void Manager::checkNeighbours(int x,int y)const
{
pair<int, int> p = make_pair(x, y);
for (int i = x - 1; i <= x + 1 ; i++)
{
for (int j = y - 1; j <= y + 1 ; j++)
{
if (i < 0)
i++;
if (i >= size)
i--;
if (j < 0)
j++;
if (j >= size)
j--;
if (myBoard->getSym(i, j) != 'W')
{
pair<int, int> q = make_pair(i, j);
if (q != p)
{
Brick b(x, y, myBoard->getSym(i, j));
myStack->push(b);
}
}
}
}
cout << "The neighbours of " << myBoard->getSym(x, y) << " are\n";
Brick a;
while (!myStack->empty())
{
cout << myStack->top().getSym() << "\n";
myStack->pop();
}
}
|
for instance if I have the following matrix:
SW
RG
The output I get is:
The neighbors of S are:
R
G
and then I enter an infinite loop.
if (i < 0)
i++;
if (i >= size)
i--;
if (j < 0)
j++;
if (j >= size)
j--;
replace all four increments and decrements with the continue statement.
Thank you!
Topic archived. No new replies allowed.