• Advertisement
Sign in to follow this  

Who are my neighbors

This topic is 4437 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
So what have you tried, and why hasn't it worked?

Share this post


Link to post
Share on other sites
Quote:
Original post by SiCrane
So what have you tried, and why hasn't it worked?


I haven't tried anything yet.

Share this post


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

Share this post


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



I test for this before passing in the values, but yes, according to my logic, this could happen. Nice catch.

Share this post


Link to post
Share on other sites
You can also try OOP too... [Though I'm not sure if 'Click' is an id or class type]. It would make it much easier (if you organize it properly).

Share this post


Link to post
Share on other sites

public bool AreNeighbors(int fromrow, int fromcol, int torow, int tocol)
{
int dx = tocol - fromcol;
int dy = torow - fromrow;
return (dx*dx + dy*dy <= 2);
}


Share this post


Link to post
Share on other sites
Quote:
Original post by Tom Knowlton
if (boolexp)
{
return true;
}
else
{
return false;
}

No need for an if/else, simply return the boolexp itself.

Share this post


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

  • Advertisement