# Demo ready

Right, the path finding is implemented, so time for the first demo.

First up a screenshot to make the post look pretty.

Demo is HERE. Just click on a square for the character to find the fastest route there, uncovering the fog as he moves.

Press Alt+F4 to exit.

Give it a test, although should be fine.

Here's the source for the path finding.

(BTW, CVector<> is just a little wrapper for std::vector<> that throws one of my game exceptions on a subscript out of range. Don't want anyone to think I'm writing my own vector class [smile]).

Any comments on the demo appreciated.

First up a screenshot to make the post look pretty.

Demo is HERE. Just click on a square for the character to find the fastest route there, uncovering the fog as he moves.

Press Alt+F4 to exit.

Give it a test, although should be fine.

Here's the source for the path finding.

(BTW, CVector<> is just a little wrapper for std::vector<> that throws one of my game exceptions on a subscript out of range. Don't want anyone to think I'm writing my own vector class [smile]).

bool CPath::Target(int Ox,int Oy,int Tx,int Ty)

{

CVectorVc,Vn;

Steps.clear();

Pos=0;

if(Ox==Tx && Oy==Ty) return true;

if(!Open(Tx,Ty)) return false;

Grid.Fill(INT_MAX);

Grid(Tx,Ty)=0;

Vc.push_back(CPoint(Tx,Ty));

int Value=0;

bool Found=false;

while(Vc.size())

{

for(std::vector::iterator I=Vc.begin();I!=Vc.end();++I)

{

int X=I->X;

int Y=I->Y;

if(Grid(X,Y)==Value)

{

if(Y>0 && Open(X,Y-1) && Grid(X,Y-1)==INT_MAX){ Grid(X,Y-1)=Value+1; Vn.push_back(CPoint(X,Y-1)); }

if(X1 && Open(X+1,Y) && Grid(X+1,Y)==INT_MAX){ Grid(X+1,Y)=Value+1; Vn.push_back(CPoint(X+1,Y)); }

if(Y1 && Open(X,Y+1) && Grid(X,Y+1)==INT_MAX){ Grid(X,Y+1)=Value+1; Vn.push_back(CPoint(X,Y+1)); }

if(X>0 && Open(X-1,Y) && Grid(X-1,Y)==INT_MAX){ Grid(X-1,Y)=Value+1; Vn.push_back(CPoint(X-1,Y)); }

}

}

Vc.swap(Vn);

Vn.clear();

++Value;

if(Grid(Ox,Oy)!=INT_MAX){ Vc.clear(); Found=true; }

}

if(!Found) return false;

int X=Ox,Y=Oy;

bool Finished=false;

while(!Finished)

{

int Min=INT_MAX;

if(Grid(X-1,Y-1)<=Min && Open(X-1,Y) && Open(X,Y-1)) Min=Grid(X-1,Y-1);

if(Grid(X+1,Y-1)<=Min && Open(X+1,Y) && Open(X,Y-1)) Min=Grid(X+1,Y-1);

if(Grid(X-1,Y+1)<=Min && Open(X-1,Y) && Open(X,Y+1)) Min=Grid(X-1,Y+1);

if(Grid(X+1,Y+1)<=Min && Open(X+1,Y) && Open(X,Y+1)) Min=Grid(X+1,Y+1);

if(Grid(X,Y-1)<=Min) Min=Grid(X,Y-1);

if(Grid(X+1,Y)<=Min) Min=Grid(X+1,Y);

if(Grid(X,Y+1)<=Min) Min=Grid(X,Y+1);

if(Grid(X-1,Y)<=Min) Min=Grid(X-1,Y);

CVectorChoices;

if(Grid(X-1,Y-1)==Min) Choices.push_back(CPoint(-1,-1));

if(Grid(X+1,Y-1)==Min) Choices.push_back(CPoint(1,-1));

if(Grid(X-1,Y+1)==Min) Choices.push_back(CPoint(-1,1));

if(Grid(X+1,Y+1)==Min) Choices.push_back(CPoint(1,1));

if(Grid(X,Y-1)==Min) Choices.push_back(CPoint(0,-1));

if(Grid(X+1,Y)==Min) Choices.push_back(CPoint(1,0));

if(Grid(X,Y+1)==Min) Choices.push_back(CPoint(0,1));

if(Grid(X-1,Y)==Min) Choices.push_back(CPoint(-1,0));

CPoint Pt=Choices[std::rand()%Choices.size()];

Steps.push_back(Pt);

X+=Pt.X;

Y+=Pt.Y;

if(X==Tx && Y==Ty) Finished=true;

}

return true;

}

Any comments on the demo appreciated.

Sign in to follow this

Followers
0

## 5 Comments

## Recommended Comments

## Create an account or sign in to comment

You need to be a member in order to leave a comment

## Create an account

Sign up for a new account in our community. It's easy!

Register a new account## Sign in

Already have an account? Sign in here.

Sign In Now