Ejemplo n.º 1
0
    public void CalNodeData(Dictionary <string, ANode> nodeDict)
    {
        ANode startNode = null;

        nodeDict.TryGetValue(xStart.ToString() + yStart, out startNode);
        if (startNode == null || !startNode.CanReached())
        {
            Debug.Log("起点不可达!");
        }


        ANode endNode = null;

        nodeDict.TryGetValue(xEnd.ToString() + yEnd, out endNode);
        if (endNode == null || !endNode.CanReached())
        {
            Debug.Log("终点不可达!");
        }

        List <ANode>     openList   = new List <ANode>();
        List <ANode>     closedList = new List <ANode>();
        HashSet <string> hasOpenSet = new HashSet <string>();
        HashSet <string> closedSet  = new HashSet <string>();

        bool reached = false;

        if (startNode.IsMatched(endNode))
        {
            Debug.Log("已经处于改点!!");
            return;
        }

        closedList.Add(startNode);
        closedSet.Add(startNode.x.ToString() + startNode.y);

        openList.AddRange(startNode.CalClosedNode(nodeDict, closedList));
        if (openList == null || openList.Count <= 0)
        {
            return;
        }

        foreach (var node in openList)
        {
            node.UpdateAllData(startNode, endNode);
            hasOpenSet.Add(node.x.ToString() + node.y);
        }

        ANode nowNode  = startNode;
        ANode lastNode = null;

        List <ANode> tempList = new List <ANode>();
        StreamWriter sw       = new StreamWriter("F:/path.txt", false, System.Text.Encoding.Default);

        while (true)
        {
            int minFValue = 0;
            nowNode = null;
            foreach (var node in openList)
            {
                //Debug.Log("H value: " + node.H);
                if (minFValue == 0 || node.F < minFValue)
                {
                    minFValue = node.F;
                    nowNode   = node;
                }
                else if (node.F == minFValue)
                {
                    if (node.H <= nowNode.H)
                    {
                        nowNode = node;
                    }
                }
            }


            if (nowNode.IsMatched(endNode))
            {
                lastNode = nowNode;
                sw.WriteLine("我终于到达啦!!" + " x: " + nowNode.x + " y: " + nowNode.y);
                Debug.Log("我终于到达啦!!" + " x: " + nowNode.x + " y: " + nowNode.y);
                break;
            }

            if (!closedSet.Contains(nowNode.x.ToString() + nowNode.y))
            {
                closedList.Add(nowNode);
                closedSet.Add(nowNode.x.ToString() + nowNode.y);
            }

            sw.WriteLine("x: " + nowNode.x + " y: " + nowNode.y);
            Debug.Log("x: " + nowNode.x + " y: " + nowNode.y);


            var nowClosedList = nowNode.CalClosedNode(nodeDict, closedList);

            foreach (var nowClosedNode in nowClosedList)
            {
                string nodeName = nowClosedNode.x.ToString() + nowClosedNode.y;
                if (!hasOpenSet.Contains(nodeName) && !closedSet.Contains(nodeName))
                {
                    nowClosedNode.UpdateAllData(startNode, endNode);
                    hasOpenSet.Add(nodeName);
                    openList.Add(nowClosedNode);
                }
            }

            openList.Remove(nowNode);

            if (openList.Count == 0)
            {
                Debug.Log("我真的找不到路了!!");
                reached = true;
            }
        }

        sw.Close();

        List <ANode> trackNodeList = new List <ANode>();
        ANode        tempNode      = null;

        while (lastNode != null)
        {
            trackNodeList.Add(lastNode);
            tempNode = lastNode.parentNode;
            lastNode = tempNode;
        }

        for (int i = 0; i < trackNodeList.Count; i++)
        {
            SetGreenColor("square" + "(" + trackNodeList[i].x + "," + trackNodeList[i].y + ")");
        }
    }