Code:
void CAstar::solvePuzzle(CPuzzle* goal, CPuzzle* curr)
{
vector<CPuzzle*> open;
vector<CPuzzle*> closed;
curr->g = 0;
curr->h = manhattan(goal, curr);
curr->f = curr->g + curr->h;
open.push_back(curr);
int minFindex = 0;
while (!open.empty())
{
for (int i = 0; i < open.size(); i++)
{
if (open[i]->f < open[minFindex]->f)
{
minFindex = i;
}
}
CPuzzle *tmp = open[minFindex];
CPuzzle tmp1(tmp->values, tmp->zeroIndex, tmp->upIndex, tmp->downIndex, tmp->leftIndex, tmp->rightIndex, tmp->g, tmp->h, tmp->f);
tmp = &tmp1;
tmp->updateAll();
int k = 1;
system("cls");
tmp->printTable();
cout << endl << endl;
if (goal->h == tmp->h)
{
cout << "\n\n\t\t *:*: Puzzle solved! :*:*\n\n";
return;
}
closed.push_back(tmp);
open.erase(open.begin() + minFindex);
if (tmp->updateUp() == true)
{
CPuzzle *aux = tmp;
CPuzzle tmp1(aux->values, aux->zeroIndex, aux->upIndex, aux->downIndex, aux->leftIndex, aux->rightIndex, aux->g, aux->h, aux->f);
aux = &tmp1;
aux->moveUp();
checkNeighbourState(goal, tmp, aux, &open, &closed);
}
if (tmp->updateDown() == true)
{
CPuzzle *aux = tmp;
CPuzzle tmp1(aux->values, aux->zeroIndex, aux->upIndex, aux->downIndex, aux->leftIndex, aux->rightIndex, aux->g, aux->h, aux->f);
aux = &tmp1;
aux->moveDown();
checkNeighbourState(goal, tmp, aux, &open, &closed);
}
if (tmp->updateLeft() == true)
{
CPuzzle *aux = tmp;
CPuzzle tmp1(aux->values, aux->zeroIndex, aux->upIndex, aux->downIndex, aux->leftIndex, aux->rightIndex, aux->g, aux->h, aux->f);
aux = &tmp1;
aux->moveLeft();
checkNeighbourState(goal, tmp, aux, &open, &closed);
}
if (tmp->updateRight() == true)
{
CPuzzle *aux = tmp;
CPuzzle tmp1(aux->values, aux->zeroIndex, aux->upIndex, aux->downIndex, aux->leftIndex, aux->rightIndex, aux->g, aux->h, aux->f);
aux = &tmp1;
aux->moveRight();
checkNeighbourState(goal, tmp, aux, &open, &closed);
}
}
}