変数の応用

重複しないようにランダムに選ぶ

数字が重複しないようランダムに出力したい場合

  int[] ary = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
  System.Random rnd = new System.Random();
  for (int n = ary.Length - 1; n > 1; n--){
   int i = rnd.Next(0, n + 1);
   int tmp = ary[i];
   ary[i] = ary[n];
   ary[n] = tmp;
  }
これでary[]に0から9までの数値がランダムで並びます
例えばary[0] ary[1] ary[2] とすれば
0から9までの数字を 重複させずに3つ ランダムに選べます
ちなみに このようなやり方を「Fisher-Yates」と呼びます

Sin関数を使う

Sin関数を使う事で オブジェクトをなめらかに移動させる事ができます

    float T = 1.0f;
    float Count = 0f;

    void Update()
    {
        float sin = Mathf.Sin(2 * Mathf.PI * Count / T);
        Count += Time.deltaTime;
        this.transform.localPosition = new Vector3(sin * 100, 0, 0);
    }
これで-100~100の間を1秒で1往復するようになります


配列を並び替える


UnityにはSort文で並び替えをする事ができますが 複数の要素を指定しての並び替えをする場合
(例えばレベルとコストが高い順に並び替える)は専用のスクリプトが必要になります

    int[] A = { 1, 3, 4, 8, 2, 10, 5, 6, 7, 9 };

    void Sort()
    {
        int sortno = 0;
        for (int i = 0; i < A.Length - 1; i++)
        {
            if (A[i] > A[i+1])
            {
                for(int ii = i; ii >= 0; ii--) {
                    if (A[ii] <= A[ii+1]){break;}
                    sortno = A[ii];
                    A[ii] = A[ii + 1];
                    A[ii + 1] = sortno;
                }
            }
        }
    }
これを実行する事で 配列Aがソートされます
ちなみにこの方法を「ノームソート」と言います


配列を順列する

配列の並び替えのパターンを全て出力します

    void Start()
    {
        List<string> result = new List<string>();
        List<string> ABCD = new List<string>() { "A", "B", "C", "D" };
        permutation(ABCD, result);
    }

    void permutation(List<string> ABCD, List<string> result)
    {
        int count = ABCD.Count;
        for (int i = 0; i < count; i++)
        {
            result.Add(ABCD[i]);
            ABCD.RemoveAt(i);
            if (count >= 2)
            {
                permutation(ABCD, result);
                ABCD.Insert(i, result[result.Count - 1]);
                result.RemoveAt(result.Count - 1);
            }

            if (count == 1)
            {
                string[] tmp = result.ToArray();
                Debug.Log(string.Join(",", tmp));
                ABCD.Insert(i, result[result.Count - 1]);
                result.RemoveAt(result.Count - 1);
            }
        }
    }
これで「A」「B」「C」「D」の4文字の並び替えを全て出力します


配列の組み合わせ

重複しない配列の並び替えのパターンを全て出力します
※(1,2,3)と(3,2,1)などは同じとして扱う

    void Start()
    {
        // 組み合わせの数
        int Number = 8;
        // いくつ選ぶか
        int Space = 3;
        C(Number, Space);
    }

    void C(int n, int r)
    {
        int[] m = new int[100];
        Nest(1, 0, n, r, m);
    }

    void Nest(int column, int nest, int n, int r, int[] m)
    {
        for (int i = nest + 1; i <= n - r + column; i++)
        {
            m[column] = i;
            if (r != column)
            {
                Nest(column + 1, i, n, r, m);
            }
            else
            {
                string str = "";
                for (int j = 1; j <= r; j++)
                {
                    str += m[j];
                }
                print(str);
            }
        }
    }
これで1~8の数字から3つ選んだ時のパターンを全て出力します

戻 る