Pongeponge

1次元ライフゲーム

セルオートマトン法―複雑系の自己組織化と超並列処理


 せっかくなので1次元バージョン作ってみた。



条件は、
両隣死亡 → 死亡
片方生存 → 誕生
両隣生存 → 圧死

こんな感じになってます。



ソースコードはこんな感じ。

using UnityEngine;
using System.Collections;

public class Manager : MonoBehaviour {

    public GameObject Box;
    public Material OnMaterial;
    public Material OffMaterial;

    private int CellsSize;
    private float WaitTime;

    private bool[] Cells;
    private bool[] Buff;
    private GameObject[] Boxs;

	// Use this for initialization
	void Start () {
        this.Init();
        StartCoroutine("Runing");
	}
	
	// Update is called once per frame
	void Update () {
	
	}

    //走り出すアレ
    private IEnumerator Runing()
    {
        while (true)
        {
            yield return new WaitForSeconds(this.WaitTime);
            this.MakeNextGeneration();
        }
    }

    //初期化
    private void Init()
    {
        this.WaitTime = 0.1f;
        this.CellsSize = 200;

        this.Cells = new bool[this.CellsSize];
        this.Buff = new bool[this.CellsSize];
        this.Boxs = new GameObject[this.CellsSize];

        float div = (float)(2.0*Mathf.PI/(this.CellsSize));
        for (int p = 0; p < this.CellsSize; p++)
        {
            float x = 40.0f*Mathf.Cos(div * p);
            float z = 40.0f * Mathf.Sin(div * p);
            this.Boxs[p] = Instantiate(this.Box);
            this.Boxs[p].transform.position = new Vector3(x, 0, z);
            if (Random.Range(0, 2) == 0)
            {
                this.Cells[p] = true;
                this.Boxs[p].GetComponent<MeshRenderer>().material = this.OnMaterial;
            }
            else
            {
                this.Cells[p] = false;
                this.Boxs[p].GetComponent<MeshRenderer>().material = this.OffMaterial;
            }
        }
    }

    //次世代を作成
    private void MakeNextGeneration()
    {
        for (int p = 0; p < this.CellsSize; p++)
        {
            int count = 0;
            int r = p - 1;
            int l = p + 1;

            if (r < 0) r = this.CellsSize - 1;
            if (l >= this.CellsSize) l = 0;

            if (this.Cells[r] == true) count++;
            if (this.Cells[l] == true) count++;

            switch (count)
            {
                case 0:
                    this.Buff[p] = false;
                    break;
                case 1:
                    this.Buff[p] = true;
                    break;
                case 2:
                    this.Buff[p] = false;
                    break;
            }
        }

        for (int p = 0; p < this.CellsSize; p++)
        {
            this.Cells[p] = this.Buff[p];
            if (this.Cells[p] == true)
            {
                this.Boxs[p].GetComponent<MeshRenderer>().material = this.OnMaterial;
            }
            else if (this.Cells[p] == false)
            {
                this.Boxs[p].GetComponent<MeshRenderer>().material = this.OffMaterial;
            }
        }
    }
}