Help with A* Pathfinding

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 openlist 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 viedos on the subject, any help?

Here is my code so far:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158

#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 ->




}
Lines 28-32: To be safe, you should initialize x and y also.

Lines 34-38 define and initialize 5 local variables that are destroyed when the constructor exits, so they don't actually do anything.

Line 50: Since x and y haven't been initialized, you're dereferenceing uninitialized pointers, which is never correct. x and y should probably be passed in as arguments.

Lines 86 & 87: If a C++ arrays has ARRAY_SIZE items, then they are accessed with indices 0 to ARRAY_SIZE-1, so mArray[ARRAY_SIZE] is out of bounds.

Lines 90-123: I suspect that strut pqueue should be defined outside the function, but it's hard to tell.

Can you tell us more about the problem that you're trying to solve? That will help us guide you.
closed account (48T7M4Gy)
https://en.wiktionary.org/wiki/Manhattan_distance
Yea, I have 2 txt files, one with a start and end coordinate

1
2
3
3 4

6 8



and one with the 'Map' like this:

1
2
3
4
5
6
7
8
9
10
1100011111
1001000011
1032111011
1030301011
1030301011
1030301011
1012111011
1010000011
1000111111
1111111111


using A Star to traverse through the map for the best route and output that path to a txt file.
Last edited on
closed account (48T7M4Gy)
https://www.raywenderlich.com/4946/introduction-to-a-pathfinding

Maybe this tutorial is useful as an overview.

What you need to do is:
1. Have a plan - preferably (and monotonously) pseudocode for it
2. Isolate the functionality you are having trouble with. (you've done that, sort of)
3, Forget about the main program and all its lines and write a test program on just the isolated part. Show us that with your pseudocode and I suspect that you'll get a better response because not everybody has expertise on A*
4. Once that isolated part is working the way you expect it to, put it into the main program.
5 Then and only then move to the next section on the pseudocode plan.

PS Since you are able to read in the file information successfully, hardcode it for the isolated test section.
closed account (48T7M4Gy)
http://www.cplusplus.com/forum/general/206036/
Topic archived. No new replies allowed.