public void Choose(GridButton button) { // Store if the operand is chosen if (button.GetNumber() == "+" || button.GetNumber() == "-" || button.GetNumber() == "*" || button.GetNumber() == "/") { Operand.text = button.GetNumber(); SelectedOperand = button; } else { // Store the first number if its empty if (Selected1 == null) { Value1.text = button.GetNumber(); Selected1 = button; } // Store the second number if its empty else if (Selected2 == null) { Value2.text = button.GetNumber(); Selected2 = button; } } // If all required button is not empty, check the result if (Selected1 != null && Selected2 != null && SelectedOperand != null) { StartCoroutine(CheckResult()); } }
IEnumerator CheckResult() { int result = 0; switch (SelectedOperand.GetNumber()) { case "+": result = int.Parse(Selected1.GetNumber()) + int.Parse(Selected2.GetNumber()); break; case "-": result = int.Parse(Selected1.GetNumber()) - int.Parse(Selected2.GetNumber()); break; case "*": result = int.Parse(Selected1.GetNumber()) * int.Parse(Selected2.GetNumber()); break; case "/": result = int.Parse(Selected1.GetNumber()) / int.Parse(Selected2.GetNumber()); break; } Result.text = result.ToString(); //StatsManager.instance.SaveStats(StatsType.FailedCount); // Check if there is a corret answer for answer button in the grids and check for the win condition each time player answered correctly foreach (GridButton button in buttons) { int n; int.TryParse(button.GetNumber(), out n); if (n == result && button.GetIsAnswer() && !button.GetAnswered()) { //StatsManager.instance.SaveStats(StatsType.SuccessCount); //StatsManager.instance.SaveStats(StatsType.FailedCount, 1, false); SelectedResult = button; Selected1.SetAnswered(true); Selected2.SetAnswered(true); SelectedOperand.SetAnswered(true); SelectedResult.SetAnswered(true); Selected1 = null; Selected2 = null; SelectedOperand = null; SelectedResult = null; Value1.text = "_"; Value2.text = "_"; Operand.text = "_"; Result.text = "_"; yield return(new WaitForSeconds(1.0f)); CheckWin(); break; } } }
public void Cancel(GridButton button) { // Reset the text and remove from our stored button Result.text = "_"; if (button == Selected1) { Selected1 = null; Value1.text = "_"; } else if (button == Selected2) { Selected2 = null; Value2.text = "_"; } else if (button = SelectedOperand) { SelectedOperand = null; Operand.text = "_"; } }