[i=s] 本帖最后由 dxxds 于 2013-4-21 07:27 编辑 最近想写个战棋游戏(本人酷爱战棋) 所以先赶紧撸了一个自动寻路的代码 有兴趣的就看吧 #include "micropather.h" #include <stdio.h> #include <math.h> #include <windows.h> using namespace micropather; //地图的长宽 x是列,y是行; #define X_MAP 15 #define Y_MAP 10 const int g_map[X_MAP * Y_MAP + 1] = { 0,0,1,1,1,0,0,0,1,1,0,0,1,0,1, 0,1,0,0,0,0,0,1,1,0,0,0,0,0,1, 0,0,1,0,1,1,0,0,0,1,1,0,1,0,1, 1,0,1,0,0,0,1,1,0,1,1,0,0,0,1, 1,0,1,0,1,0,0,1,0,0,0,0,1,0,0, 0,0,0,1,0,1,0,1,1,0,1,1,1,0,1, 0,0,0,0,0,1,0,0,0,1,1,0,0,0,1, 0,0,0,1,1,1,1,1,0,0,1,0,1,0,1, 1,0,0,0,1,0,0,0,1,0,1,0,1,1,1, 0,0,1,0,0,0,1,0,0,0,1,0,0,0,0, }; //可以试着改变上面的地图看实际效果 void gotoxy(int x,int y) { COORD point; point.X=x;point.Y=y; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),point); } class Cway_find : public Graph { private: Cway_find(const Cway_find& ); void operator = (const Cway_find& ); MicroPather* m_pather; int plr_x, plr_y; std::vector<void*> m_path; public: Cway_find() : m_pather ( 0 ) { m_pather = new MicroPather(this); } virtual ~Cway_find() { delete m_pather; } int X() { return plr_x; } int Y() { return plr_y; } unsigned Checksum() { m_pather->Checksum(); } void ClearPath() { m_path.resize( 0 ); } int Passable(int nx, int ny) { if( nx >= 0 && nx <X_MAP && ny >= 0 && ny <Y_MAP) { int index = ny * X_MAP + nx; int c = g_map[index]; if ( c == 0) return 1; //可通行; } return 0; //不可通行; } void SetStartPos(int x, int y) { plr_x = x; plr_y = y; } int SetEndPos(int nx, int ny) { int result = 0; if(Passable(nx, ny) == 1) { float totalCost; result = m_pather->Solve(XYToNode(plr_x, plr_y), XYToNode(nx, ny), &m_path, &totalCost); if(result == MicroPather::SOLVED) { plr_x = nx; plr_y = ny; } } return result; } //把索引号转化为XY坐标; void NodeToXY(void *node, int *x, int *y) { int index = (int)node; *y = index / X_MAP; *x = index - *y * X_MAP; } //把XY坐标转化为索引号; void* XYToNode(int x, int y) { return (void*)(y * X_MAP + x); } //最小代价估计函数,求A*算法中的h值; virtual float LeastCostEstimate(void *nodeStart, void *nodeEnd) { int xStart, yStart, xEnd, yEnd; NodeToXY(nodeStart, &xStart, &yStart); NodeToXY(nodeEnd, &xEnd, &yEnd); int dx = xStart - xEnd; int dy = yStart - yEnd; return (float)sqrt((double)(dx * dx) + (double)(dy * dy)); } virtual void AdjacentCost(void *node, std::vector<StateCost> *neighbors) { int x, y; //上下左右四个方向; const int dx[4] = {1, 0, -1, 0}; //4个方向上的x的变化; const int dy[4] = {0, -1, 0, 1}; //4个方向上的y的变化; const float cost[4] = {1.0f, 1.0f, 1.0f, 1.0f}; NodeToXY(node, &x, &y); for(int i = 0; i < 4; ++i) { int nx = x + dx; int ny = y + dy; //是否可行; int pass = Passable(nx, ny); if(pass == 1) { StateCost nodeCost = {XYToNode(nx, ny), cost}; neighbors->push_back(nodeCost); } } } virtual void PrintStateInfo(void *node) { int x, y; NodeToXY(node, &x, &y); system("pause>nul"); gotoxy(x*2,y); printf("☆"); gotoxy(0,12); printf("(%d,%d)",x,y); } void Print() { unsigned size = m_path.size(); int sum = X_MAP*Y_MAP; for (int i=0;i<sum;++i) { if(i>1 && (i)%X_MAP==0) printf("\n"); if(g_map==0) printf(" "); if (g_map==1) printf("■"); } for(unsigned k = 0; k < size; ++k) { PrintStateInfo(m_path[k]); } } }; int main() { Cway_find Cway_find; Cway_find.SetStartPos(0, 0); Cway_find.SetEndPos(14, 9); Cway_find.Print(); gotoxy(0,12); printf("到终点 10 秒后自动关闭程序"); Sleep(10000); return 0; } VS2008编译通过 以下是头文件下载链接 http://ishare.iask.sina.com.cn/f/36797563.html