dimanche 31 mai 2015

List.Contains always returning false

I am trying to implement a really basic A* implementation.

I have a 'completed' list that contains coordinates of all previously assessed nodes.

For the sake of argument, lets say I am trying to get from (1,0) to (3,0).

After the third iteration, my 'completed' list contains (1,0) and (2,0). It is currently assessing all of the neighbours around 2,0. This includes the already assessed (1,0).

When calling completed.Contains(neighbour), where neighbour = (1,0) it should return true. However it somehow does not meet the condition. Thus creating a duplicate node and assesses in an infinite loop.

The below is an example in code of what is happening. Point = Simple object containing an X and Y.

point1 = new Point(1,0);
point2 = new Point(2,0);
neighbour = point1;

var completed = new List<Point>();
completed.Add(point1);
completed.Add(point2);

if(completed.Contains(neighbour))
{
     // Do something. (In my code, this should break a loop, so...)
     continue;
}
// However, this is happening instead...
if(!completed.Contains(neighbour))
{
    // Adds to the list of the next node to be worked on. Thus creating a loop.
}

There are more conditions on these if's in my actual code, but for arguments sake and for my sanity I have made them basic as above, to no avail. I'm not sure why it cannot see the existing value. Is it because I am not looking at the values themselves, but just the index? (Therefore 1,0 never exists)?

Aucun commentaire:

Enregistrer un commentaire