游戏设计迷进——欢迎调试高手一起调试A*算法程序——100分

wanggongming 2005-04-28 11:35:24
在游戏设计中,人物行走设计是不可缺少的一环,如何使主角由一点走到另外一点,而且路程最短,而且要绕过中间的障碍物和游戏中的其他人物,例如:在一般的RPG游戏中,用鼠标在屏幕上轻轻一点,主角就可以从当前位置按照最佳路径走到目的地,使不少游戏都不可不考虑。一般用A*算法来设计,我近期写课程论文,以此为切入口,算法都已经完成,但是具体程序调试老是出错,我用VC6编程,源码如下:
//#define NDEBUG
#include <stdio.h>
#include <conio.h>
#include <assert.h>
#include <stdlib.h>
//#include <mem.h>

#define tile_num(x,y) (y*map_w+x) //将 x,y 坐标转换为地图上块的编号
#define tile_x(n) (n%map_w) //由块编号得出 x,y 坐标
#define tile_y(n) (n/map_w)

#define MAPMAXSIZE 180 //地图面积最大为 180x180,如果没有64K内存限制可以更大
#define MAXINT 32767

//树结构, 比较特殊, 是从叶节点向根节点反向链接,方便从叶节点找到根节点
typedef struct tree_node *TREE;

struct tree_node {
int h; //节点所在的高度,表示从起始点到该节点所有的步数
int tile; //该节点的位置
TREE father; //该节点的上一步
};

//链接结构,用于保存处理过的和没有处理过的结点
typedef struct link_node *LINK;

struct link_node {
TREE node;
int f;
LINK next;
};

LINK sort_queue; // 保存没有处理的行走方法的节点
LINK store_queue; // 保存已经处理过的节点 (搜索完后释放)

char * map; //地图数据
//unsigned int * dis_map; //保存搜索路径时,中间目标地最优解
int * dis_map; //保存搜索路径时,中间目标地最优解


int map_w,map_h; //地图宽和高
int start_x,start_y,end_x,end_y; //地点,终点坐标

// 初始化队列
void init_queue()
{
sort_queue=(LINK)malloc(sizeof(*sort_queue));
sort_queue->node=NULL;
sort_queue->f=-1;
sort_queue->next=(LINK)malloc(sizeof(*sort_queue));
sort_queue->next->node=NULL;
sort_queue->next->f=MAXINT;
sort_queue->next->next=NULL;

store_queue=(LINK)malloc(sizeof(*store_queue));
store_queue->node=NULL;
store_queue->f=-1;
store_queue->next=NULL;
}

// 待处理节点入队列, 依靠对目的地估价距离插入排序
void enter_queue(TREE node,int f)
{
LINK p=sort_queue,father,q;
while(f>p->f) { //sort队列按照从小到大排序,寻找f的最佳插入位置
father=p;
p=p->next;
assert(p);
}
q=(LINK)malloc(sizeof(*q)); //找到后生成sort队列节点
assert(sort_queue);
q->f=f,q->node=node,q->next=p; //进队列并修改指针
father->next=q;
}

// 将离目的地估计最近的方案出队列
TREE get_from_queue()
{
LINK bestchoice=sort_queue->next;//因为sort数组已经排序,所以第一个节点就是最佳节点
LINK next=sort_queue->next->next;
sort_queue->next=next; //删除第一个节点

bestchoice->next=store_queue->next; //把最佳节点插入store数组。
store_queue->next=bestchoice;
return bestchoice->node; //返回最佳节点
}

// 释放栈顶节点
void pop_stack() //释放store数组的第一个节点
{
LINK s=store_queue->next;
assert(s);
store_queue->next=store_queue->next->next;
free(s->node); //删除树上的节点
free(s); //删除链表节点
}

// 释放申请过的所有节点
void freetree()
{
//int i;
LINK p;
while(store_queue){ //删除store数组
p=store_queue;
free(p->node);//删除树上的节点
store_queue=store_queue->next;//修改store数组
free(p);//删除链表节点
}
while (sort_queue) { //删除sort数组
p=sort_queue;
free(p->node);//删除树上的节点
sort_queue=sort_queue->next;//修改store数组
free(p);//删除链表节点
}
}
...全文
231 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
guofu_x 2005-05-10
  • 打赏
  • 举报
回复
唉,很好啊,可惜现在还有点看不懂,呵呵
flying_dancing 2005-05-10
  • 打赏
  • 举报
回复
//我这里也有一个....但程序运行另需要一个描述地图的数据文件 ///编译有点问题,但有一些是编译器的问题
// #define NDEBUG
#include <stdio.h>
#include <conio.h>
#include <assert.h>
#include <stdlib.h>
#define MAPMAXSIZE 100 //地图面积最大为 100x100
#define MAXINT 8192 //定义一个最大整数, 地图上任意两点距离不会超过它
#define STACKSIZE 65536 //保存搜索节点的堆栈大小

#define tile_num(x,y) ((y)*map_w+(x)) //将 x,y 坐标转换为地图上块的编号
#define tile_x(n) ((n)%map_w) //由块编号得出 x,y 坐标
#define tile_y(n) ((n)/map_w)

// 树结构, 比较特殊, 是从叶节点向根节点反向链接
typedef struct node *TREE;

struct node {
int h;
int tile;
TREE father;
} ;

typedef struct node2 *LINK;

struct node2 {
TREE node;
int f;
LINK next;
};

LINK queue; // 保存没有处理的行走方法的节点
TREE stack[STACKSIZE]; // 保存已经处理过的节点 (搜索完后释放)
int stacktop;
unsigned char map[MAPMAXSIZE][MAPMAXSIZE]; //地图数据
int dis_map[MAPMAXSIZE][MAPMAXSIZE]; //保存搜索路径时,中间目标地最优解

int map_w,map_h; //地图宽和高
int start_x,start_y,end_x,end_y; //地点,终点坐标

// 初始化队列
void init_queue()
{
queue=(LINK)malloc(sizeof(*queue));
queue->node=NULL;
queue->f=-1;
queue->next=(LINK)malloc(sizeof(*queue));
queue->next->f=MAXINT;
queue->next->node=NULL;
queue->next->next=NULL;
}

// 待处理节点入队列, 依靠对目的地估价距离插入排序
void enter_queue(TREE node,int f)
{
LINK p=queue,father,q;
while(f>p->f) {
father=p;
p=p->next;
assert(p);
}
q=(LINK)malloc(sizeof(*q));
assert(queue);
q->f=f,q->node=node,q->next=p;
father->next=q;
}

// 将离目的地估计最近的方案出队列
TREE get_from_queue()
{
TREE bestchoice=queue->next->node;
LINK next=queue->next->next;
free(queue->next);
queue->next=next;
stack[stacktop++]=bestchoice;
assert(stacktop<STACKSIZE);
return bestchoice;
}

// 释放栈顶节点
void pop_stack()
{
free(stack[--stacktop]);
}

// 释放申请过的所有节点
void freetree()
{
int i;
LINK p;
for (i=0;i<stacktop;i++)
free(stack[i]);
while (queue) {
p=queue;
free(p->node);
queue=queue->next;
free(p);
}
}

// 估价函数,估价 x,y 到目的地的距离,估计值必须保证比实际值小
int judge(int x,int y)
{
int distance;
distance=abs(end_x-x)+abs(end_y-y);
return distance;
}

// 尝试下一步移动到 x,y 可行否
int trytile(int x,int y,TREE father)
{
TREE p=father;
int h;
if (map[y][x]!=' ') return 1; // 如果 (x,y) 处是障碍,失败
while (p) {
if (x==tile_x(p->tile) && y==tile_y(p->tile)) return 1; //如果 (x,y) 曾经经过,失败
p=p->father;
}
h=father->h+1;
if (h>=dis_map[y][x]) return 1; // 如果曾经有更好的方案移动到 (x,y) 失败
dis_map[y][x]=h; // 记录这次到 (x,y) 的距离为历史最佳距离

// 将这步方案记入待处理队列
p=(TREE)malloc(sizeof(*p));
p->father=father;
p->h=father->h+1;
p->tile=tile_num(x,y);
enter_queue(p,p->h+judge(x,y));
return 0;
}

// 路径寻找主函数
void findpath(int *path)
{
TREE root;
int i,j;
stacktop=0;
for (i=0;i<map_h;i++)
for (j=0;j<map_w;j++)
dis_map[i][j]=MAXINT;
init_queue();
root=(TREE)malloc(sizeof(*root));
root->tile=tile_num(start_x,start_y);
root->h=0;
root->father=NULL;
enter_queue(root,judge(start_x,start_y));
for (;;) {
int x,y,child;
TREE p;
root=get_from_queue();
if (root==NULL) {
*path=-1;
return;
}
x=tile_x(root->tile);
y=tile_y(root->tile);
if (x==end_x && y==end_y) break; // 达到目的地成功返回

child=trytile(x,y-1,root); //尝试向上移动
child&=trytile(x,y+1,root); //尝试向下移动
child&=trytile(x-1,y,root); //尝试向左移动
child&=trytile(x+1,y,root); //尝试向右移动
if (child!=0)
pop_stack(); // 如果四个方向均不能移动,释放这个死节点
}

// 回溯树,将求出的最佳路径保存在 path[] 中
for (i=0;root;i++) {
path[i]=root->tile;
root=root->father;
}
path[i]=-1;
freetree();
}

void printpath(int *path)
{
int i;
for (i=0;path[i]>=0;i++) {
gotoxy(tile_x(path[i])+1,tile_y(path[i])+1);
cprintf("\xfe");
}
}

int readmap()
{
FILE *f;
int i,j;
f=fopen("map.dat","r");
assert(f);
fscanf(f,"%d,%d\n",&map_w,&map_h);
for (i=0;i<map_h;i++)
fgets(&map[i][0],map_w+1,f);
fclose(f);
start_x=-1,end_x=-1;
for (i=0;i<map_h;i++)
for (j=0;j<map_w;j++) {
if (map[i][j]=='s') map[i][j]=' ',start_x=j,start_y=i;
if (map[i][j]=='e') map[i][j]=' ',end_x=j,end_y=i;
}
assert(start_x>=0 && end_x>=0);
return 0;
}

void showmap()
{
int i,j;
clrscr();
for (i=0;i<map_h;i++) {
gotoxy(1,i+1);
for (j=0;j<map_w;j++)
if (map[i][j]!=' ') cprintf("\xdb");
else cprintf(" ");
}
gotoxy(start_x+1,start_y+1);
cprintf("s");
gotoxy(end_x+1,end_y+1);
cprintf("e");
}

int main()
{
int path[MAXINT];
readmap();
showmap();
getch();
findpath(path);
printpath(path);
getch();
return 0;
}
wanggongming 2005-05-10
  • 打赏
  • 举报
回复
楼上的朋友,你好,你的这个程序我见过,原来刚开始做这个程序时我参考的就是这个程序,但是这个程序有些问题:
1:VC中没有gotoxy函数!
2:VC中没有clrscr()擦去屏幕的函数!
如果这个程序改编一下也可以,但是怎么改呀!!!
inlin 2005-05-09
  • 打赏
  • 举报
回复
那里面怎么没有main函数
inlin 2005-05-09
  • 打赏
  • 举报
回复
学习!
sun428 2005-05-07
  • 打赏
  • 举报
回复
学习,学习!
delixizhou 2005-05-07
  • 打赏
  • 举报
回复
。。。我前2天刚用a*算法写了个八数码问题的求解。。感觉蛮亲切的。。。只不过感觉楼主的评估函数很简单啊,可以自己设计一个嘛
somedummy 2005-05-02
  • 打赏
  • 举报
回复
A*这么短?不是吧?

这个东西C小版的斑竹BluntBlade做过,不过是C++的……
sailorjun 2005-04-28
  • 打赏
  • 举报
回复
我的可以通过
zhousqy 2005-04-28
  • 打赏
  • 举报
回复
學習。
zhangqiushui 2005-04-28
  • 打赏
  • 举报
回复
呵呵
学习
帮顶
zengwujun 2005-04-28
  • 打赏
  • 举报
回复
呵呵,又来了,再学习

3,881

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 其它技术问题
社区管理员
  • 其它技术问题社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧