[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

Question
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

9.4

Printing off the Path in an Adjacency Matrix (Pacman)

Asked by Arcyani in C++ Programming Language, Game Programming, Programming Languages

Tags: Path Finding C++ Adjacency Matrix

Hello everyone.

I have an example of a Pacman game, we are doing path finding and we are looking at BFS (Breadth first search)

I need to be able to write down all the paths that the BFS search explores when searching for a path to the "goal". It suggests that It would be quicker to edit the pathPlanBFS function so that the program writes out the paths for you.

There are many loops within loops and I am not sure what entity I should be printing off or where to put the cout.

And can someone explain this line to me as well

if(adjacencyMatrix[currentIndex][otherIndex])

What is the if statement actually asking, if either is true or false?

- Thanks
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:
list<int> Level::planPathBFS(short currentX, short currentY, short goalX, short goalY, short debugMode)
{
        /*
        Each node in the graph has an index.
        For and given x and y position this can be found b calling index(x,y) e.g.
        goalIndex = index(goalX, goalY); 
        This function returns a path, a linked list of nodes, give a route through the map e.g.
        <555, 554, 553, 552, 551> 
        This path is contructed using a breadth first approach.
        The initial node is added to possible path list.
        This possible papth list is added to a second list, the list of possible paths.
        So after initialisation this list of possible paths only has one element,
        a list containing the current nodes index:
        <<555>> 
        This list is then expanded as follows:
                For each element in the list:
                        Take the tail of the list as the current node
                        Check for connections from this node to other nodes (using the adjacency matrix)
                        If there is a connection
                                Add a new path to the list consisting of the current path + the (other) new node
                                Check if this new is the goal node, if so return the current path
                        End 
        Should look something like this:
        Start node is 555
        Goal node is 552 
        <<555>>
        <<555,554>,<555,556>>
        <<555,554,553>,<555,554,555>,<555,556,555>,<555,556,557>>
        <<555,554,553,552>,... goal found to return path: <555,554,553,552>
                                 
        */
        list<int> path; // Final path to be constructed
        list<list<int>> pathTree; // BFS tree
        list<list<int>> pathTreeTmp; // BFS tree - used in working
        list<int> ppath;// Partial path used in BSF tree construction 
        list<list<int>>::iterator ptIter; // Iterator used for looping through the list of partial paths 
        int goalIndex; // Index of the goal node
        int currentIndex; // Index of the current node
        int otherIndex; // Index of a second node
        bool goalFound = false; // Has the goal been found? 
        goalIndex = index(goalX, goalY); // Set the goal index
        currentIndex = index(currentX, currentY); // Set the index of the current node 
        ppath.push_front(currentIndex); // Add the current index to a partial path
        pathTree.push_front(ppath); // Add that partial path to the list of partial paths 
        while (!goalFound) // Until we find the goal node
        {
                // Loop through the partial paths
                for(ptIter=pathTree.begin();ptIter != pathTree.end(); ++ptIter)
                {
                        ppath = *ptIter; // Take the next partial path
                        currentIndex = ppath.back(); // Find the index of the current node, the last node in the curretn partial path 
                        // Look in the adjacency matrix for connections to other nodes
                        for(otherIndex = 0; otherIndex < nNodes; otherIndex++)
                        {
                                // Check for a connection
                                if(adjacencyMatrix[currentIndex][otherIndex])
                                {
                                        // If so make a new partial path which is equal to the current path
                                        list<int> ppathTmp = ppath;
                                        // Add the index of the new node to the end of the path
                                        ppathTmp.push_back(otherIndex);
                                        // Add this new partial path to the list of partial paths
                                        pathTreeTmp.push_back(ppathTmp);
                                        // If the new node is the goal node
                                        if(otherIndex == goalIndex)
                                        {
                                                goalFound = true; // Set goal found to true
                                                cout << pathTreetmp ;
                                                path = ppathTmp;  // Set the path which will be return to the current partial path
                                        }
                                }
                        }
                } 
                pathTree = pathTreeTmp; // Set the list of paths to the new list
                pathTreeTmp.erase(pathTreeTmp.begin(),pathTreeTmp.end()); // Delete everything in the new list so it's ready to be filled again
        } 
 
 
        return path; // Return the path - starts with th current node ends in the goal node
}
[+][-]11/02/09 06:18 PM, ID: 25725611Accepted Solution

View this solution now by starting your 30-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

About this solution

Zones: C++ Programming Language, Game Programming, Programming Languages
Tags: Path Finding C++ Adjacency Matrix
Sign Up Now!
Solution Provided By: trinitrotoluene
Participating Experts: 3
Solution Grade: A
 
[+][-]11/02/09 02:27 AM, ID: 25718262Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]11/02/09 04:00 AM, ID: 25718635Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]11/02/09 08:47 AM, ID: 25721019Assisted Solution

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 30-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]11/02/09 08:49 AM, ID: 25721037Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]11/02/09 10:11 AM, ID: 25721881Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20091111-EE-VQP-92 - Hierarchy / EE_QW_3_20080625