I am attempting to create an implementation of the A* algorithm on a 2D grid and have arrived stuck at the point where I am needing to create a set of a node's neighbours. Below are the structs I am using.
// Holds values for x and y locations on the grid
struct Coord {
int x, y;
};
// holds data for each node required for A*
struct Node {
int type; // used for defining if this node is a blocker, empty, start or end
Coord location;
int g = 0;
int h = 0;
int f = g + h;
Node *parent_; // pointer to this node's parent
std::string debugmessage;
};
The error appears when I create this function here:
// finds a node's neighbours for A*
std::set<Node> neighbours(Node& n_) {
std::set<Node> neighbours_;
Node temp = n_;
int x = temp.location.x;
int y = temp.location.y;
// start at the location belonging to 'n_'
for (y; y < HEIGHT; y++) {
for (x; x < WIDTH; x++) {
// east
if (x < WIDTH - 1) {
neighbours_.insert(astarArray[x + 1][y]);
}
// west
if (x > 0) {
neighbours_.insert(astarArray[x - 1][y]);
}
// south
if (y < HEIGHT - 1) {
neighbours_.insert(astarArray[x][y + 1]);
}
// north
if (y > 0) {
neighbours_.insert(astarArray[x][y -1]);
}
}
}
return neighbours_;
}
Thank you for your time.