예제 #1
0
        public AStar(float[,] gridT , int startXT , int startYT , int targetXT , int targetYT)
            : base(gridT , startXT , startYT , targetXT , targetYT)
        {
            grid = gridT;
            startX = startXT;
            startY = startYT;
            targetX = targetXT;
            targetY = targetYT;

            if (((startX == targetX) && (startY == targetY)) == false)
            {

                bool found = false; // used to determine if path is found

                // creates the start node
                Node current = new Node();
                current.x = startX;
                current.y = startY;

                //creates the target node
                targetNode = new Node();
                targetNode.x = targetX;
                targetNode.y = targetY;

                //puts the start item in list
                open.Add(current);

                do
                {
                    // This call places the lowest f at the bottom

                    open.Sort(
                    delegate(Node x, Node y)
                    {
                        return x.f() - y.f();
                    });

                    // Get the node with the lowest TotalCost
                    current = open[0];

                    if (current.x == targetNode.x)
                    {
                        if (current.y == targetNode.y)
                        {
                            found = true;
                            break;
                        }
                    }

                    checkAdjacent(current.x, current.y, open, closed, current);

                    //remove a from the open list and move into the 'closed' list
                    open.Remove(current);
                    closed.Add(current);

                } while (open.Count > 0); // Keeps going until the open list is empty

                // if a path was found the path is worked out then returned
                if (found == true)
                {

                    foreach (Node n in closed)
                    {
                        PathNode pathNode = new PathNode();
                        pathNode.x = current.x;
                        pathNode.y = current.y;

                        pathNodes.Add(pathNode);

                        if( current.parent == null)
                        {
                            break;
                        }
                        current = current.parent;
                    }

                    /*
                    do
                    {
                        PathNode pathNode = new PathNode();
                        pathNode.x = current.x;
                        pathNode.y = current.y;

                        pathNodes.Add(pathNode);

                        if (current.parent.Equals(null))
                        {
                            // path.Add(new int[2] { current.x, current.y });
                            current = current.parent;
                        }
                        else
                        {
                            closed.Clear();
                        }

                    } while (closed.Count > 0);
                    */
                }

            }
        }
        private bool isNextNodeSafe(PathNode node)
        {
            if (realMap[node.x, node.y] <= 5.0)
            {
                return true;
            }

            if (realMap[node.x, node.y] == knownMap[node.x, node.y])
            {
                return true; // means no safer path
            }
            return false;
        }