At the moment I am trying to work out the A Star Pathfinding Algorithm.
Where I'm stuck is the following:
Assigning a 'Cost' to each node - I have a text file of a 10x10 array, in the array each 'node' has a cost of 0,1,2 or 3, this is how I'm reading it in:
The next issue I have:
I need to compare the nodes around the 'Current' Node, now, I can push the starting node and current to a list, I assume I then compare current/next to the already visited list and then I want the one with the lowest score (cost + manhattan distance) however, the way I've implemented it seems to ignore those nodes completely:
I know its moving start and current to the openlist as the debugging does tell me and when i outputted this part the start and current nodes were there just fine, it was the rest of it that was missing
void map()
{
vector<string> mArray;
int row = 10;
int col = 10;
ifstream inFile("dMap.txt");
string line;
while (getline(inFile, line))
{
mArray.push_back(line);
}
while (inFile && row <= 100)
{
inFile >> mArray[row][col];
if (inFile)
{
if (++col == 10)
{
++row;
col = 0;
}
}
}
}
First of all, in lines 11-14 you read the entire dMap.txt file and put it into your array. Then you try to read more information from the same file, but since you consumed the whole thing, lines 18-26 never get executed.
Then, if those lines did execute, your row and column both are initialized at 10, so the first element you place is mArray[10][10]. Then you increment col to 11, so it's never equal to 10, so you never change the value of row. So row never equals 100 (you really wanted 10 here) to end your loop. You end up populating mArray[10][10] - mArray[10][<number of characters in the file> + 9)]. Except mArray[10] is an empty string, so there is undefined behavior when you access its elements.
On top of all that, you never return the local variable mArray (declared in line 3), so whatever you did is lost as soon as you return from the function.
There are a LOT of things wrong with this function. You might want to start over and build up the function 1 step at a time. Make sure each step works before adding in more code.
You can test them yourself by stopping at line 15 and printing out each of the strings in the array. Make sure you have what you want and then move on to the next step.
And its the while loop thats wrong?
The 2nd while loop certainly has problems. Whether you need the 2nd while loop at all depends on what you are trying to accomplish in the 1st while loop. It does appear that you might be attempting to re-do in the 2nd while loop what you already did in the 1st, but it is not crystal clear.
And don't forget about the local variable mArray going out of scope before you do anything with it. Did you mean to pass in a std::vector& as an argument?
No, this is to set up a data array, vector isn't being passed.
So I have a array of numbers like
1 1 1 1 0 0 3 3 2 1
for 10 lines (has to work with random numbers between 0 - 3) and I want to set them in a 10 x 10 array so I can use them as a map (the numbers represent costs of each square)
You need to start over. What you have isn't even close to what you want.
No, this is to set up a data array, vector isn't being passed.
If the vector is not being passed in as an argument, then as soon as you are done with the function, it goes away and you can't use it any more.
vector<string> mArray;
This is a vector of strings. That means each entry is a string.
I want to set them in a 10 x 10 array
You want something like this:
std::vector<std::vector<int> >
So I have a array of numbers like
1 1 1 1 0 0 3 3 2 1
1 2 3 4
while (getline(inFile, line))
{
mArray.push_back(line);
}
This will append the string "1 1 1 1 0 0 3 3 2 1" as the next element of mArray. It does not convert anything to integers or even identify more than 1 element.
I think you want something along the lines of the following. (Note: I did not compile this, so there may be a syntax error or 2.)