コード例 #1
0
    public List <Spot> FindPath(Spot startSpot, Spot endSpot)
    {
        List <Spot>    openSet   = new List <Spot>();
        HashSet <Spot> closedSet = new HashSet <Spot>();

        openSet.Add(startSpot);

        while (openSet.Count > 0)
        {
            Spot currentSpot = openSet[0];
            for (int i = 1; i < openSet.Count; i++)
            {
                if (openSet[i].GetFCost() < currentSpot.GetFCost() || (openSet[i].GetFCost() == currentSpot.GetFCost() && openSet[i].hCost < currentSpot.hCost))
                {
                    currentSpot = openSet[i];
                }
            }

            openSet.Remove(currentSpot);
            closedSet.Add(currentSpot);

            if (currentSpot == endSpot)
            {
                return(RetracePath(startSpot, endSpot));
            }

            foreach (Spot neighborSpot in currentSpot.GetNeighbors())
            {
                if (closedSet.Contains(neighborSpot))
                {
                    continue;
                }

                float newMovementCostToNeighbor = currentSpot.gCost + Spots.GetDistance(currentSpot, neighborSpot);
                if (newMovementCostToNeighbor < neighborSpot.gCost || !openSet.Contains(neighborSpot))
                {
                    neighborSpot.gCost      = newMovementCostToNeighbor;
                    neighborSpot.hCost      = Spots.GetDistance(neighborSpot, endSpot);
                    neighborSpot.parentSpot = currentSpot;

                    if (!openSet.Contains(neighborSpot))
                    {
                        openSet.Add(neighborSpot);
                    }
                }
            }
        }

        return(null);
    }