Search code examples
algorithmpseudocodepath-findinga-star

A* Pathfinding - I think I have it down in pseudocode, need verification


They say that taking what someone said, and repeating it back is the best form of verifying the basics of a concept (comprehension). So, I've been reading about A* and translated it into pseudocode, and I think I got it. Can anyone verify and/or give tips on whether or not this implementation would work?

Sorry if this is rushed, I'm on break at work ;)

openList.ClearTiles
closeList.clearTiles
path.clearTiles

openList.Add startTile

While openList.Count > 0 and PathFound = false
    activeTile = openList.GetTileWithLowestPathCost
    openList.remove activeTile
    closeList.add activeTile

    if targetTile.equals(activeTile)
       pathFound = true
    else
       for each activeTile.neighbors as nTile
           if nTile not in openList and not in closeList and IsMovable
              nTile.parent = activeTile
              nTile.hueristic = computeHeuristic
              nTile.movementCost = computeMovementCost
              nTile.pathCost = nTile.hueristic + nTile.movementCost

              openList.add nTile
           elseif isMovable = false
              closelist.add nTile
           endif
       next
    endif
endwhile

if pathFound = true        
   while activeTile.parent is not nothing
       path.insertAtZero activeTile
       activeTile = activeTile.parent
   endwhile
endif

Solution

  • Well one major issue with your pseudo code and one minor issue.

    The major issue:
    When you find nTile, it might be in closed or in open already, and in your implementation - you ignore it and skip it. However, if it is in closed or open, you should check maybe the path cost you just found is better then the path cost which is in closed or open, and if it is, remove it from closed/open and re-insert it to open with the new path cost.

    The minor issue:
    Sometimes there is more then one target node [there is a set of target nodes, you are looking for a path to one of them]. So instead of if targetTile.equals(activeTile) you can check if heuristicValue(activeTile) == 0 [assuming the heuristic function is admissible] or check if activeTile is in targetStates.