Jump to content
  • Advertisement
Sign in to follow this  
flashinpan

Who are my neighbors

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by SiCrane
So 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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by outRider
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.




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

Share this post


Link to post
Share on other sites
Quote:
Original post by Tom Knowlton
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.


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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!