| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 450 人关注过本帖
标题:A* 代码
只看楼主 加入收藏
SunshineGirl
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:129
专家分:131
注 册:2012-3-20
结帖率:52.94%
收藏
已结贴  问题点数:8 回复次数:3 
A* 代码
https://code.
A* 是项目里面技术含量最低的一段代码
static PathFind* pathFind = NULL;

PathFind* PathFind::getTheOnlyInstance()
{
        if (!pathFind)
        {
                pathFind = new PathFind();
        }

        return pathFind;
}

// A*3õê¼»ˉ
Astar* PathFind::newAstar()
{
        Astar* astar = (Astar*)malloc(sizeof(Astar));
    astar->openHead = (AstartListNode*)malloc(sizeof(AstartListNode));
    astar->closedHead = (AstartListNode*)malloc(sizeof(AstartListNode));
    astar->openHead->next = NULL;
    astar->closedHead->next = NULL;
    return astar;
}

// A*êí·Å
void PathFind::deleteAstar(Astar* astar)
{
    while (astar->openHead != NULL)
    {
        AstartListNode* t = astar->openHead;
        astar->openHead = astar->openHead->next;
        free(t);
        t = NULL;
    }

        while (astar->closedHead != NULL)
    {
        AstartListNode* t = astar->closedHead;
        astar->closedHead = astar->closedHead->next;
        free(t);
        t = NULL;
    }

        free(astar);
}

// ½áμã3õê¼»ˉ
void PathFind::AstarNodeInit(AstartListNode* current, AstartListNode* father, int x, int y, int endX, int endY)
{
        current->x = x;
    current->y = y;
    current->father = father;
    current->next = NULL;

    if (father != NULL)
    {
        current->f = father->f + 1;
    }
    else
    {
        current->f = 0;
    }

    current->g = abs(endX - x) + abs(endY - y);
    current->h = current->f + current->g;
}

// è¡μÃ3é±¾×îD¡μĽáμã
AstartListNode* PathFind::AstarGetMinCostList(Astar* astar)
{
        AstartListNode* min = astar->openHead->next;
    AstartListNode* current = min->next;

    while (current != NULL)
    {
        if (current->h < min->h)
        {
            min = current;
        }

        current = current->next;
    }

    return min;
}

// ìí¼ó½áμãμ½á′±í
void PathFind::AstarAddNode(AstartListNode* head, AstartListNode* node)
{
        while (head->next != NULL)
    {
        head = head->next;
    }

    head->next = node;
}

// ′óá′±íÖDé¾3y½áμã
void PathFind::AstarRemoveNode(AstartListNode* head, AstartListNode* node)
{
        AstartListNode* current = head;
    head = head->next;

    while (head != NULL)
    {
        if (head == node)
        {
            current->next = head->next;
            head->next = NULL;
            break;
        }
        else
        {
            current = head;
            head = head->next;
        }
    }
}

// ¼ì2éTileêÇ·ñÔúá′±íÖD
bool PathFind::AstarCheckNodeInList(int x, int y, AstartListNode* head)
{
        bool result = false;
    head = head->next;

    while (head != NULL)
    {
        if (head->x == x && head->y == y)
        {
            result = true;
            break;
        }
        else
        {
            head = head->next;
        }
    }

    return result;
}

// ¼ì2éTileêÇ·ñêÇμØí¼μÄ·ÕÏ
bool PathFind::AstarIsBlock(int x, int y)
{
        if (x >= 0 && x < tiledMap->getMapSize().width && y >= 0 && y < tiledMap->getMapSize().height)
        {      
                CCTMXLayer* mapLayer = tiledMap->layerNamed("mainCityLayer");
                unsigned int gid = mapLayer->tileGIDAt(ccp(x, y));

                CCDictionary* tileProperty = tiledMap->propertiesForGID(gid);
                int v = ((CCString*)tileProperty->objectForKey("walk"))->intValue();

        if (v == 0)
        {
            return true;
        }
    }
    else
    {
        return false;
    }

    return false;
}

bool PathFind::AStarSearch(CCTMXTiledMap* tiledMap, int startX, int startY, int endX, int endY)
{
        astarPathCount = 0;
        astarPathList.clear();

        this->tiledMap = tiledMap;

        this->startX = startX;
        this->startY = startY;
        this->endX = endX;
        this->endY = endY;

        if (AstarIsBlock(endX, endY))
        {
                return false;
        }

        int offsetX[] = {0, 0, -1, 1, -1, -1, 1, 1};
    int offsetY[] = {1, -1, 0, 0, 1, -1, 1, -1};
   
        Astar* astar = newAstar();

    AstartListNode* currentNode = NULL;
    AstartListNode* startNode = (AstartListNode*)malloc(sizeof(AstartListNode));
    AstarNodeInit(startNode, NULL, startX, startY, endX, endY);
    // °ÑÆeê¼½áμã¼óèëOpenList
    AstarAddNode(astar->openHead, startNode);

    // èç2éOpenList2»Îa¿Õ
    while (astar->openHead->next != NULL)
    {
        // è¡μÃ3é±¾×îD¡μĽáμã
        currentNode = AstarGetMinCostList(astar);

        // èç1ûμ±Ç°½áμãêÇÄ¿±ê½áμã
        if (currentNode->x == endX && currentNode->y == endY)
        {
            break;
        }
        else
        {
            // °Ñμ±Ç°½áμãìí¼óμ½Closed±íÖD
            AstarAddNode(astar->closedHead, currentNode);
            // °Ñμ±Ç°½áμã′óOpen±íÖDé¾3y
            AstarRemoveNode(astar->openHead, currentNode);

            for (int i = 0; i < 8; i++)
            {
                int x = currentNode->x + offsetX[i];
                int y = currentNode->y + offsetY[i];

                                if (x < 0 || x >= tiledMap->getMapSize().width || y < 0 || y >= tiledMap->getMapSize().height)
                {
                    continue;
                }
                else
                {
                    if (!AstarCheckNodeInList(x, y, astar->openHead)
                        && !AstarCheckNodeInList(x, y, astar->closedHead)
                        && !AstarIsBlock(x, y)
                       )
                    {
                        AstartListNode* endNode = (AstartListNode*)malloc(sizeof(AstartListNode));
                        AstarNodeInit(endNode, currentNode, x, y, endX, endY);
                        AstarAddNode(astar->openHead, endNode);
                    }
                }
            }
        }
    }

    if (astar->openHead->next == NULL && (currentNode->x != endX || currentNode->y != endY))
    {
        astarPathCount = 0;
        return false;
    }
    else
    {
        while (currentNode != NULL)
        {
                        CCPoint tilePoint;
                        tilePoint.x = currentNode->x;
                        tilePoint.y = currentNode->y;
            
                        CCTMXLayer* mapLayer = tiledMap->layerNamed("mainCityLayer");
                        CCSprite* tile = mapLayer->tileAt(ccp(tilePoint.x, tilePoint.y));
                        astarPathList.push_front(tilePoint);
            currentNode = currentNode->father;
            astarPathCount++;
                }

                return true;
    }

    deleteAstar(astar);
    return false;
}


[ 本帖最后由 SunshineGirl 于 2015-4-16 12:14 编辑 ]
2014-06-12 15:19
wangnannan
Rank: 18Rank: 18Rank: 18Rank: 18Rank: 18
等 级:贵宾
威 望:87
帖 子:2546
专家分:9359
注 册:2007-11-3
收藏
得分:2 
大方一点吧 要给 就把SERVER端 代码给个

出来混,谁不都要拼命的嘛。 。拼不赢?那就看谁倒霉了。 。有机会也要看谁下手快,快的就能赢,慢。 。狗屎你都抢不到。 。还说什么拼命?
2014-06-12 15:50
tlliqi
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
等 级:贵宾
威 望:204
帖 子:15453
专家分:65956
注 册:2006-4-27
收藏
得分:2 
不懂
2014-06-12 18:18
yuccn
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:何方
等 级:版主
威 望:167
帖 子:6814
专家分:42393
注 册:2010-12-16
收藏
得分:2 
换个马甲有出现了。呵呵

我行我乐
公众号:逻辑客栈
我的博客:
https://blog.yuccn. net
2014-06-13 21:55
快速回复:A* 代码
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.030018 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved