public void Pathfind_StartAndEndOutOfMatrix()
    {
        AStarEightDirections aStar = new AStarEightDirections(7, 7);

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

        aStar.FindPath(startPosition, endPosition);

        foreach (AStarEightDirectionsHeapNode node in aStar.matrix)
        {
            Assert.AreEqual(null, node.nextNode);
        }
    }
    public void Pathfind_EndIsObstacle()
    {
        AStarEightDirections aStar = new AStarEightDirections(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 (AStarEightDirectionsHeapNode node in aStar.matrix)
        {
            Assert.AreEqual(null, node.nextNode);
        }
    }
    public void Pathfind_CanFind_NoObstacle()
    {
        AStarEightDirections aStar = new AStarEightDirections(7, 7);

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

        aStar.FindPath(startPosition, endPosition);

        /*
         *  0   0   0   0   ↘   ↓   E
         *
         *  0   0   0   ↘   ↓   ↙   ←
         *
         *  0   0   ↘   ↓   ↙   ←   ↖
         *
         *  0   ↘   ↓   ↙   ←   ↖   0
         *
         *  ↘   ↓   ↙   ←   ↖   0   0
         *
         *  ↓   ↙   ←   ↖   0   0   0
         *
         *  S   ←   ↖   0   0   0   0
         */
        AStarEightDirectionsHeapNode[,] matrix    = aStar.matrix;
        AStarEightDirectionsHeapNode[,] nextNodes = new AStarEightDirectionsHeapNode[7, 7]
        {
            { null, matrix[0, 0], matrix[1, 1], null, null, null, null },
            { matrix[0, 0], matrix[0, 0], matrix[1, 1], matrix[2, 2], null, null, null },
            { matrix[1, 1], matrix[1, 1], matrix[1, 1], matrix[2, 2], matrix[3, 3], null, null },
            { null, matrix[2, 2], matrix[2, 2], matrix[2, 2], matrix[3, 3], matrix[4, 4], null },
            { null, null, matrix[3, 3], matrix[3, 3], matrix[3, 3], matrix[4, 4], matrix[5, 5] },
            { null, null, null, matrix[4, 4], matrix[4, 4], matrix[4, 4], matrix[5, 5] },
            { null, null, null, null, matrix[5, 5], matrix[5, 5], matrix[5, 5] }
        };
        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] 的原因
            }
        }
    }
Пример #4
0
    void DoFindPath()
    {
        _aStar.FindPath(_startPosition, _endPosition);

        DisplayPathfinding();
    }