Public Group

# Who are my neighbors

This topic is 4764 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I have a 10 x 10 game board. For any two squares I want to find out if they are neighbors. A neighbor is any square that is one square up, down, left, right, or diagnoal from the square in question. I need some recommendations on how best to go about this. For example, the method that makes the determination may take the following parameters: public bool AreNeighbors(int fromrow, int fromcol, int torow, int tocol) { //determine if the square represented by (fromrow,fromcol} is a neighbor of the square represented by (torow,tocol) } Thanks.

##### Share on other sites
So what have you tried, and why hasn't it worked?

##### Share on other sites
Quote:
 Original post by SiCraneSo what have you tried, and why hasn't it worked?

I haven't tried anything yet.

##### Share on other sites
So if you put zero effort into it, why should we put in any?

##### Share on other sites
Quote:
 Original post by SiCraneSo if you put zero effort into it, why should we put in any?

Good question. Perhaps out of sheer benevolence. Sometimes you don't base your helping someone on whether or not they have actually tried anything yet. Sometimes you just help someone because you are nice and you want to help them out.

But you are right...I should have made an effort before posting my question.

Let me see what I can come up with, and I'll post my attempt here if I have any questions.

Thank you,

Tom

##### Share on other sites
I'm sure SiCrane assumed you had tried something because the problem isn't difficult at all. In fact, I'm guessing you have it solved already... but just in case, I'll give you a hint: subtraction, absolute value, 1.

##### Share on other sites
With a problem like this you will find drawing it out to be a great help. Draw out a grid and assign a number value to each cell. Then try to calculate the neighbor grid numbers.

##### Share on other sites
Here is what I have come up with, so far:

public bool AreNeighbors(Click from, Click to)
{
if((Math.Abs(from.col - to.col) <=1) && (Math.Abs(from.row - to.row) <=1))
{
return true;
}
else
{
return false;
}
}

This seems to work from my testing so far.

##### Share on other sites
Quote:
 Original post by outRiderI'm sure SiCrane assumed you had tried something because the problem isn't difficult at all. In fact, I'm guessing you have it solved already... but just in case, I'll give you a hint: subtraction, absolute value, 1.

I guess the problem seemed harder than it turned out to be.

##### Share on other sites
Quote:
 Original post by Tom KnowltonHere is what I have come up with, so far:public bool AreNeighbors(Click from, Click to){ if((Math.Abs(from.col - to.col) <=1) && (Math.Abs(from.row - to.row) <=1)) { return true; } else { return false; }}This seems to work from my testing so far.

So it looks like a grid can be it's own neighbor?

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 10
• 11
• 13
• 9
• 11
• ### Forum Statistics

• Total Topics
634090
• Total Posts
3015434
×