Pongeponge

ライフゲーム2

細胞のしくみビジュアル図解―ミクロの世界の巧妙な生命装置 (ニュートンムック Newton別冊)


 やったーできたー(棒)
やっぱり条件間違ってた。



 ソースはこんな感じ。
yieldをなんか使いこなせない。
最初Updateの中に突っ込んでて文句言われたし。

using UnityEngine;
using System.Collections;

public class Main : MonoBehaviour
{

    public GameObject box;
    private int width = 30;
    private int height = 30;

    public Material bl;
    public Material re;

    private GameObject[,] boxs;
    private bool[,] world;
    private bool[,] buf;

    // Use this for initialization
    void Start()
    {
        boxs = new GameObject[width, height];
        world = new bool[width, height];
        buf = new bool[width, height];

        for (int x = 0; x < width; x++)
        {
            for (int z = 0; z < height; z++)
            {
                boxs[x, z] = Instantiate(box);
                boxs[x, z].transform.position = new Vector3(x, 0, z);


                if (Random.Range(0, 2) == 0)
                {
                    world[x, z] = true;
                    boxs[x, z].GetComponent<MeshRenderer>().material = re;
                }
                else
                {
                    world[x, z] = false;
                    boxs[x, z].GetComponent<MeshRenderer>().material = bl;
                }
            }
        }

        StartCoroutine("celll");
    }

    // Update is called once per frame
    void Update()
    {
    }

    private IEnumerator celll()
    {
        while (true)
        {
            yield return new WaitForSeconds(0.1f);
            cel();

        }
    }

    //private IEnumerator cel()
    private void cel()
    {
        int count;

        for (int x = 0; x < width; x++)
        {
            for (int z = 0; z < height; z++)
            {
                count = 0;
                for (int px = -1; px <= 1; px++)
                {
                    for (int pz = -1; pz <= 1; pz++)
                    {
                        if (px == 0 && pz == 0)
                        {
                        }
                        else
                        {
                            int vx = x + px;
                            int vz = z + pz;

                            if (vx < 0) vx = width - 1;
                            if (vx >= width) vx = 0;

                            if (vz < 0) vz = height - 1;
                            if (vz >= height) vz = 0;

                            if (world[vx, vz] == true) count++;
                        }
                    }
                }

                switch (count)
                {
                    case 0:
                        buf[x, z] = false;
                        break;
                    case 1:
                        buf[x, z] = false;
                        break;
                    case 2:
                        buf[x, z] = world[x, z];
                        break;
                    case 3:
                        buf[x, z] = true;
                        break;
                    case 4:
                        buf[x, z] = false;
                        break;
                    case 5:
                        buf[x, z] = false;
                        break;
                    case 6:
                        buf[x, z] = false;
                        break;
                    case 7:
                        buf[x, z] = false;
                        break;
                    case 8:
                        buf[x, z] = false;
                        break;
                }
            }
        }

        for (int x = 0; x < width; x++)
        {
            for (int z = 0; z < height; z++)
            {
                world[x, z] = buf[x, z];
                if (world[x, z] == true)
                {
                    boxs[x, z].GetComponent<MeshRenderer>().material = re;
                }
                else
                {
                    boxs[x, z].GetComponent<MeshRenderer>().material = bl;
                }
            }
        }
    }
}