Пример #1
0
        public void CustomMaze1()
        {
            // Arrange
            var maze = new List <string>()
            {
                "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
                "A.....x........x..............x",
                "x..x..x..xxxx..x..xxxxxxx..xxxx",
                "x..x..x.....x..x.....x........x",
                "x..x..xxxxxxx..xxxxxxx..xxxx..x",
                "x..x..x.....x..x.....x.....x..x",
                "x..x..x..x..x..x..x..xxxxxxx..x",
                "x..x..x..x..x.....x...........x",
                "x..x..x..x..x..xxxxxxxxxx..xxxx",
                "x..x..x..x.....x..............x",
                "x..x..x..xxxxxxxxxxxxxxxx..x..x",
                "x..x.....x........x..x.....x..x",
                "x..xxxxxxx..xxxx..x..x..xxxx..x",
                "x.....x.....x.....x.....x.....x",
                "xxxx..xxxx..xxxxxxxxxxxxx..x..x",
                "x..x.....x........x..x.....x..x",
                "x..xxxx..xxxxxxx..x..x..xxxx..x",
                "x..x.....x........x.....x.....x",
                "x..x..x..x..xxxxxxxxxxxxx..xxxx",
                "x.....x.................x.....B",
                "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
            };
            var expected = "Maze Directions from Start(A) to End(B): EEEESSSSSSSSSSEEEENNNNNNEESSSSEEEENNEEENNEESSEEEEEESSEEESSSSSSSSWWSSEEEE";

            // Assert
            var actual = AStarAlgorithm.AStarSearch(maze);

            Assert.AreEqual(expected, actual);
        }
Пример #2
0
        public void EmptyMaze()
        {
            // Arrange
            var maze     = new List <string>();
            var expected = "\nThe maze provided is empty, please input a valid Maze.\n";

            // Assert
            var actual = AStarAlgorithm.AStarSearch(maze);

            Assert.AreEqual(expected, actual);
        }
Пример #3
0
        public void MazeNoEndPointMaze()
        {
            // Arrange
            var maze = new List <string>()
            {
                "xxxxxxx",
                "xA....x",
                "xxxxxxx"
            };
            var expected = "\nThe maze provided is not valid, please input a valid Maze.\n";

            // Assert
            var actual = AStarAlgorithm.AStarSearch(maze);

            Assert.AreEqual(expected, actual);
        }
Пример #4
0
        public void ImpossibleMaze()
        {
            // Arrange
            var maze = new List <string>()
            {
                "xxxxxxx",
                "xA.x.Bx",
                "xxxxxxx"
            };
            var expected = "\nThis maze cannot be escaped from...\n";

            // Assert
            var actual = AStarAlgorithm.AStarSearch(maze);

            Assert.AreEqual(expected, actual);
        }
Пример #5
0
    public void Navigate(ShipRuntime targetShip, Vector2Int globalStartPos, Vector2Int globalEndPos)
    {
        Init();
        this.targetShip = targetShip;

        if (globalStartPos == globalEndPos)
        {
            StartCoroutine("DelaySearch");
            return;
        }

        nextIndex = 1;

        CellTemplate startCell = targetShip.GetCellByGlobalPos(globalStartPos);
        CellTemplate endCell   = targetShip.GetCellByGlobalPos(globalEndPos);

        // Invalid path supplied
        if (startCell == null || endCell == null || startCell.CellState == 0 || endCell.CellState == 0)
        {
            return;
        }

        ShipPiece startPiece = targetShip.GetPieceByGlobalCellPos(globalStartPos);
        ShipPiece endPiece   = targetShip.GetPieceByGlobalCellPos(globalEndPos);


        NavGrid startGrid = new NavGrid(startPiece);

        startGrid.Generate();

        NavGrid goalGrid = startGrid;

        if (startPiece != endPiece)
        {
            goalGrid = new NavGrid(endPiece);
            goalGrid.Generate();
        }

        AStarAlgorithm aStarAlgorithm = new AStarAlgorithm(startGrid, goalGrid, globalStartPos, globalEndPos);

        int newTTmp = 0;

        curPath = aStarAlgorithm.AStarSearch(ref newTTmp);

        wiggleTimer = Random.Range(0, 6.28318f);
    }
    public bool Navigate(ShipRuntime targetShip, Vector2Int globalStartPos, Vector2Int globalEndPos)
    {
        if (globalStartPos == globalEndPos)
        {
            return(false);
        }

        NavCell[] overrideStart = null;

        // Cur, prev and T evaluation
        if (curPath != null)
        {
            if (nextIndex < curPath.Length)
            {
                overrideStart = new NavCell[2];

                // Store C0 and C1
                overrideStart[0] = curPath[nextIndex - 1];
                overrideStart[1] = curPath[nextIndex];
            }
        }

        nextIndex = 1;

        CellTemplate startCell = targetShip.GetCellByGlobalPos(globalStartPos);
        CellTemplate endCell   = targetShip.GetCellByGlobalPos(globalEndPos);

        // Invalid path supplied
        if (startCell == null || endCell == null || startCell.CellState == 0 || endCell.CellState == 0)
        {
            return(false);
        }

        ShipPiece startPiece = targetShip.GetPieceByGlobalCellPos(globalStartPos);
        ShipPiece endPiece   = targetShip.GetPieceByGlobalCellPos(globalEndPos);


        NavGrid startGrid = new NavGrid(startPiece);

        startGrid.Generate();

        NavGrid goalGrid = startGrid;

        if (startPiece != endPiece)
        {
            goalGrid = new NavGrid(endPiece);
            goalGrid.Generate();
        }

        AStarAlgorithm aStarAlgorithm = new AStarAlgorithm(startGrid, goalGrid, globalStartPos, globalEndPos);

        int newTState = -1;

        curPath = aStarAlgorithm.AStarSearch(ref newTState, overrideStart);

        // Either reset, invert or leave move timer
        switch (newTState)
        {
        case -1:
            moveTimer = 0;
            break;

        case 1:
            moveTimer = 1 - moveTimer;
            break;
        }

        pathTracer.SetPositions(GetNavArray());

        return(curPath != null);
    }