C# 将节点添加到成本排序字典时,*寻路AI处于冻结状态

C# 将节点添加到成本排序字典时,*寻路AI处于冻结状态,c#,dictionary,artificial-intelligence,a-star,C#,Dictionary,Artificial Intelligence,A Star,我正试图在一个简单的四向移动网格上构建一个基于星形寻路的人工智能,但是每当我试图将节点添加到比较字典并对其进行排序时,它都会在dictionary.add上崩溃。我不完全确定为什么在这一点上,有人能发现缺陷吗 void DirectionFinder() { // Find Target Point // GetBoardPosition(); BuildOrReBuildPawnRefBoard(); bool IsTrue = false; i

我正试图在一个简单的四向移动网格上构建一个基于星形寻路的人工智能,但是每当我试图将节点添加到比较字典并对其进行排序时,它都会在dictionary.add上崩溃。我不完全确定为什么在这一点上,有人能发现缺陷吗

    void DirectionFinder()
{
    // Find Target Point //
    GetBoardPosition();
    BuildOrReBuildPawnRefBoard();
    bool IsTrue = false;
    int Serial = lastSquare.Location.Serial;
    // Get the Serial Keys of the nodes that are neighboring this node //
    bool TestNode = NodesClosed.ContainsKey(Serial);
    Dictionary<int, Node> SortArray = new Dictionary<int, Node>();
    SortArray.Clear();
    if (TestNode) 
    {
        Node ScanNode = NodesClosed[Serial];
        // Get the Number of Neighbors I have //
        int TestNumNeighbors = ScanNode.NeighborNodes.Count;
        // Set up a Loop that will go through these nodes and get the lowest-scored movement node //
        for (int loop = 0; loop < TestNumNeighbors; loop++)
        {
            int ScanNodePointX = ScanNode.NeighborNodes[loop].X;
            int ScanNodePointY = ScanNode.NeighborNodes[loop].Y;
            int ScanTestSerial = System_GameController.Instance.NodeArray[ScanNodePointX, ScanNodePointY].Location.Serial;
            bool IsNodeOpen = NodesOpen.ContainsKey(ScanTestSerial);
            if (IsNodeOpen)
            {
                Debug.Log("This Node is Open:" + ScanTestSerial);
                Node CompareNode = NodesOpen[ScanTestSerial];
                int CostOfNode = CompareNode.TotalCost;
                SortArray.Add(CostOfNode, CompareNode);
            }
        }
    }
}
void DirectionFinder()
{
//找到目标点//
GetBoardPosition();
BuildOrReBuildPawnRefBoard();
布尔IsTrue=假;
int Serial=lastSquare.Location.Serial;
//获取与此节点相邻的节点的串行密钥//
bool TestNode=NodesClosed.ContainsKey(串行);
字典SortArray=新字典();
SortArray.Clear();
if(TestNode)
{
节点扫描节点=节点关闭[串行];
//我有多少邻居//
int TestNumNeighbors=ScanNode.nextornodes.Count;
//设置一个循环,通过这些节点,获得得分最低的移动节点//
for(int-loop=0;loop
它一直延伸到“CostOfNode”。行得通。。。。然后,当它试图添加到Sortaray时,它就会中断

它正在破坏字典。添加。我不完全确定为什么在这一点上,有人能发现缺陷吗

    void DirectionFinder()
{
    // Find Target Point //
    GetBoardPosition();
    BuildOrReBuildPawnRefBoard();
    bool IsTrue = false;
    int Serial = lastSquare.Location.Serial;
    // Get the Serial Keys of the nodes that are neighboring this node //
    bool TestNode = NodesClosed.ContainsKey(Serial);
    Dictionary<int, Node> SortArray = new Dictionary<int, Node>();
    SortArray.Clear();
    if (TestNode) 
    {
        Node ScanNode = NodesClosed[Serial];
        // Get the Number of Neighbors I have //
        int TestNumNeighbors = ScanNode.NeighborNodes.Count;
        // Set up a Loop that will go through these nodes and get the lowest-scored movement node //
        for (int loop = 0; loop < TestNumNeighbors; loop++)
        {
            int ScanNodePointX = ScanNode.NeighborNodes[loop].X;
            int ScanNodePointY = ScanNode.NeighborNodes[loop].Y;
            int ScanTestSerial = System_GameController.Instance.NodeArray[ScanNodePointX, ScanNodePointY].Location.Serial;
            bool IsNodeOpen = NodesOpen.ContainsKey(ScanTestSerial);
            if (IsNodeOpen)
            {
                Debug.Log("This Node is Open:" + ScanTestSerial);
                Node CompareNode = NodesOpen[ScanTestSerial];
                int CostOfNode = CompareNode.TotalCost;
                SortArray.Add(CostOfNode, CompareNode);
            }
        }
    }
}
如果要添加到字典中的两个不同节点的
CostOfNode
相同,则第二个节点会导致崩溃

见:

ArgumentException

字典中已存在具有相同键的元素


看看埃里克·利珀特(Eric Lippert)的C#A明星吧:这实际上可以解释这一点。解决这个问题的最佳方法是什么?我“认为”我可以通过使用我的节点序列(因为它们都不同)作为键来避免这种情况,并以成本作为值对值进行排序。我也可以删除一个键,如果它们是相同的。。。