#A* not working

5 messages · Page 1 of 1 (latest)

rocky yoke
#

doesnt get to the main return line ever.

public class AStarPathfinding {

    // Define a Tile class as in the previous example

    public static List<Tile> findPath(Tile[][] tiles, int startX, int startY, int endX, int endY) {

        Tile startTile = tiles[startX][startY];
        Tile endTile = tiles[endX][endY];


        PriorityQueue<Tile> openSet = new PriorityQueue<>();
        List<Tile> closedSet = new ArrayList<>();

        startTile.gScore = 0;

        openSet.add(startTile);


        while (!openSet.isEmpty()) {


            Tile currentTile = openSet.poll();


            if (currentTile == endTile) {
                List<Tile> path = new ArrayList<>();
                while (currentTile != null) {
                    path.add(currentTile);
                    currentTile = currentTile.parent;
                }
                Collections.reverse(path);
                return path;
            }


            closedSet.add(currentTile); ```
blissful ventureBOT
#

This post has been reserved for your question.

Hey @rocky yoke! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

rocky yoke
#

rest of code:

for (int x = currentTile.posx - 1; x <= currentTile.posx + 1; x++) {
                for (int y = currentTile.posy - 1; y <= currentTile.posy + 1; y++) {


                    if (x < 0 || x >= tiles.length || y < 0 || y >= tiles[0].length ||
                            (x == currentTile.posx && y == currentTile.posy)) {
                        continue;
                    }


                    Tile neighborTile = tiles[x][y];


                    if (neighborTile.occupied || closedSet.contains(neighborTile)) {
                        continue;
                    }


                    double tentativeGScore = currentTile.gScore + getDistance(currentTile, neighborTile);


                    if (!openSet.contains(neighborTile)) {
                        openSet.add(neighborTile);
                    }


                    else if (tentativeGScore >= neighborTile.gScore) {
                        continue;
                    }


                    neighborTile.parent = currentTile;
                    neighborTile.gScore = tentativeGScore;
                    neighborTile.fScore = neighborTile.gScore + getDistance(neighborTile, endTile);
                }
            }
        }

        return null;
    }

    private static double getDistance(Tile tile1, Tile tile2) {
        return Math.sqrt(Math.pow(tile1.posx - tile2.posx, 2) + Math.pow(tile1.posy - tile2.posy, 2));
    }
} ```
blissful ventureBOT
#

💤 Post marked as dormant

This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.