Beispiel #1
0
    public void Pathfind_CantFind()
    {
        AStarWithMinBinary aStar = new AStarWithMinBinary(7, 7);

        Vector2 startPosition = new Vector2(0, 0);
        Vector2 endPosition   = new Vector2(6, 6);

        aStar.SetObstacle(new Vector2(0, 1));
        aStar.SetObstacle(new Vector2(1, 1));
        aStar.SetObstacle(new Vector2(2, 1));
        aStar.SetObstacle(new Vector2(4, 1));
        aStar.SetObstacle(new Vector2(4, 2));
        aStar.SetObstacle(new Vector2(4, 3));
        aStar.SetObstacle(new Vector2(5, 3));
        aStar.SetObstacle(new Vector2(6, 3));
        aStar.SetObstacle(new Vector2(1, 4));
        aStar.SetObstacle(new Vector2(2, 4));
        aStar.SetObstacle(new Vector2(3, 4));
        aStar.SetObstacle(new Vector2(2, 5));
        aStar.SetObstacle(new Vector2(0, 6));
        aStar.SetObstacle(new Vector2(1, 6));

        aStar.FindPath(startPosition, endPosition);

        /*
         *  N   N   0   0   0   0   E
         *
         *  ↓   ←   N   0   0   0   0
         *
         *  ↓   N   N   N   0   0   0
         *
         *  →   →   →   ↓   N   N   N
         *
         *  →   →   →   ↓   N   ↓   ←
         *
         *  N   N   N   ↓   N   ↓   ←
         *
         *  S   ←   ←   ←   ←   ←   ←
         */
        AStarWithMinBinaryHeapNode[,] matrix    = aStar.matrix;
        AStarWithMinBinaryHeapNode[,] nextNodes = new AStarWithMinBinaryHeapNode[7, 7]
        {
            { null, matrix[0, 0], matrix[1, 0], matrix[2, 0], matrix[3, 0], matrix[4, 0], matrix[5, 0] },
            { null, null, null, matrix[3, 0], null, matrix[5, 0], matrix[5, 1] },
            { matrix[1, 2], matrix[2, 2], matrix[3, 2], matrix[3, 1], null, matrix[5, 1], matrix[5, 2] },
            { matrix[1, 3], matrix[2, 3], matrix[3, 3], matrix[3, 2], null, null, null },
            { matrix[0, 3], null, null, null, null, null, null },
            { matrix[0, 4], matrix[0, 5], null, null, null, null, null },
            { null, null, null, null, null, null, null }
        };
        for (int x = 0; x < 7; x++)
        {
            for (int y = 0; y < 7; y++)
            {
                Assert.AreEqual(nextNodes[y, x], aStar.matrix[x, y].nextNode);      //横y竖x,何等反人类,而且竟然严格符合了数组下标顺序:前面是第一层,后面是第二层
            }
        }
    }
Beispiel #2
0
    public void Pathfind_StartAndEndOutOfMatrix()
    {
        AStarWithMinBinary aStar = new AStarWithMinBinary(7, 7);

        Vector2 startPosition = new Vector2(0, 7);
        Vector2 endPosition   = new Vector2(-1, 6);

        aStar.FindPath(startPosition, endPosition);

        foreach (AStarWithMinBinaryHeapNode node in aStar.matrix)
        {
            Assert.AreEqual(null, node.nextNode);
        }
    }
Beispiel #3
0
    public void Pathfind_EndIsObstacle()
    {
        AStarWithMinBinary aStar = new AStarWithMinBinary(7, 7);

        Vector2 startPosition = new Vector2(0, 0);
        Vector2 endPosition   = new Vector2(6, 6);

        aStar.SetObstacle(new Vector2(6, 6));

        aStar.FindPath(startPosition, endPosition);

        foreach (AStarWithMinBinaryHeapNode node in aStar.matrix)
        {
            Assert.AreEqual(null, node.nextNode);
        }
    }
Beispiel #4
0
    public void Pathfind_CanFind_NoObstacle()
    {
        AStarWithMinBinary aStar = new AStarWithMinBinary(7, 7);

        Vector2 startPosition = new Vector2(0, 0);
        Vector2 endPosition   = new Vector2(6, 6);

        aStar.FindPath(startPosition, endPosition);

        /*
         *  ↓   ←   ←   ←   ←   ←   E
         *
         *  ↓   ←   ↑   ↑   ↑   ↑   0
         *
         *  ↓   ←   0   0   0   0   0
         *
         *  ↓   ←   0   0   0   0   0
         *
         *  ↓   ←   0   0   0   0   0
         *
         *  ↓   ←   0   0   0   0   0
         *
         *  S   ←   0   0   0   0   0
         */
        AStarWithMinBinaryHeapNode[,] matrix    = aStar.matrix;
        AStarWithMinBinaryHeapNode[,] nextNodes = new AStarWithMinBinaryHeapNode[7, 7]
        {
            { null, matrix[0, 0], null, null, null, null, null },
            { matrix[0, 0], matrix[0, 1], null, null, null, null, null },
            { matrix[0, 1], matrix[0, 2], null, null, null, null, null },
            { matrix[0, 2], matrix[0, 3], null, null, null, null, null },
            { matrix[0, 3], matrix[0, 4], null, null, null, null, null },
            { matrix[0, 4], matrix[0, 5], matrix[2, 6], matrix[3, 6], matrix[4, 6], matrix[5, 6], null },
            { matrix[0, 5], matrix[0, 6], matrix[1, 6], matrix[2, 6], matrix[3, 6], matrix[4, 6], matrix[5, 6] }
        };
        for (int x = 0; x < 7; x++)
        {
            for (int y = 0; y < 7; y++)
            {
                Assert.AreEqual(nextNodes[y, x], aStar.matrix[x, y].nextNode);      //二维数组赋值是按照横竖x的方式进行的,也就是说上面的矩阵写错了,要转置,这就是一个用 [y, x] 一个用 [x, y] 的原因
            }
        }
    }
    void DoFindPath()
    {
        _aStar.FindPath(_startPosition, _endPosition);

        DisplayPathfinding();
    }