Who are my neighbors

Started by
12 comments, last by Fred304 18 years, 3 months ago
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.
Advertisement
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).
Projects:> Thacmus - CMS (PHP 5, MySQL)Paused:> dgi> MegaMan X Crossfire
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);}

Quote:Original post by Tom Knowlton
if (boolexp)
{
return true;
}
else
{
return false;
}

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

This topic is closed to new replies.

Advertisement