Exemple #1
0
    public IEnumerator DFSsolve()
    {
        Stack <tile> waiting = new Stack <tile>();

        waiting.Push(start);
        visited.Add(start);
        bool found = false;

        while (waiting.Count != 0)
        {
            yield return(new WaitForSeconds(0.1f));

            tile cur = waiting.Pop();
            if (!cur.isStart() && !cur.isGoals())
            {
                cur.spriteRenderer.color = Color.cyan;
            }

            if (cur.isGoals())
            {
                found = true;
                break;
            }

            List <tile> neighbors = field.GetNeighbor(cur);

            foreach (tile t in neighbors)
            {
                if (!visited.Contains(t) && (t.isWalkable() || t.isGoals()))
                {
                    waiting.Push(t);
                    t.prev = cur;
                }
                visited.Add(cur);
            }
        }

        if (found)
        {
            tile c = end;
            while (c.prev != null)
            {
                c = c.prev;
                if (!c.isGoals() && !c.isStart())
                {
                    c.spriteRenderer.color = Color.green;
                }
            }
            //c.spriteRenderer.color = Color.green;
        }
        else
        {
            Debug.Log("solution not found");
        }
    }
Exemple #2
0
    void Update()
    {
        if (Input.GetMouseButton(0))
        {
            if (EventSystem.current.IsPointerOverGameObject())
            {
                return;
            }

            Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
            tile    curTile  = GetTile(Mathf.RoundToInt(mousePos.x), Mathf.RoundToInt(mousePos.y));
            if (curTile != null)
            {
                // Debug.Log(curTile.getPos());

                SpriteRenderer sprt = curTile.obj.GetComponent <SpriteRenderer>();


                curTile.setState(state);

                if (curTile.isGoals())
                {
                    end = curTile;
                    curTile.spriteRenderer.color = Color.green;
                }
                else if (curTile.isStart())
                {
                    start = curTile;
                    curTile.spriteRenderer.color = Color.red;
                }
                else if (curTile.isWalkable())
                {
                    curTile.spriteRenderer.color = Color.white;
                }
                else
                {
                    curTile.spriteRenderer.color = Color.black;
                }

                Debug.Log(state);
            }
        }

        if (Input.GetMouseButton(2))
        {
            Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
            tile    curTile  = GetTile(Mathf.RoundToInt(mousePos.x), Mathf.RoundToInt(mousePos.y));
            if (curTile != null)
            {
                Debug.Log(curTile.state);
                Debug.Log(curTile.isGoals());
            }
        }

        if (SolveType)
        {
            TypeText.text = "Depth First Search";
        }
        else
        {
            TypeText.text = "Breath First Search";
        }
    }