Ejemplo n.º 1
0
    //parses the input string and updates game state if it is a valid assignment, else returns false
    public bool ParseAndUpdate(string input)
    {
        references = MasterController.instance.references;
        string name         = trimVariableNameFromString(ref input);
        bool   isAssignment = trimAssignmentFromString(ref input) == "=";

        //Assignment case (variable)
        if (isAssignment)
        {
            try
            {
                int evaluated = EvaluateString.Evaluate(input, references);
                if (!references.ContainsKey(name))
                {
                    GameObject newBlock = Instantiate(variable, Vector3.zero, Quaternion.identity);
                    newBlock.GetComponent <VariableController>().SetNameAndData(name, evaluated);
                    references.Add(name, newBlock);
                }
                else
                {
                    ((GameObject)references[name]).GetComponent <VariableController>().SetData(evaluated);
                }
            }
            catch (Exception e)
            {
                Debug.Log("Parse failed.");
                return(false);
            }
        }
        return(true);
    }
 private Boolean evaluate(String expression, int answer)
 {
     if (expression.Length != 3 || isOperator(expression, 0) || isOperator(expression, expression.Length - 1))
     {
         return(false);
     }
     else
     {
         try
         {
             return(((int)EvaluateString.Evaluate(expression)) == answer);
         }
         catch (Exception e)
         {
             return(false);
         }
     }
 }
Ejemplo n.º 3
0
    private Boolean evaluate(String expression, int answer)
    {
        if (expression.Length != 5 || isOperator(expression, 0) || isOperator(expression, expression.Length - 1))
        {
            return(false);
        }
        else
        {
            String expAnswer = "";
            for (int i = 0; i < expression.Length; i++)
            {
                if (PlayerPrefs.GetInt("Solve") == 0 && i == 0)
                {
                    expAnswer += "(";
                }

                if (PlayerPrefs.GetInt("Solve") == 0 && i == 3)
                {
                    expAnswer += ")";
                }

                if (PlayerPrefs.GetInt("Solve") == 1 && i == 2)
                {
                    expAnswer += "(";
                }

                expAnswer += expression[i];

                if (PlayerPrefs.GetInt("Solve") == 1 && i == 4)
                {
                    expAnswer += ")";
                }
            }
            try
            {
                return(((int)EvaluateString.Evaluate(expAnswer)) == answer);
            } catch (Exception e)
            {
                return(false);
            }
        }
    }