Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added astar
Binary file not shown.
31 changes: 31 additions & 0 deletions src/astar.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <tuple>
#include <queue>
#include <math.h>
#include <stack>

class Grid {
public:
Expand Down Expand Up @@ -96,6 +97,32 @@ double calculateHValue(Grid grid, const Pair& src) {
+ pow((src.second - dest.second), 2.0));
}

void tracePath(cell *cellDetails, int num_r, int num_c) {

cell **cellDetail;
cellDetail = &cellDetails;
std::stack<Pair> Path;
int row = num_r - 1;
int col = num_c - 1;
std::cout << "row: " << num_r << " and col: " << num_c << std::endl;
Pair next_node = cellDetail[row][col].parent;
std::cout << "wehre we breaking" << std::endl;
Pair third_node = cellDetail[2][2].parent;
std::cout << "(2,2)'s parent: (" << third_node.first << "," << third_node.second << ")" << std::endl;
do {
Path.push(next_node);
row = next_node.first;
col = next_node.second;
} while (cellDetail[row][col].parent != next_node);

Path.push(Pair(row, col));
while (!Path.empty()) {
Pair p = Path.top();
Path.pop();
std::cout << "-> (" << p.first << "," << p.second << ") ";
}
}

void aSearchAlgorithm(Grid grid) {
// 1) Initialize Open List
// 2) Initialize the Closed List. Put the starting node
Expand Down Expand Up @@ -145,6 +172,10 @@ void aSearchAlgorithm(Grid grid) {
if (neighbor.first == grid.getRowSize() - 1 &&
neighbor.second == grid.getColSize() - 1) {
std::cout << "Destination cell reached" << std::endl;
cellDetails[neighbor.first][neighbor.second].parent = { r, c };
tracePath((cell *)cellDetails,
grid.getRowSize(),
grid.getColSize());
return;
// ii) else, compute both g and h for successor
// successor.g = q.g + distance between successor and q
Expand Down