Ejemplo n.º 1
0
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;
}
Ejemplo n.º 2
0
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);
}
Ejemplo n.º 3
0
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;
}