I'm trying to create a method (using the A* algorithm) that solves a puzzle and returns the steps to that solution. The solution it's easy.. but I can't return the path to that.
I used a list of Nodes and then every time a push back a new Node I set the parent pointing to the Node which new came;
list<Node> opened;
list<Node> closed;
Node current;
opened.push_back(start);
while( opened.size() !=0 )
{
current = getLowestCostPath(opened);
if(IsSolution(current) == true)
return opened;
opened.remove(current);
if( !Has(closed, current))
closed.push_back(current);
for( int i = 0; i < static_cast<int>(current.GetBoard().capacity()); i++ )
{
xx = current.GetBoard()[i].X();
yy = current.GetBoard()[i].Y();
for(int j = 0; j < static_cast<int>(current.GetBoard().capacity()); j++)
{
if( isMovable(current))
{
//if found a new node
Node newNode = Node(newBoard);
Node *t = ¤t;
if(!Has(opened, newNode ) && !Has(closed, newNode ) )
{
newNode.SetParent(t);
opened.push_back(newPath);
}
}
}
}
}
(..)
the class Node it's just this
class Node{
public:
std::vector<Point> board;
Path *parent;
Node();
Node(std::vector<Point> board)
virtual std::vector<Point> GetBoard() const;
virtual Path* GetParent() const;
virtual void SetParent(Node *p);
Node::Node(): board(),parent(NULL)
{
}
std::vector<Point> Node::GetBoard() const
{
return board;
}
void Path::SetParent(Node *p)
{
this->parent = p;
}
Path* Path::GetParent() const
{
return parent;
}
But then I realized that I can't BUILD the path to solve the puzzle...
I can't even see a single Parent board...
for( list<Node>::iterator it = goal.begin(); it != goal.end(); it++)
{
if( (*it).GetParent() != NULL ){
cout << "writing a board" << endl;
mas::WriteLine( (*it).GetParent()->GetBoard() , "\n");
}
}
I've searched all over the internet and I can't realize what am I doing wrong :(
I also tried this but it makes VS2005 Crash.
//goal it's the opened list that the method Solve returned....
for(std::list<Node>::iterator it = goal.end(); it->GetParent() != NULL; t->GetParent())
{
std::cout << "step" << std::endl;
mas::WriteLine((*it).GetBoard(), "\n");
}