1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
|
#include<SDL.h>
//#include <iostream.h>
SDL_Surface *ghost[2],*wall,*screen,*tuc,*toc;
SDL_Rect rect,Brect;
typedef struct point //Structure du point
{
int x,y;
};
typedef struct noeud
{
int x,y,F,G,H;
point parent;
};
const int Col=21; //nombre de colonnes
const int Lin=19; //nombre de lignes
typedef noeud List[Lin*Col+1];
List Open;//[Lin*Col+1]; //liste des noeuds Opens
List Closed;//[Lin*Col+1]; //liste des noeuds CLosed (analysés)
//List Chemin;//[Lin*Col+1]; //liste des noeuds CLosed (analysés)
point suiv[4];
point Start,Goal; //points de départ et arrivés
//la map
int map[Lin] [Col]={
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,0,0,0,0,0,1,1,1,0,1,1,1,0,0,0,1,0,0,0,1},
{1,0,1,0,1,0,1,1,1,0,1,1,1,0,1,0,0,0,1,0,1},
{1,0,1,0,1,0,1,1,1,0,1,1,1,0,1,1,1,0,1,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1},
{1,0,1,0,1,1,1,1,1,0,1,1,1,0,1,0,1,1,1,0,1},
{1,0,1,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,1,0,1},
{1,0,1,0,1,0,1,0,1,1,1,0,1,0,1,0,1,0,1,0,1},
{1,0,0,0,1,0,0,0,1,0,1,0,1,0,0,0,1,0,0,0,1},
{1,1,1,0,1,1,1,0,0,0,1,0,1,1,1,0,1,1,1,0,1},
{1,0,0,0,1,0,0,0,1,0,1,0,1,0,0,0,1,0,0,0,1},
{1,0,1,0,1,0,1,0,1,1,1,0,1,0,1,0,1,0,1,0,1},
{1,0,1,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,1,0,1},
{1,0,1,0,1,1,1,1,1,0,1,1,1,0,1,0,1,1,1,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1},
{1,0,1,0,1,0,1,1,1,0,1,1,1,0,1,1,1,0,1,0,1},
{1,0,1,0,1,0,1,1,1,0,1,1,1,0,1,0,0,0,1,0,1},
{1,0,0,0,0,0,1,1,1,0,1,1,1,0,0,0,1,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}
};
int CostToGoal(int x,int y, point Goal)
{
int a=x-Goal.x;
int b=y-Goal.y;
int Cost=a*a+b*b;
return Cost;
};
int CostFromStart(int x,int y, point Start)
{
int a=x-Start.x;
int b=y-Start.y;
int Cost=a*a+b*b;
return Cost;
};
int Cost(int x,int y,int x1,int y1)
{
int a=x-x1;
int b=y-y1;
int Cost=a*a+b*b;
return Cost;
};
void Add(List list, noeud node)
{
for (int i=list[Lin*Col].H;i>0;i--)
list[i]=list[i-1];
list[0]=node;
list[Lin*Col].H+=1;
//cout<<"node"<<node.x<<"ajout"<<node.y<<"\n";
};
bool found(int x,int y, List list)
{
for (int i=0;i<list[Lin*Col].H;i++)
if ((list[i].x==x)&&(list[i].y==y)) return true;
return false;
};
int index(int x,int y, List list)
{
int i=0;
while ((list[i].x!=x)||(list[i].y!=y))
i++;
return i;
};
bool Valid(int x,int y)
{
if (map[y] [x]==0) return true;
return false;
};
noeud GetLowest(List Open)
{
int x=Open[0].H;
int y=0;
for (int i=0;i<Open[Lin*Col].H;i++)
if (Open[i].H<x) {x=Open[i].H;y=i;}
return (Open[y]);
};
void Remove(List list,noeud Newnode)
{
int i=0;
while ((list[i].x!=Newnode.x) || (list[i].y!=Newnode.y))
i++;
//int j=i;
for (int j=i;j<list[Lin*Col].H-i;j++)
list[j]=list[j+1];
list[Lin*Col].H-=1;
};
void Adjust(List list,noeud Newnode)
{
int i=0;
while ((list[i].x!=Newnode.x) || (list[i].y!=Newnode.y))
i++;
noeud tmp;
tmp=Newnode;//list[0];
for (int j=i;j>0;j--)
list[j]=list[j-1];
list[0]=Newnode;//tmp;
};
//=====================================================================
// A Star
//=====================================================================
int Astar()
{
//SDL_Delay(3000);
//A Star
/*Start.x=2;
Start.y=5;
Goal.x=9;
Goal.y=12;*/
//init
for (int k=0;k<Lin*Col+1;k++)
{
Open[Lin*Col].x=0;
Closed[Lin*Col].x=0;
//Chemin[Lin*Col].x=0;
Open[Lin*Col].y=0;
Closed[Lin*Col].y=0;
//Chemin[Lin*Col].y=0;
}
int NewCost=0;
int nbsuiv;
Open[Lin*Col].H=0; //La liste Open est vide
Closed[Lin*Col].H=0; //La liste CLosed est vide
//Chemin[Lin*Col].H=0;
noeud Newnode;
noeud node;
node.x=Start.x;
node.y=Start.y;
node.G=CostFromStart(node.x,node.y,Start);
node.H=CostToGoal(node.x,node.y,Goal);
node.F=node.G+node.H;
node.parent.x=-1;
node.parent.y=-1;
Add(Open,node); //ajouter le nouveau noeud dans Open
while (Open[Lin*Col].H!=0)
{
node=GetLowest(Open);
//noeud pop;
//pop.x=node.parent.x;pop.y=node.parent.y;
//if (found(pop.x,pop.y,Chemin));// Remove(Chemin,node);
/*else*/ //Add(Chemin,pop);
rect.x=node.x*32;
rect.y=node.y*32;
SDL_BlitSurface(tuc,NULL,screen,&rect);
SDL_Flip(screen);
Remove(Open,node);
if ((node.x==Goal.x) && (node.y==Goal.y))
{
SDL_Delay(3000);
int x=node.parent.x;
int y=node.parent.y;
while (x!=-1)
{
rect.x=x*32;
rect.y=y*32;
SDL_BlitSurface(toc,NULL,screen,&rect);
SDL_Flip(screen);
SDL_Delay(100);
x=Closed[index(x,y,Closed)].parent.x;
y=Closed[index(x,y,Closed)].parent.y;
}
SDL_Delay(1000);
return 1;
}
else
{ nbsuiv=0;
if (Valid(node.x+1,node.y)) {suiv[nbsuiv].x=node.x+1;suiv[nbsuiv].y=node.y;nbsuiv+=1;}
if (Valid(node.x-1,node.y)) {suiv[nbsuiv].x=node.x-1;suiv[nbsuiv].y=node.y;nbsuiv+=1;}
if (Valid(node.x,node.y+1)) {suiv[nbsuiv].x=node.x;suiv[nbsuiv].y=node.y+1;nbsuiv+=1;}
if (Valid(node.x,node.y-1)) {suiv[nbsuiv].x=node.x;suiv[nbsuiv].y=node.y-1;nbsuiv+=1;}
//cout<<"le node"<<node.x<<"-"<<node.y<<"\n";
for (int i=0;i<nbsuiv;i++)
{
//cout<<"voisin"<<suiv[i].x<<"-"<<suiv[i].y<<"\n";
Newnode.x=suiv[i].x;Newnode.y=suiv[i].y;
NewCost=CostFromStart(node.x,node.y,Start)+Cost(Newnode.x,Newnode.y,node.x,node.y);
if ((found(Newnode.x,Newnode.y,Open)||found(Newnode.x,Newnode.y,Closed))&&(Newnode.G<=NewCost))
{
}
else
{
Newnode.x=suiv[i].x;Newnode.y=suiv[i].y;
Newnode.parent.x=node.x;
Newnode.parent.y=node.y;
Newnode.G=NewCost;
Newnode.H=CostToGoal(Newnode.x,Newnode.y,Goal);
Newnode.F=Newnode.H+Newnode.G;
if (found(Newnode.x,Newnode.y,Closed)) Remove(Closed,Newnode);
if (found(Newnode.x,Newnode.y,Open)) Adjust(Open,Newnode);
else Add(Open,Newnode);
}
}
}
Add(Closed,node);
}
return 0;
}
//=====================================================================
// Main SDL_DrawMap
//=====================================================================
int main (int argc, char **argv)
{
Start.x=2;
Start.y=4;
Goal.x=15;
Goal.y=9;
for (int i=0;i<Lin*Col;i++)
{
Open[i].x=0;Open[i].y=0;Closed[i].x=0;Closed[i].y=0;
}
rect.x=120;
rect.y=130;
rect.w=32;
rect.h=32;
Brect.w=32;
Brect.h=32;
ghost[0]=SDL_LoadBMP("./sprites/ghost0.bmp");
ghost[1]=SDL_LoadBMP("./sprites/ghost1.bmp");
wall=SDL_LoadBMP("./sprites/wall.bmp");
tuc=SDL_LoadBMP("./sprites/tuc.bmp");
toc=SDL_LoadBMP("./sprites/toc.bmp");
if (SDL_Init(SDL_INIT_VIDEO)<0) return 0;
screen=SDL_SetVideoMode(800,600,16, SDL_DOUBLEBUF | SDL_HWSURFACE | SDL_FULLSCREEN);
for (int i=0;i<19;i++)
for (int j=0;j<21;j++)
{
if (map[i] [j]==1)
{
rect.x=32*j;
rect.y=32*i;
SDL_BlitSurface(wall,NULL,screen,&rect);
}
}
rect.x=32*Start.x;
rect.y=32*Start.y;
SDL_BlitSurface(ghost[0],NULL,screen,&rect);
rect.x=32*Goal.x;
rect.y=32*Goal.y;
SDL_BlitSurface(ghost[1],NULL,screen,&rect);
SDL_Flip(screen);
SDL_Delay(1000);
if (Astar()==1)
{}
} |
Partager