Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8147011
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T14:12:14+00:00 2026-06-06T14:12:14+00:00

Is there a better way to handle my FindAdjacent() function for my A Star

  • 0

Is there a better way to handle my FindAdjacent() function for my A Star algorithm? It’s awfully messy, and it doesn’t set the parent node correctly. When it tries to find the path, it loops infinitely because the parent of the node has a pent of the node and the parents are always each other.

Any help would be amazing. This is my function:

void AStarImpl::FindAdjacent(Node* pNode)
{
    for (int i = -1; i <= 1; i++)
    {
        for (int j = -1; j <= 1; j++)
        {
            if (pNode->mX != Map::GetInstance()->mMap[pNode->mX + i][pNode->mY + j].mX 
                || pNode->mY != Map::GetInstance()->mMap[pNode->mX + i][pNode->mY + j].mY)
            {
                if (pNode->mX + i <= 14 && pNode->mY + j <= 14)
                {
                    if (pNode->mX + i >= 0 && pNode->mY + j >= 0)
                    {
                        if (Map::GetInstance()->mMap[pNode->mX + i][pNode->mY + j].mTypeID != NODE_TYPE_SOLID)
                        {
                            if (find(mOpenList.begin(), mOpenList.end(), &Map::GetInstance()->mMap[pNode->mX + i][pNode->mY + j]) == mOpenList.end())
                            {

                                Map::GetInstance()->mMap[pNode->mX+i][pNode->mY+j].mParent = &Map::GetInstance()->mMap[pNode->mX][pNode->mY];
                                mOpenList.push_back(&Map::GetInstance()->mMap[pNode->mX+i][pNode->mY+j]);
                            }
                        }
                    }
                }

            }
        }
    }

    mClosedList.push_back(&Map::GetInstance()->mMap[pNode->mX][pNode->mY]);
}

If you’d like any more code, just ask and I can post it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T14:12:15+00:00Added an answer on June 6, 2026 at 2:12 pm

    You can reduce the amount of nested ifs by using continue. Generally speaking, the following two code blocks are equivalent:

    while(conditionA){
        if(conditionB){
            doStuff();
        }
    }
    
    while(conditionA){
        if (!conditionB){continue;}
        doStuff();
    }
    

    We can use this principle to reduce the amount of nested ifs in your code.

    void AStarImpl::FindAdjacent(Node* pNode)
    {
        for (int i = -1; i <= 1; i++)
        {
            for (int j = -1; j <= 1; j++)
            {
                if (pNode->mX == Map::GetInstance()->mMap[pNode->mX + i][pNode->mY + j].mX && pNode->mY == Map::GetInstance()->mMap[pNode->mX + i][pNode->mY + j].mY){continue;}
                if (pNode->mX + i > 14 || pNode->mY + j > 14){continue;}
                if (pNode->mX + i < 0 || pNode->mY + j < 0){continue;}
                if (Map::GetInstance()->mMap[pNode->mX + i][pNode->mY + j].mTypeID == NODE_TYPE_SOLID){continue;}
                if (find(mOpenList.begin(), mOpenList.end(), &Map::GetInstance()->mMap[pNode->mX + i][pNode->mY + j]) != mOpenList.end()){continue;}
                Map::GetInstance()->mMap[pNode->mX+i][pNode->mY+j].mParent = &Map::GetInstance()->mMap[pNode->mX][pNode->mY];
                mOpenList.push_back(&Map::GetInstance()->mMap[pNode->mX+i][pNode->mY+j]);
            }
        }
    
        mClosedList.push_back(&Map::GetInstance()->mMap[pNode->mX][pNode->mY]);
    }
    

    If I understand your first if condition correctly, you’re just trying to assert that pNode is not its own neighbor. In which case you can change the code to:

    void AStarImpl::FindAdjacent(Node* pNode)
    {
        for (int i = -1; i <= 1; i++)
        {
            for (int j = -1; j <= 1; j++)
            {
                if (i == 0 && j == 0){continue;}
                if (pNode->mX + i > 14 || pNode->mY + j > 14){continue;}
                if (pNode->mX + i < 0 || pNode->mY + j < 0){continue;}
                if (Map::GetInstance()->mMap[pNode->mX + i][pNode->mY + j].mTypeID == NODE_TYPE_SOLID){continue;}
                if (find(mOpenList.begin(), mOpenList.end(), &Map::GetInstance()->mMap[pNode->mX + i][pNode->mY + j]) != mOpenList.end()){continue;}
                Map::GetInstance()->mMap[pNode->mX+i][pNode->mY+j].mParent = &Map::GetInstance()->mMap[pNode->mX][pNode->mY];
                mOpenList.push_back(&Map::GetInstance()->mMap[pNode->mX+i][pNode->mY+j]);
            }
        }
    
        mClosedList.push_back(&Map::GetInstance()->mMap[pNode->mX][pNode->mY]);
    }
    

    Ideally, your FindAdjacent method should not have to modify the open or closed sets at all. instead, make it return all neighbors, regardless of whether they are open or closed. If you want to add these neighbors to the opened or closed set, or check that they are a member of those sets, that should be done in the method that actually implements the aStar algorithm.

    Vector<Node> AStarImpl:FindAdjacent(Node* pNode)
    {
        Vector<Node> neighbors;
        for (int i = -1; i <= 1; i++)
        {
            for (int j = -1; j <= 1; j++)
            {
                if (i == 0 && j == 0){continue;}
                if (pNode->mX + i > 14 || pNode->mY + j > 14){continue;}
                if (pNode->mX + i < 0 || pNode->mY + j < 0){continue;}
                if (Map::GetInstance()->mMap[pNode->mX + i][pNode->mY + j].mTypeID == NODE_TYPE_SOLID){continue;}
                neighbors.push_back(Map::GetInstance()->mMap[pNode->mX+i][pNode->mY+j]);
            }
        }
        return neighbors;
    }
    

    You perform some of the same operations multiple times. You can make your intentions more clear by storing the results of those operations in variables. Doing this won’t make your code any shorter, but it might make it more readable.

    Vector<Node> AStarImpl:FindAdjacent(Node* pNode)
    {
        Vector<Node> neighbors;
        for (int i = -1; i <= 1; i++)
        {
            for (int j = -1; j <= 1; j++)
            {
                if (i == 0 && j == 0){continue;}
                int x = pNode->mX + i;
                int y = pNode->mY + j;
                if (x > 14 || y > 14){continue;}
                if (x < 0 || y < 0){continue;}
                Node candidate = Map::GetInstance()->mMap[x][y];
                if (candidate.mTypeID == NODE_TYPE_SOLID){continue;}
                neighbors.push_back(candidate);
            }
        }
        return neighbors;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a better way in the new ASP.net MVC 4 WebApi to handle
Is there a better way to write the following function? Having the '#' +
Is there any better way to handle Exceptions using Play framework apart from what
Is there a better way to handle exceptions that occur inside an Action Filter
Is there a better way to handle form tab order in PHP than this?
Q: Is there a better way to handle SqlExceptions? The below examples rely on
Save me from raptor death - is there any better way to handle this
Using C#, is there a better way to handle multiple types of exceptions rather
Is there better way to delete a parameter from a query string in a
Is there better way to enumerate all photos on the device than this one?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.