たしざん1年生くらい

加算混合の発想 硬直思考からどう脱するか
 前、Unityで足し算し続けるアプリケーションを作った。pongeponge.hatenablog.jp
操作不能の完全観賞用だった。


 前回を反省して、今回はちゃんと入力を作ってみた。

・たし算のみ
・答えは1けた
・0なし
・繰り上がりなし


 ボタンを等間隔に配置とか、簡単にできないんだろうか……まぁいいけどさ。


 コードがごちゃっとしてわかりにくい、と自分でも思う。
もっと振り分けたほうがよかったなぁ。

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class Manager : MonoBehaviour {

    private static int MaxDigit = 3;    //入力可能最大桁
    public Text obj_Answer;
    public Text obj_Expression;

    private string answer = "";
    private string expression = "";
    private string InputNum = "";

    private bool ReceiveInput = true;

	// Use this for initialization
	void Start () {
        Random.seed = System.Environment.TickCount;
        this.MakeExpression();
        this.ShowExpression();
	}
	
	// Update is called once per frame
	void Update () {
       // this.NewProblem();
        this.CheckAnswer(this.InputNum);
	
	}

    private IEnumerator newp()
    {
        this.ReceiveInput = false;

        this.obj_Answer.text = "せいかい!";
        this.obj_Answer.color = Color.red;
        yield return new WaitForSeconds(0.5f);

        this.obj_Answer.color = Color.black;
        this.NewProblem();
        this.PushDeleteBtn();

        this.ReceiveInput = true;
    }

    private void MakeExpression()
    {
        int ans = Random.Range(2, 9);
        int term1 = Random.Range(1, ans - 1);
        int term2 = ans - term1;

        this.expression = string.Format("{0} + {1}", term1, term2);
        this.answer = ans.ToString();
    }

    private void ShowExpression()
    {
        this.obj_Expression.text = this.expression;
    }

    private bool CheckAnswer(string s)
    {
        if (this.answer == s)
        {
            StartCoroutine("newp");
            return true;
        }
        else return false;
    }

    private void NewProblem()
    {
        if (this.CheckAnswer(this.InputNum) == true)
        {
            this.MakeExpression();
            this.ShowExpression();
            this.PushDeleteBtn();
        }
    }

    //数字ボタン
    public void PushBtn(string s)
    {
        if (this.ReceiveInput == false) return;

        //桁制限
        if (this.InputNum.Length == MaxDigit) return;

        this.InputNum += s;

        this.obj_Answer.text = this.InputNum;

        print(this.InputNum);
    }

    //1文字バック
    public void PushBackBtn()
    {
        if (this.ReceiveInput == false) return;

        //これ以上消せない
        if (this.InputNum.Length == 0) return;

        this.InputNum = this.InputNum.Remove(this.InputNum.Length - 1);

        this.obj_Answer.text = this.InputNum;

        print(this.InputNum);
    }

    //全消し
    public void PushDeleteBtn()
    {
        if (this.ReceiveInput == false) return;

        this.InputNum = "";

        this.obj_Answer.text = this.InputNum;

        print(this.InputNum);
    }
}

小学校6年間の算数が6時間でわかる本

小学校6年間の算数が6時間でわかる本