示例#1
0
    internal virtual void CreateCells()
    {
        initializer.Prepare(pattern, col, row);

        cells = new LifeCell[col, row];
        float offsetX = margin * 0.5f * (float)-col;
        float offsetY = margin * 0.5f * (float)-row;

        for (int x = 0; x < col; x++)
        {
            for (int y = 0; y < row; y++)
            {
                GameObject cell = Instantiate(cellPrefab);
                cell.transform.parent        = transform;
                cell.transform.localPosition = new Vector3(offsetX + margin * (float)x, 0f, offsetY + margin * (float)y);
                LifeCell lifeCell = cell.GetComponent <LifeCell>();
                lifeCell.y  = y;
                lifeCell.x  = x;
                cells[x, y] = lifeCell;

                switch (GoLController.instance.rule)
                {
                case GoLController.GoLRule.Basic:
                case GoLController.GoLRule.RandomBinary:
                case GoLController.GoLRule.VichniacVote:
                    bool isLive = this.initializer.IsLive(x, y);
                    lifeCell.Init(isLive, isLive ? 1.0f : 0f);
                    break;

                case GoLController.GoLRule.Continuous:
                    lifeCell.Init(true, UnityEngine.Random.value);
                    break;
                }
                SetUpCell(lifeCell);

                lifeCell.UpdateState(lifeCell.state);
            }
        }
    }
示例#2
0
        public void CalculateState_ShoudStatyDead_Test()
        {
            // Arrange
            List <List <LifeCell> > testLifeCellsMatrix = new List <List <LifeCell> >();
            List <LifeCell>         column1             = new List <LifeCell>();

            testLifeCellsMatrix.Add(column1); // 1 alive neighour
            column1.Add(new LifeCell(false, ref testLifeCellsMatrix));
            column1.Add(new LifeCell(true, ref testLifeCellsMatrix));
            column1.Add(new LifeCell(false, ref testLifeCellsMatrix));

            List <LifeCell> column2 = new List <LifeCell>();

            testLifeCellsMatrix.Add(column2); // 1 alive neighour
            column2.Add(new LifeCell(false, ref testLifeCellsMatrix));
            column2.Add(new LifeCell(false, ref testLifeCellsMatrix));
            column2.Add(new LifeCell(true, ref testLifeCellsMatrix));

            List <LifeCell> column3 = new List <LifeCell>();

            testLifeCellsMatrix.Add(column3); // 1 alive neighour
            column3.Add(new LifeCell(false, ref testLifeCellsMatrix));
            column3.Add(new LifeCell(false, ref testLifeCellsMatrix));
            column3.Add(new LifeCell(true, ref testLifeCellsMatrix));

            LifeCell testCell = testLifeCellsMatrix.ElementAt(0).ElementAt(1);

            bool expectedIsAlive = false;

            // Act
            testCell.CalculateState();
            testCell.UpdateState();

            // Assert
            Assert.AreEqual(expectedIsAlive, testCell.IsAlive);
        }
示例#3
0
    internal virtual void ChangeState()
    {
        for (int x = 0; x < col; x++)
        {
            for (int y = 0; y < row; y++)
            {
                LifeCell cell = cells[x, y];

                float sum = 0f;
                for (int dx = -1; dx <= 1; dx++)
                {
                    for (int dy = -1; dy <= 1; dy++)
                    {
                        if (dx == 0 && dy == 0)
                        {
                            continue;
                        }
                        LifeCell c = getCellByIndex(x + dx, y + dy);

                        switch (rule)
                        {
                        case GoLRule.VichniacVote:
                            sum += c.isLive ? 1 : 0;
                            break;

                        default:
                            sum += c.state;
                            break;
                        }
                    }
                }

                MethodInfo method = processor.GetType().GetMethod(rule.ToString());
                if (method == null)
                {
                    Debug.LogWarning(string.Format("undefined method: {0}", rule.ToString()));
                    return;
                }
                float nextstate = (float)method.Invoke(processor, new object[] { cell, sum });
                cell.nextState = nextstate;

                if (continuous || rule == GoLRule.Continuous)
                {
                    cell.ChangeValue(cell.state, cell.nextState);
                }
                else
                {
                    cell.UpdateState(cell.nextState);
                }
            }
        }

        for (int x = 0; x < col; x++)
        {
            for (int y = 0; y < row; y++)
            {
                LifeCell cell = cells[x, y];
                cell.preState = cell.state;
                cell.state    = cell.nextState;
            }
        }

        Invoke("ChangeState", span);
    }