Пример #1
0
    /// <summary>
    /// Creates a New Node.
    /// </summary>
    /// <returns>The nearby block.</returns>
    /// <param name="x">The x coordinate of new node.</param>
    /// <param name="y">The y coordinate of new node.</param>
    /// <param name="originalNode">The original node, which is a nearby node to this new node.</param>
    private BlNode CreateNewNode(float x, float y, BlNode originalNode)
    {
        BlNode t = new BlNode(new Vector3(x, y, 0));

        t.distanceScore = t.GetDistanceScore(dest);
        t.stepScore     = originalNode.stepScore + 1;

        return(t);
    }
Пример #2
0
    private bool _solved = false;                       // a flag indicating whether the problem is solved



    // Use this for initialization
    void Start()
    {
        // initializate the block information and gamObject arrays
        _mapInformation = new BlockType[NUMBER, NUMBER];
        _blockArray     = new GameObject[NUMBER, NUMBER];

        // initializate the priority queue
        pqueue = new PriorityQueue <BlNode>();

        initializeArray();

        for (int i = 0; i < NUMBER; i++)                        // initialize that all the surrounding
        {
            _mapInformation[0, i]          = BlockType.blockingblock;
            _mapInformation[NUMBER - 1, i] = BlockType.blockingblock;
            _mapInformation[i, 0]          = BlockType.blockingblock;
            _mapInformation[i, NUMBER - 1] = BlockType.blockingblock;
        }

        // relocate the cached camera
        myCamera.transform.position = new Vector3(NUMBER / 2, NUMBER / 2, -10);
        myCamera.orthographicSize   = NUMBER / 2;

        // show all blocks
        ShowBlocks();

        // initialize the BlNode for the source and saved into the priority queue
        BlNode st = new BlNode(sour);

        st.stepScore     = 0;
        st.distanceScore = st.GetDistanceScore(dest);
        pqueue.Enqueue(st);

        // start the solve coroutine (show details for each step)
        StartCoroutine(Solve());
    }
Пример #3
0
    /// <summary>
    /// Solve the map by A*.
    /// </summary>
    IEnumerator Solve()
    {
        while (!_solved)
        {
            BlNode cur = pqueue.Dequeue();

            if (cur == null)
            {
                break;
            }

            float _x = cur.pos.x;
            float _y = cur.pos.y;

            _mapInformation[(int)_x, (int)_y] = BlockType.visitedblock;


            if (_x + 1 < NUMBER && !isBlock(_x + 1, _y))                        // go one step right
            {
                if (_mapInformation[(int)(_x + 1), (int)_y] != BlockType.visitedblock)
                {
                    pqueue.Enqueue(CreateNewNode(_x + 1, _y, cur));
                    _mapInformation[(int)(_x + 1), (int)_y] = BlockType.pretendedblock;
                }
            }

            if (_x - 1 > 0 && !isBlock(_x - 1, _y))                                     // go one step left
            {
                if (_mapInformation[(int)(_x - 1), (int)_y] != BlockType.visitedblock)
                {
                    pqueue.Enqueue(CreateNewNode(_x - 1, _y, cur));
                    _mapInformation[(int)(_x - 1), (int)_y] = BlockType.pretendedblock;
                }
            }

            if (_y + 1 < NUMBER && !isBlock(_x, _y + 1))                        // go one step up
            {
                if (_mapInformation[(int)_x, (int)(_y + 1)] != BlockType.visitedblock)
                {
                    pqueue.Enqueue(CreateNewNode(_x, _y + 1, cur));
                    _mapInformation[(int)_x, (int)(_y + 1)] = BlockType.pretendedblock;
                }
            }

            if (_y - 1 > 0 && !isBlock(_x, _y - 1))                             // go one step down
            {
                if (_mapInformation[(int)_x, (int)(_y - 1)] != BlockType.visitedblock)
                {
                    pqueue.Enqueue(CreateNewNode(_x, _y - 1, cur));
                    _mapInformation[(int)_x, (int)(_y - 1)] = BlockType.pretendedblock;
                }
            }

            if (_mapInformation[(int)dest.x, (int)dest.y] == BlockType.visitedblock)                    //determine whether or not it reaches the destination
            {
                _solved = true;
            }

            _mapInformation[(int)sour.x, (int)sour.y] = BlockType.sourceblock;                                  // remark the source block for coloring

            ShowBlocks();

            //yield return new WaitForSeconds(waitSecond);
            yield return(null);
        }

        _mapInformation[(int)dest.x, (int)dest.y] = BlockType.destinationblock;
        _mapInformation[(int)sour.x, (int)sour.y] = BlockType.sourceblock;

        if (!_solved)
        {
            Debug.Log("Unsolvable");
        }

        ShowBlocks();
    }