dxxds 发表于 2013-4-20 10:32:55

简单的自动寻路实现(真的超简单哦~)

本帖最后由 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 =
{   
    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;
               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 = {1, 0, -1, 0};//4个方向上的x的变化;
          const int dy = {0, -1, 0, 1};//4个方向上的y的变化;
          const float cost = {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);
          }
   }
};

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

晓♠晨 发表于 2013-4-20 12:34:01

战旗是陆战棋?

紫色的雨 发表于 2013-4-20 12:59:24

好像很不错、

繁尘、离梦 发表于 2013-4-20 14:43:55

深搜的应用。
你其实可以做成桌面程序玩,然后自动生成这个map随机地图,这样的话,新手也更有兴趣学习。

dxxds 发表于 2013-4-21 00:08:31

繁尘、离梦 发表于 2013-4-20 14:43 static/image/common/back.gif
深搜的应用。
你其实可以做成桌面程序玩,然后自动生成这个map随机地图,这样的话,新手也更有兴趣学习。 ...

也不错,有空我做做吧

这个代码确实看起来太临时了

dxxds 发表于 2013-4-21 07:28:14

晓♠晨 发表于 2013-4-20 12:34 static/image/common/back.gif
战旗是陆战棋?

是战棋类游戏

钛合金节操 发表于 2013-4-21 18:28:40

跟风发一个gcc版本a*最优路径搜索的~

PC1500 发表于 2013-4-22 16:06:43

咩……目标我的项目神马的可以借鉴……留爪

a692637801 发表于 2013-4-28 21:59:40

你告诉我他有多少行!!!!038~

狂风飘雪 发表于 2013-5-1 16:42:33

话说你这个程序的做法让我觉得和ACM的最优路线出迷宫的一些做法很像~~诶诶~LZ加油吧

汝欠咱的一生 发表于 2013-5-2 22:02:28

DFS吧。

z师无限 发表于 2013-8-21 21:51:00

恩恩,蛮好的,只是看完好废力啊,这么长的代码,没深厚的功力是不行的
页: [1]
查看完整版本: 简单的自动寻路实现(真的超简单哦~)