I have posted a couple of threads relating to this program, Astar pathifinding, it is now complete and i just need to sort out this final function.
It works fine at the moment, this function plots the shortest path found, backwards from the target square to the start square, and at present draws it too with circles, what i need is to somehow save this path back and then draw it from the start square to the target square.
in the earlier functions on each loop the current best path square is stored in ParentSQ[] the value stored is the number of the grid on the board found by 'down * maxacross + across'
then the adjacent 8squares are filled with this value in ParentGrid[cgY][cgX]so they 'point' to the centre square.
so reversing back from target i take the last value in ParentSQ[] (PlotXY) and turn it into cgY and cgX co-ordinates with
cgY = PlotXY / MXDWN , cgX = PlotXY - MXACR * cgY;
then i see what is stored in ParentGrid[cgY][cgX] then i take this value (PlotXY) and turn it into cgY, cgX etc etc till i get to start square.
Code:
ShowPath(int PathSearch,int ParentGrid[MXDWN][MXACR],int ParentSQ[MXDWN * MXACR],int cgY,int cgX)
{
int count = 0;
int PlotXY = 0;
char nextgo;
PlotXY = ParentSQ[PathSearch];
cgY = PlotXY / MXDWN;
cgX = PlotXY - MXACR * cgY;
setfillstyle(1,15);
for(count = PathSearch; count > -1; count--)
{
if(ParentGrid[cgY][cgX] == 0) count = 0;
circle((cgX * MXACR)+ 10,(cgY * MXDWN)+ 10,8);
PlotXY = ParentGrid[cgY][cgX];
cgY = PlotXY / MXDWN;
cgX = PlotXY - MXACR * cgY;
nextgo = getch();
}
return(0);
}