让A *寻路工作的问题

时间:2018-02-21 14:54:27

标签: c++ arrays a-star

我的A * Pathfinding算法存在问题。

我对C ++编程很新,我试图通过二维数组形式的地图网格来导航演员。

我现在已经尝试了几天,但无济于事。我查阅了无数的教程和其他C ++代码,并尝试将其塑造成我的规格,但我无法得到结果。

我不知道我哪里出错了。

对于问题模糊的道歉,没有特定问题,问题是代码似乎从开放列表中选择随机区块,并且永远不会返回正确的值。

对于意大利面条代码也很抱歉。

提前致谢

的main.cpp

int moveCostG(int x, int y, int dx, int dy) {
    int g = (abs(dx - x) + abs(dy - y));
    return g;
}

int moveCostH(int dx, int dy, int nx, int ny){
    int h = (abs(nx - dx) + abs(ny - dy));
    return h;
}

int moveCostF(int x, int y, int dx, int dy, int nx, int ny) {
    return (moveCostG(x, y, dx, dy) + moveCostH(dx, dy, nx, ny));
}

bool searchList(std::vector<coordList> n_list, int search_term_x, int search_term_y) {
    int size = sizeof(n_list)/sizeof(coordList);
    for (i = 0; i < size; i++) {
        if ((n_list[i].x == search_term_x) && (n_list[i].y == search_term_y)){
            return true;
        }
    }
    return false;
}

void pathfinding(int x, int y, int nx, int ny) {
    //x, y = current location
    //dx, dy = changed location
    //nx, ny = location to go to
    int size = 0;
    int dx = x;
    int dy = y;
    bool found = false;

    //boolMap is a 2d adjacency matrix that represents whether or not a tile
    //is passable.
    //0 = Passable, 1 = NOT passable
  int boolMap[MAX_X][MAX_Y];
  for (i = 0; i < MAX_X; i++) {
    for (j = 0; j < MAX_Y; j++) {
      if (!map[i][j].layer1.passable || !map[i][j].layer2.passable) {
        boolMap[i][j] = 1;
      }
      else {
        boolMap[i][j] = 0;
      }
    }
  }

    //Creating lists
    std::vector<coordList> openList;
    std::vector<coordList> closedList;

    //Adding starting location to closed list
    coordList current;
    current.x = x;
    current.y = y;
    current.f = moveCostF(x, y, dx, dy, nx, ny);
    closedList.push_back(current);

    //Pushing a placeholder to list so search function works
    openList.push_back(current);

    //Adding next door neighbours
    current.x = dx + 1;
    current.y = dy;
    current.f = moveCostF(x, y, dx + 1, dy, nx, ny);
    if (boolMap[current.x][current.y] == 0){ //Check if tile is passable
        if (searchList(closedList, current.x, current.y) == true) {
            //found in closed -- ignore it
        }
        else if (searchList(openList, current.x, current.y) == false){
            openList.push_back(current);
            size++;
        }
    }

    current.x = dx - 1;
    current.y = dy;
    current.f = moveCostF(x, y, dx - 1, dy, nx, ny);

    if (boolMap[current.x][current.y] == 0){ //Check if tile is passable
        if (searchList(closedList, current.x, current.y) == true) {
            //found in closed -- ignore it
        }
        else if (searchList(openList, current.x, current.y) == false){
            openList.push_back(current);
            size++;
        }
    }

    current.x = dx;
    current.y = dy + 1;
    current.f = moveCostF(x, y, dx, dy + 1, nx, ny);

    if (boolMap[current.x][current.y] == 0){ //Check if tile is passable
        if (searchList(closedList, current.x, current.y) == true) {
            //found in closed -- ignore it
        }
        else if (searchList(openList, current.x, current.y) == false){
            openList.push_back(current);
            size++;
        }
    }

    current.x = dx;
    current.y = dy - 1;
    current.f = moveCostF(x, y, dx, dy - 1, nx, ny);

    if (boolMap[current.x][current.y] == 0){ //Check if tile is passable
        if (searchList(closedList, current.x, current.y) == true) {
            //found in closed -- ignore it
        }
        else if (searchList(openList, current.x, current.y) == false){
            openList.push_back(current);
            size++;
        }
    }

    //Removing placeholder of openList
    openList.erase(openList.begin());

    //--- NOW ALL INITIAL SQUARES HAVE BEEN CALCULATED
    bool first_time = true;
    //While?
    while (found == false) {
        if (first_time == false) {

            //Add new walkable tiles
            current.x = dx + 1;
            current.y = dy;
            current.f = moveCostF(x, y, dx + 1, dy, nx, ny);
            if (boolMap[current.x][current.y] == 0){ //Check if tile is passable
                if (searchList(closedList, current.x, current.y) == true) {
                    //found in closed -- ignore it
                }
                else if (searchList(openList, current.x, current.y) == false){
                    openList.push_back(current);
                    size++;
                }
            }

            current.x = dx - 1;
            current.y = dy;
            current.f = moveCostF(x, y, dx - 1, dy, nx, ny);

            if (boolMap[current.x][current.y] == 0){ //Check if tile is passable
                if (searchList(closedList, current.x, current.y) == true) {
                    //found in closed -- ignore it
                }
                else if (searchList(openList, current.x, current.y) == false){
                    openList.push_back(current);
                    size++;
                }
            }

            current.x = dx;
            current.y = dy + 1;
            current.f = moveCostF(x, y, dx, dy + 1, nx, ny);

            if (boolMap[current.x][current.y] == 0){ //Check if tile is passable
                if (searchList(closedList, current.x, current.y) == true) {
                    //found in closed -- ignore it
                }
                else if (searchList(openList, current.x, current.y) == false){
                    openList.push_back(current);
                    size++;
                }
            }

            current.x = dx;
            current.y = dy - 1;
            current.f = moveCostF(x, y, dx, dy - 1, nx, ny);

            if (boolMap[current.x][current.y] == 0){ //Check if tile is passable
                if (searchList(closedList, current.x, current.y) == true) {
                    //found in closed -- ignore it
                }
                else if (searchList(openList, current.x, current.y) == false){
                    openList.push_back(current);
                    size++;
                }
            }

        }

        first_time = false;

        //Setting lowest to high number so first result is always lower
        int lowest = 999999999;
        int lowest_place;

        //Finding lowest F value
        for (i = 0; i < size; i++) {
            if (openList[i].f < lowest){
                lowest = openList[i].f;
                lowest_place = i;
            }
        }

        //Adding the lowest to the closed list
        closedList.push_back(openList[lowest_place]);
        //Removing lowest from open list
        openList.erase(openList.begin() + lowest_place);
        size--;

        //Checking if next location is the location is the destination
        dx = openList[lowest_place].x;
        dy = openList[lowest_place].y;
        if ((dx == nx) && (dy == ny)) {
            printf("FOUND IT!");
            found = true;
            break;
        }

    }
}

使用的类:

class coordList{
public:
    int x;
    int y;
    int f;
    coordList();
};

0 个答案:

没有答案