I am trying to read in from 2 text files (one contains the start and end coordinates, the other is a map of costs for use with the Manhatten Distance) and I'm really struggling with the implementation of the code from various algorithms I've found.
I've been able to read in the text files and put them into 2 separate arrays fine, the real issue I'm having is with adding to the open list and compares their costs so that the smallest goes onto the priority queue and then compare its neighbors, I just cannot work this out after hours upon hours of reading and watching YouTube videos on the subject, any help?
Here is my code so far:
#include "stdafx.h"
#include <iostream>
#include <string>
#include <fstream>
#include <deque>
#include <math.h>
#include <vector>
#include <deque>
using namespace std;
struct pNode
{
int xPos;
int yPos;
pNode* Parent;
pNode* child;
float g; //Cost of this Node plus predecessors
float h;//Heuristic estimate to Goal state
float f; // f= g+h
pNode() :
Parent(0),
child(0),
g(0.0f),
h(0.0f),
f(0.0f)
{
bool wall = false;
bool water = false;
bool grass = false;
bool startNode = false;
bool endNode = false;
}
};
class compare
{
public:
bool compareNodes()
{
pNode *x;
pNode *y;
return x->f > y->f;
}
};
class dMap
{
public:
static const int ARRAY_SIZE = 100;
void LoadFile();
void Display() const;
void priorityQueue();
void aStar();
private:
pNode mArray[ARRAY_SIZE];
pNode mArrayCoords[ARRAY_SIZE];
};
void dMap::LoadFile()
{
ifstream infile("dMap.txt");
if (!infile)
{
cout << "Cannot open File";
exit(1);
}
for (int i = 0; i < ARRAY_SIZE; i++)
{
infile >> mArray[i].xPos >> mArray[i].yPos;
}
}
void dMap::priorityQueue()
{
int disX = mArray[ARRAY_SIZE].xPos;
int disY = mArray[ARRAY_SIZE].yPos;
int heuristic(int xPos, int yPos);
struct pQueue
{
int fx;
int fy;
int x;
int y;
int cost;
int h;
int totalCost;
pQueue(int _x, int _y, int _cost)
{
x = _x;
y = _y;
cost = _cost;
h = heuristic(x, y);
totalCost = cost + h;
}
int manhattenDistance(int xPos, int yPos)
{
return abs(xPos - fx) + abs(yPos - fy);
}//Manhatten Distance
bool operator<(const pQueue &b)const
{
return totalCost>b.totalCost;
}
};
}
void dMap::aStar()
{
LoadFile();
pNode mArrayCoords[ARRAY_SIZE];
ifstream infile("dCoords.txt");
if (!infile)
{
cout << "Cannot open File";
exit(1);
}
for (int i = 0; i <9; i++)
{
infile >> mArrayCoords[i].xPos >> mArrayCoords[i].yPos;
}
deque <pNode*> openList;
pNode* startNode = new (pNode);
deque <pNode*> closedList;
pNode* endNode = new (pNode);
pNode* currentNode = new (pNode);
deque <pNode*> finalList;
//startNode ->
}