当前位置: 首页>>代码示例>>C++>>正文


C++ AStar::findPath方法代码示例

本文整理汇总了C++中AStar::findPath方法的典型用法代码示例。如果您正苦于以下问题:C++ AStar::findPath方法的具体用法?C++ AStar::findPath怎么用?C++ AStar::findPath使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在AStar的用法示例。


在下文中一共展示了AStar::findPath方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: main

int main(){
    int **map = new int*[10 * 10];
    memset(map, 0, 10 * 10);

    AStar* astar = AStar::create(map,10,10);
    vector<ASNode> path = astar->findPath(0,1,9,9);
    astar->printPath(path);
    return 1;
}
开发者ID:owlwisp,项目名称:GameTools,代码行数:9,代码来源:Astar.cpp

示例2: move

void Boss::move(int dir) {
    if (died) {
        return;
    }
    
    bool cancelCurrentPath = false;
    if (pathAStar != NULL) {
        // boss follow the path left.
        if (pathAStar->size() > 1) {
            Tile *currentTile = pathAStar->popBack();
            int nextRow = currentTile->row;
            int nextCol = currentTile->col;
            // if the next position is empty
            if (sim->board->validate(nextRow, nextCol) &&
                sim->board->getProp(nextRow, nextCol) == NULL &&
                sim->board->getUnit(nextRow, nextCol) == NULL) {
                sim->board->setUnit(row, col, NULL);
                sim->board->setUnit(nextRow, nextCol, this);
            }
            else {
                pathAStar->pushBack(currentTile);
                cancelCurrentPath = true;
            }
            
            if (!cancelCurrentPath) {
                return;
            }
        }
        
        delete pathAStar;
        pathAStar = NULL;
    }
    
    AStar *aStar = new AStar();
    
    DLLContainer<Tile *> *path = aStar->findPath(row, col, sim->board->getHero()->getRow(), sim->board->getHero()->getCol());
    delete aStar;
    if (path != NULL && path->size() > 2) {
        pathAStar = path;
        path->popBack(); // boss skip boss' position.
//        while (!path->isEmpty()) {
//            Tile *currentTile = path->popBack();
//            cout << "(" << currentTile->row << ", " << currentTile->col << ")->";
//        }
//        cout << endl;
//        sim->board->print();
//        exit(1);
        return;
    }
    
    if (path != NULL) {
        delete path;
    }
        
    // if hero is adjavcent then hit him.
    for (int i = row-1; i <= row+1; i++) {
        for (int j = col-1; j <= col+1; j++) {
            if (sim->board->validate(i, j) &&
                sim->board->getUnit(i, j) != NULL &&
                sim->board->getUnit(i, j)->isHero()) {
                sim->board->getUnit(i, j)->decHp(atk); // found! attak!!!
                return;
            }
        }
    }
        
    Monster::move(dir);
}
开发者ID:changyook21,项目名称:Classic-RPG-game,代码行数:68,代码来源:monster.cpp

示例3: main

int main(int argc, const char * argv[])
{
    AStar astar;
    int rows = 20, columns = 20;
    Tiles env(rows, columns);
    random_device generator;
    uniform_int_distribution<int> distribution(1,20);
    
    vector<int> impassableNodes;
    for (int i = 0; i < columns * rows; i++)
    {
        int dice_roll = distribution(generator);
        if (dice_roll == 3)
        {
            impassableNodes.push_back(i);
        }
    }
    
    cout << "blocks(" << impassableNodes.size() << "): ";
    for (auto i : impassableNodes)
    {
        env.SetUntraverseable(i);
        cout << i << ", ";
    }
    cout << endl;

    
    
    std::uniform_int_distribution<int> startEndDistribution(0,rows * columns-1);
    int start, end;
    while (true) {
        start = startEndDistribution(generator);
        if (std::find(impassableNodes.begin(), impassableNodes.end(), start) == impassableNodes.end())
        {
            break;
        }
    }
    while (true) {
        end = startEndDistribution(generator);
        if (std::find(impassableNodes.begin(), impassableNodes.end(), end) == impassableNodes.end())
        {
            break;
        }
    }
    cout << "start = " << start << ", end  = " << end << ";" << endl;

    env.DrawMap(start, end);
    
    std::cout<<"-----------\n\n\n\n";
    
    astar.findPath(env, start, end);
//    astar.findPath(env, 11, 18);
    auto p = env.GetSmoothedPath(astar.getResultPath());
    
    for (auto n : p)
    {
        cout << n << ", ";
    }
    cout << endl;
    
    env.DrawSolution(p);
    return 0;
}
开发者ID:geediiiiky,项目名称:astar,代码行数:63,代码来源:main.cpp


注:本文中的AStar::findPath方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。