witam,
mam problem z troche rozległym kodem, ale zdaje mi się, że wiem gdzie leży błąd tylko nie wiem jak go rozwiązać :
mam funkcję która wykonuje algorytm a* - w miejscu zaznaczonym komentarzem wystepuje właśnie Block Type Is Valid :
std::list<MapSearchNode> findPath(MapSearchNode a, MapSearchNode goalNode)
{
MapSearchNode final;
bool goalFound = false;
std::list<MapSearchNode> closedList;
std::list<MapSearchNode> openList;
std::list<MapSearchNode *> listToDelete;
std::list<MapSearchNode>::iterator openListIterator;
std::list<MapSearchNode>::iterator openListIteratorToErase;
a.g = 0;
a.h = a.GoalDistanceEstimate(goalNode);
openList.push_back(a);
MapSearchNode B;
while(!openList.empty())
{
//find node with minimum f value on the openList
/*MapSearchNode*/ B = *openList.begin(); // B as best node
if( (int) openList.size() == 1 )
openListIteratorToErase = openList.begin();
else
{
for(openListIterator = openList.begin() ; ; )
{
if(openListIterator->g + openListIterator->h < B.g + B.h)
{
B = *openListIterator;
openListIteratorToErase = openListIterator;
}
if(openListIterator == --openList.end() )
break;
if(openListIterator->x != (--openList.end())->x && openListIterator->y != (--openList.end())->y )
openListIterator++;
}
}
//remove B from openList and add it to closedList
if(B.IsContainedIn(openList))
openList.erase(openListIteratorToErase);
closedList.push_back(B);
float tentGscore = 99999;
for(int i = 0; i < 8 ; ++i)// CHECK EACH NEIGHBOUR OF B - NODE WITH LOWEST F VALUE FROM OPENLIST-------------
{
MapSearchNode *thisNeighbour = new MapSearchNode;
*thisNeighbour = GiveNeighbour(B, i);
listToDelete.push_back(thisNeighbour);
/* ----->>> !! */ if( (thisNeighbour->IsContainedIn(closedList)) || (GetMap(*thisNeighbour) == 9 /*not traversible*/))
{
//delete thisNeighbour;
continue;
}
tentGscore = B.g + ( i%2 ? 1.0 : 1.4 );
if( !(thisNeighbour->IsContainedIn(openList)) )
{
thisNeighbour->g = tentGscore;
thisNeighbour->h = thisNeighbour->GoalDistanceEstimate(goalNode);
thisNeighbour->parent = &B;
openList.push_back( *thisNeighbour ); // add it to openList
}
else
{
for(std::list<MapSearchNode>::iterator iter = openList.begin(); iter != openList.end(); iter++)
{
if(iter->x == thisNeighbour->x && iter->y == thisNeighbour->y )
{
openList.erase(iter);
break;
}
}
thisNeighbour->g = tentGscore;
thisNeighbour->h = thisNeighbour->GoalDistanceEstimate(goalNode);
thisNeighbour->parent = &B;
openList.push_back( *thisNeighbour ); // add it to openList
}
if(thisNeighbour->IsGoal(goalNode))
{
goalFound = true;
thisNeighbour->parent = &B;
final = *thisNeighbour;
break;
}
}
if(goalFound)
break;
}
// construct the route from back to front
std::list<MapSearchNode> toReturn;
MapSearchNode temp;
while( final.parent != NULL)
{
toReturn.push_front(final);
temp = final;
final = *final.parent;
delete &temp;
}
for(std::list<MapSearchNode *>::iterator delIter = listToDelete.begin() ; delIter != listToDelete.end(); delIter++)
{
delete &delIter;
}
return toReturn;
}
funkcja is contained in wygląda natomiast tak:
bool MapSearchNode::IsContainedIn( std::list<MapSearchNode> listToLookIn)
{
if(listToLookIn.empty())
return false;
else if(( listToLookIn.size() == 1 ) && (listToLookIn.begin()->x == this->x) && (listToLookIn.begin()->y == this->y))
return true;
for(std::list<MapSearchNode>::iterator myIterator = listToLookIn.begin(); myIterator != listToLookIn.end(); myIterator++)
{
if( (this->x == myIterator->x) && (this->y == myIterator->y) )
return true;
}
return false;
}
przy debugowaniu okzauje się że program przechodzi przez funkcję isContainedIn dochodzi do return false i potem wystepuje błąd.
jakieś wskazówki ?