Who are my neighbors
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.
Quote:Original post by SiCrane
So what have you tried, and why hasn't it worked?
I haven't tried anything yet.
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
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.
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.
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.
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.
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.
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?
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement