Example #1
0
    private void Update()
    {
        if (DrawState.Instance.settings.value == 3)
        {
            if (Input.GetMouseButtonDown(0))
            {
                Vector3      mousePos   = Camera.main.ScreenToWorldPoint(Input.mousePosition);
                Vector2      mousePos2D = new Vector2(mousePos.x, mousePos.y);
                RaycastHit2D hit        = Physics2D.Raycast(mousePos2D, Vector2.zero);
                if (hit.collider != null && hit.collider.gameObject.tag == "State")
                {
                    foreach (var s in DrawState.Instance.States)
                    {
                        if (s.stateGameObject == hit.collider.gameObject)
                        {
                            startState     = s;
                            lineStartPoint = hit.collider.gameObject.transform.position;
                        }
                    }
                }

                else
                {
                    canDraw = false;
                }
            }

            else if (Input.GetMouseButtonUp(0))
            {
                Vector3      mousePos   = Camera.main.ScreenToWorldPoint(Input.mousePosition);
                Vector2      mousePos2D = new Vector2(mousePos.x, mousePos.y);
                RaycastHit2D hit        = Physics2D.Raycast(mousePos2D, Vector2.zero);
                if (hit.collider != null && hit.collider.gameObject.tag == "State")
                {
                    foreach (var s in DrawState.Instance.States)
                    {
                        if (s.stateGameObject == hit.collider.gameObject)
                        {
                            EndState     = s;
                            lineEndPoint = hit.collider.gameObject.transform.position;
                        }
                    }
                }

                else
                {
                    canDraw = false;
                }

                foreach (var rel in Relations)
                {
                    if (rel.startState == startState && rel.endState == EndState)
                    {
                        canDraw = false;
                        if (lineEndPoint != lineStartPoint && hit.collider.gameObject.tag != "MainCamera" &&
                            !inputValue.activeSelf && !rel.isRecursive && hit.collider.gameObject.tag != "Sector" &&
                            hit.collider.gameObject.tag != "Text" && hit.collider.gameObject.tag != "Text2" &&
                            hit.collider.gameObject.tag != "Arrow")
                        {
                            Vector3 pos = new Vector3(rel.Arrow.transform.Find("Text").transform.position.x + (valueDistance * rel.textPosMultiplier),
                                                      rel.Arrow.transform.Find("Text").transform.position.y,
                                                      rel.Arrow.transform.Find("Text").transform.position.z);
                            rel.textPosMultiplier++;
                            addedText      = Instantiate(Text, pos, Quaternion.identity);
                            addedText.name = "Text" + rel.textIndex;
                            rel.textIndex++;
                            rel.Arrow.GetComponent <FixRotation>().Texts.Add(addedText);
                            addedText.transform.SetParent(rel.Arrow.GetComponent <Transform>());
                            addedText.GetComponent <TextMeshPro>().fontSize = 2.65f;
                            TuringRelation r = new TuringRelation(rel.startState, rel.endState, rel.Arrow, rel.Line);
                            r.isAdded = true;
                            Relations.Add(r);
                            rel.startState.relatedOutLines.Add(r);
                            rel.endState.relatedInLines.Add(r);
                            Rel = r;
                            addInputValue.SetActive(true);
                        }
                        if (lineEndPoint != lineStartPoint && hit.collider.gameObject.tag != "MainCamera" &&
                            !inputValue.activeSelf && rel.isRecursive && hit.collider.gameObject.tag != "Sector" &&
                            hit.collider.gameObject.tag != "Text" && hit.collider.gameObject.tag != "Text2" &&
                            hit.collider.gameObject.tag != "Arrow")
                        {
                            Vector3 pos = new Vector3(rel.Arrow.transform.Find("Text2").transform.position.x + (valueDistance * rel.textPosMultiplier),
                                                      rel.Arrow.transform.Find("Text2").transform.position.y,
                                                      rel.Arrow.transform.Find("Text2").transform.position.z);
                            rel.textPosMultiplier++;
                            addedText      = Instantiate(Text2, pos, Quaternion.identity);
                            addedText.name = "Text" + rel.textIndex;
                            rel.textIndex++;
                            rel.Arrow.GetComponent <FixRotation>().Texts.Add(addedText);
                            addedText.transform.SetParent(rel.Arrow.GetComponent <Transform>());
                            addedText.GetComponent <TextMeshPro>().fontSize = 2.65f;
                            TuringRelation r = new TuringRelation(rel.startState, rel.endState, rel.Arrow, rel.Line);
                            r.value       = addedText.GetComponent <TextMeshPro>().text[0];
                            r.isAdded     = true;
                            r.isRecursive = true;
                            Relations.Add(r);
                            rel.startState.relatedOutLines.Add(r);
                            rel.endState.relatedInLines.Add(r);
                            Rel = r;
                            addInputValue.SetActive(true);
                        }
                    }
                    if (rel.startState == EndState && rel.endState == startState)
                    {
                        recursive = true;
                    }
                }

                if (lineEndPoint != lineStartPoint && hit.collider.gameObject.tag != "MainCamera" && !inputValue.activeSelf &&
                    !addInputValue.activeSelf && canDraw && !recursive && hit.collider.gameObject.tag != "Sector" &&
                    hit.collider.gameObject.tag != "Text" && hit.collider.gameObject.tag != "Text2" &&
                    hit.collider.gameObject.tag != "Arrow")
                {
                    Vector3 dir        = lineEndPoint - lineStartPoint;
                    Vector3 pos        = (lineEndPoint + lineStartPoint) / 2;
                    float   angle      = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
                    var     gameObject = new GameObject();
                    gameObject.name = "Line" + counter;
                    gameObject.tag  = "Line";
                    var lineRenderer = gameObject.AddComponent <LineRenderer>();
                    lineRenderer.material = lineMaterial;
                    lineRenderer.SetPositions(new Vector3[] { lineStartPoint, lineEndPoint });
                    lineRenderer.startWidth = lineWidth;
                    lineRenderer.endWidth   = lineWidth;
                    if (SceneManager.GetActiveScene().buildIndex == 1)
                    {
                        Arrow = Instantiate(arrow, pos, Quaternion.AngleAxis(angle + 90, Vector3.forward));
                        Arrow.transform.SetParent(lineRenderer.GetComponent <Transform>());
                        Relation rel = new Relation(startState, EndState, Arrow, lineRenderer);
                        rel.startPoint = lineStartPoint;
                        rel.endPoint   = lineEndPoint;
                        Relations.Add(rel);
                        Rel = rel;
                        foreach (var s in DrawState.Instance.States)
                        {
                            if (s.Name == startState.Name)
                            {
                                s.relatedOutLines.Add(rel);
                            }

                            else if (s.Name == EndState.Name)
                            {
                                s.relatedInLines.Add(rel);
                            }
                        }
                    }

                    else if (SceneManager.GetActiveScene().buildIndex == 2)
                    {
                        Arrow = Instantiate(turingArrow, pos, Quaternion.AngleAxis(angle + 90, Vector3.forward));
                        Arrow.transform.SetParent(lineRenderer.GetComponent <Transform>());
                        TuringRelation rel = new TuringRelation(startState, EndState, Arrow, lineRenderer);
                        rel.startPoint = lineStartPoint;
                        rel.endPoint   = lineEndPoint;
                        Relations.Add(rel);
                        Rel = rel;
                        foreach (var s in DrawState.Instance.States)
                        {
                            if (s.Name == startState.Name)
                            {
                                s.relatedOutLines.Add(rel);
                            }

                            else if (s.Name == EndState.Name)
                            {
                                s.relatedInLines.Add(rel);
                            }
                        }
                    }

                    inputValue.SetActive(true);
                    Lines.Add(lineRenderer.gameObject);
                    counter += 1;
                }

                if (lineEndPoint != lineStartPoint && hit.collider.gameObject.tag != "MainCamera" && !inputValue.activeSelf &&
                    !addInputValue.activeSelf && canDraw && recursive && hit.collider.gameObject.tag != "Sector" &&
                    hit.collider.gameObject.tag != "Text" && hit.collider.gameObject.tag != "Text2" &&
                    hit.collider.gameObject.tag != "Arrow")
                {
                    Vector3 dir = lineEndPoint - lineStartPoint;
                    Vector2 lineStartPoint2D  = lineStartPoint;
                    Vector2 lineEndPoint2D    = lineEndPoint;
                    Vector3 newLineStartPoint = lineStartPoint2D + (-(Vector2.Perpendicular(dir)).normalized * 0.4f);
                    Vector3 newLineEndPoint   = lineEndPoint2D + (-(Vector2.Perpendicular(dir)).normalized * 0.4f);
                    newLineStartPoint = new Vector3(newLineStartPoint.x, newLineStartPoint.y, -8.700012f);
                    newLineEndPoint   = new Vector3(newLineEndPoint.x, newLineEndPoint.y, -8.700012f);
                    Vector3 pos        = (newLineEndPoint + newLineStartPoint) / 2;
                    float   angle      = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
                    var     gameObject = new GameObject();
                    gameObject.name = "Line" + counter;
                    var lineRenderer = gameObject.AddComponent <LineRenderer>();
                    lineRenderer.material = lineMaterial;
                    lineRenderer.SetPositions(new Vector3[] { newLineStartPoint, newLineEndPoint });
                    lineRenderer.startWidth = lineWidth;
                    lineRenderer.endWidth   = lineWidth;
                    if (SceneManager.GetActiveScene().buildIndex == 1)
                    {
                        Arrow = Instantiate(arrow, pos, Quaternion.AngleAxis(angle + 90, Vector3.forward));
                        Arrow.transform.SetParent(lineRenderer.GetComponent <Transform>());
                        Relation rel = new Relation(startState, EndState, Arrow, lineRenderer);
                        rel.startPoint  = newLineStartPoint;
                        rel.endPoint    = newLineEndPoint;
                        rel.isRecursive = true;
                        Relations.Add(rel);
                        Rel = rel;
                        foreach (var s in DrawState.Instance.States)
                        {
                            if (s.Name == startState.Name)
                            {
                                s.relatedOutLines.Add(rel);
                            }
                            else if (s.Name == EndState.Name)
                            {
                                s.relatedInLines.Add(rel);
                            }
                        }
                    }

                    else if (SceneManager.GetActiveScene().buildIndex == 2)
                    {
                        Arrow = Instantiate(turingArrow, pos, Quaternion.AngleAxis(angle + 90, Vector3.forward));
                        Arrow.transform.SetParent(lineRenderer.GetComponent <Transform>());
                        TuringRelation rel = new TuringRelation(startState, EndState, Arrow, lineRenderer);
                        rel.startPoint  = newLineStartPoint;
                        rel.endPoint    = newLineEndPoint;
                        rel.isRecursive = true;
                        Relations.Add(rel);
                        Rel = rel;
                        foreach (var s in DrawState.Instance.States)
                        {
                            if (s.Name == startState.Name)
                            {
                                s.relatedOutLines.Add(rel);
                            }
                            else if (s.Name == EndState.Name)
                            {
                                s.relatedInLines.Add(rel);
                            }
                        }
                    }

                    inputValue.SetActive(true);
                    Lines.Add(lineRenderer.gameObject);
                    foreach (var relation in Relations)
                    {
                        if (relation.endState == startState && relation.startState == EndState)
                        {
                            antiRecursiveRel = relation;
                        }
                    }

                    Vector3 _newLineStartPoint = lineEndPoint2D - (-(Vector2.Perpendicular(dir)).normalized * 0.4f);
                    Vector3 _newLineEndPoint   = lineStartPoint2D - (-(Vector2.Perpendicular(dir)).normalized * 0.4f);
                    _newLineStartPoint = new Vector3(_newLineStartPoint.x, _newLineStartPoint.y, -8.700012f);
                    _newLineEndPoint   = new Vector3(_newLineEndPoint.x, _newLineEndPoint.y, -8.700012f);
                    Vector3 _pos = (_newLineEndPoint + _newLineStartPoint) / 2;
                    antiRecursiveRel.Line.SetPositions(new Vector3[] { _newLineStartPoint, _newLineEndPoint });
                    antiRecursiveRel.Arrow.transform.position = _pos;
                    antiRecursiveRel.isRecursive = true;
                    counter += 1;
                }

                if (startState == EndState && hit.collider.gameObject.tag != "MainCamera" && !inputValue.activeSelf &&
                    !addInputValue.activeSelf && hit.collider.gameObject.tag != "Sector" &&
                    hit.collider.gameObject.tag != "Text" && hit.collider.gameObject.tag != "Text2" &&
                    hit.collider.gameObject.tag != "Arrow")
                {
                    if (!startState.stateGameObject.transform.Find("ReturnRel").GetComponent <SpriteRenderer>().enabled)
                    {
                        startState.stateGameObject.transform.Find("ReturnRel").GetComponent <SpriteRenderer>().enabled =
                            true;
                        Arrow = startState.stateGameObject;
                        TuringRelation rel = new TuringRelation(startState, startState);
                        rel.startPoint = startState.statePosition;
                        rel.endPoint   = startState.statePosition;
                        rel.isReturn   = true;
                        Relations.Add(rel);
                        Rel = rel;
                        foreach (var s in DrawState.Instance.States)
                        {
                            if (s.Name == EndState.Name)
                            {
                                s.relatedOutLines.Add(rel);
                            }
                        }

                        inputValue.SetActive(true);
                    }

                    else if (EndState.stateGameObject.transform.Find("ReturnRel").GetComponent <SpriteRenderer>().enabled)
                    {
                        TuringRelation rel = new TuringRelation(startState, startState);
                        Vector3        pos = new Vector3(startState.stateGameObject.transform.Find("Text").transform.position.x,
                                                         startState.stateGameObject.transform.Find("Text").transform.position.y + (0.4f * startState.textPosMultiplier),
                                                         startState.stateGameObject.transform.Find("Text").transform.position.z);
                        startState.textPosMultiplier++;
                        addedText      = Instantiate(Text, pos, Quaternion.identity);
                        addedText.name = "Text" + rel.textIndex;
                        rel.textIndex++;
                        addedText.transform.SetParent(startState.stateGameObject.GetComponent <Transform>());
                        addedText.GetComponent <TextMeshPro>().fontSize = 2.65f;
                        rel.startPoint = startState.statePosition;
                        rel.endPoint   = startState.statePosition;
                        rel.isReturn   = true;
                        Relations.Add(rel);
                        Rel = rel;
                        foreach (var s in DrawState.Instance.States)
                        {
                            if (s.Name == EndState.Name)
                            {
                                s.relatedOutLines.Add(rel);
                            }
                        }
                        addInputValue.SetActive(true);
                    }
                }
            }
        }
        canDraw   = true;
        recursive = false;
    }
Example #2
0
    public void Solve()
    {
        Steps.Clear();
        State          startState  = null;
        TuringRelation rel         = null;
        GameObject     startSector = Tape.Instance.startSector;

        foreach (var s in DrawState.Instance.States)
        {
            if (s.Name == "StartState")
            {
                startState = s;
            }
        }

        for (;;)
        {
            Steps.Add(startState);

            if (startState.relatedOutLines.Count == 0)
            {
                if (!startState.isEnd)
                {
                    Accepted = false;
                }
                break;
            }

            foreach (var r in startState.relatedOutLines)
            {
                if (r.value == startSector.transform.Find("Text").GetComponent <TextMeshPro>().text[0])
                {
                    rel        = (TuringRelation)r;
                    startState = rel.endState;
                    Rels.Add(rel);
                    truthCounter++;
                }
            }

            if (truthCounter == 0)
            {
                if (!startState.isEnd)
                {
                    Accepted = false;
                }
                break;
            }

            if (rel == null)
            {
                if (!startState.isEnd)
                {
                    Accepted = false;
                }
                break;
            }

            startSector.transform.Find("Text").GetComponent <TextMeshPro>().text = rel.valueToChange.ToString();

            if (rel.isRight)
            {
                startSector = Tape.Instance.Sectors[Tape.Instance.Sectors.IndexOf(startSector) + 1];
            }

            else
            {
                startSector = Tape.Instance.Sectors[Tape.Instance.Sectors.IndexOf(startSector) - 1];
            }

            truthCounter = 0;
        }
        Debug.Log(Accepted);

        if (Accepted)
        {
            rejectedText.gameObject.SetActive(false);
            acceptedText.gameObject.SetActive(true);
        }

        else
        {
            rejectedText.gameObject.SetActive(true);
            acceptedText.gameObject.SetActive(false);
        }

        Accepted = true;
        Tape.Instance.counter = 0;
    }
Example #3
0
    bool TuringSolve(string inputValue)
    {
        State          startState   = null;
        TuringRelation rel          = null;
        GameObject     startSector  = Tape.Instance.startSector;
        bool           Accepted     = true;
        int            truthCounter = 0;

        foreach (var s in DrawState.Instance.States)
        {
            if (s.Name == "StartState")
            {
                startState = s;
            }
        }

        for (int i = 0; i < inputValue.Length; i++)
        {
            Tape.Instance.Sectors[7 + i].transform.Find("Text").GetComponent <TextMeshPro>().text = inputValue[i].ToString();
        }

        for (;;)
        {
            if (startState.relatedOutLines.Count == 0)
            {
                if (!startState.isEnd)
                {
                    Accepted = false;
                }
                break;
            }

            foreach (var r in startState.relatedOutLines)
            {
                if (r.value == startSector.transform.Find("Text").GetComponent <TextMeshPro>().text[0])
                {
                    rel        = (TuringRelation)r;
                    startState = rel.endState;
                    truthCounter++;
                }
            }

            if (truthCounter == 0)
            {
                if (!startState.isEnd)
                {
                    Accepted = false;
                }
                break;
            }

            if (rel == null)
            {
                if (!startState.isEnd)
                {
                    Accepted = false;
                }
                break;
            }

            startSector.transform.Find("Text").GetComponent <TextMeshPro>().text = rel.valueToChange.ToString();

            if (rel.isRight)
            {
                startSector = Tape.Instance.Sectors[Tape.Instance.Sectors.IndexOf(startSector) + 1];
            }

            else
            {
                startSector = Tape.Instance.Sectors[Tape.Instance.Sectors.IndexOf(startSector) - 1];
            }
            truthCounter = 0;
        }

        return(Accepted);
    }