How to use this code? [Newbie]

Hello, I am new here :)
I am teaching myself A Star algorithm and I have found this code in a book I'm reading about AI but the book doesn't show an example use and I am having trouble using the code. :(

Here is the class and the implementation files, I uploaded them on textuploader due to max text limit:
http://textuploader.com/drtih

Any example use would be very helpful to me, thank you.

Here is my attempt (doesn't work but I might've gotten close):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int main()
{
	int startY = 3;
	int startX = 3;
	int endX = 8;
	int endY = 8;
	char s = 'A';
	char e = 'G';
	char path = '*';
	Pathfind* pathfinder = new Pathfind();

	pathfinder->startSearch(pathfinder->SearchSpace[startY][startX]);
	Node* end = pathfinder->closedList.back();

	while (end->_Type != START)
	{   
                cout << path;
		end = end->parentNode;
	}

	return 0;
}


Result: An infinite loop that keeps printing asterisks symbols. :(

Thank you!
Hi,

If you upload the code to somewhere that we can compile and run it that would be better, try this site.

http://coliru.stacked-crooked.com/

I already tried to compile, but: Make sure you have all the necessary include files, and fix up the comments (/ /)which cause compile errors.

Compile with a high level of warnings:

g++ -std+c++14 -Wall -Wextra -pedantic-errors *.cpp -o ExeFilename

http://www.cplusplus.com/forum/general/183731/#msg899203

Also, try to use the debugger in your IDE, it will save you days of staring at code. Create a watch list of variables, a break point before where you think there might be a problem step through the code 1 line at a time, deduce where things go wrong.
Last edited on
Thank you for your message, here is the code: http://coliru.stacked-crooked.com/
You haven't put the includes in.

When you load it to coliru, press the share button, so we get your code.
Result: An infinite loop that keeps printing asterisks symbols. :(


Don't know what you used, but after attempts to fix obvious errors, it doesn't compile for me. Maybe There is still code missing.?
Here you go guys http://coliru.stacked-crooked.com/a/4bccef86ab29114e I'm pretty sure that's all the author included in the book, they just didn't demonstrate how to use the code because writing an int main() function is too much work apparently :p
main.cpp:32:6: warning: 'Node::_iY' will be initialized after [-Wreorder]

int _iY;


Those warnings are due to the members not being initialised in the same order as they are in the class declaration.

TheIdeasMan wrote:
Compile with a high level of warnings:

g++ -std+c++14 -Wall -Wextra -pedantic-errors *.cpp -o ExeFilename


Google is your best friend, I found this link with code for main:

http://code.activestate.com/recipes/577457-a-star-shortest-path-algorithm/
Thank you, I think this A* is bad though as it hits the wall. For example:

Suppose you have this where A is the starting point and B is the destination, W is the wall and P is the path.
1
2
3
4
5
6
7
xxxxxxx
xxxxxxx
xAxwxB
xxxwxxx
xxxwxxx
xxxxxxx
xxxxxxx

Run the search:
1
2
3
4
5
6
7
xxxxxxx
xxxxxxx
xApwxB
xxpwxpx
xxpwpxx
xxxpxxx
xxxxxxx

This, I think, is false as it should look something like this:
1
2
3
4
5
6
7
xxxxxxx
xxxxxxx
xApwxB
xxpwxpx
xxpwxpx
xxpppxx
xxxxxxx


Topic archived. No new replies allowed.