public void DeadCellComesBackToLifeWithThreeNeighborsWhoAreAlive() { //arrange EvolutionEvent = null; LifeCell cellInQuestion = new LifeCell(isAlive: false); LifeCell[] neighbors = { new LifeCell(isAlive: true), new LifeCell(isAlive: true), new LifeCell(isAlive: true) }; cellInQuestion.SubscribeToEvolutionEvent(ref EvolutionEvent); foreach (var neighbor in neighbors) { neighbor.SubscribeToEvolutionEvent(ref EvolutionEvent); cellInQuestion.AddNeighbor(neighbor); neighbor.AddNeighbor(cellInQuestion); } //act cellInQuestion.CheckIfYouAreAlive(); foreach (var neighbor in neighbors) { neighbor.CheckIfYouAreAlive(); } EvolutionEvent?.Invoke(this, EventArgs.Empty); //assert Assert.IsTrue(cellInQuestion.IsAlive); }
public float Basic(LifeCell cell, float sum) { float nextstate; if (cell.isLive) { if (2 <= sum && sum <= 3) { nextstate = 1f; cell.isLive = true; } else { nextstate = 0f; cell.isLive = false; } } else { if (3 == sum) { nextstate = 1f; cell.isLive = true; } else { nextstate = 0f; cell.isLive = false; } } return(nextstate); }
public float RandomBinary(LifeCell cell, float sum) { float nextstate; cell.isLive = UnityEngine.Random.value > 0.5f; nextstate = cell.isLive ? 1f : 0f; return(nextstate); }
public void NewCellIteration(Dictionary <CellCoords, Cell> newCells) { LivingCells = new Dictionary <CellCoords, LifeCell>(new CellCoordsComparer()); LivingCells.Clear(); List <LifeCell> removeCellsFromList = new List <LifeCell>(); foreach (LifeCell cell in DisplayLifeCells) { Cell CurrentCell; bool continueLife = newCells.TryGetValue(new CellCoords(cell.x, cell.y), out CurrentCell); if (!continueLife) { Destroy(cell.gameObject); removeCellsFromList.Add(cell); } else { LivingCells.Add(new CellCoords(cell.x, cell.y), cell); } } foreach (LifeCell removeCell in removeCellsFromList) { DisplayLifeCells.Remove(removeCell); } removeCellsFromList.Clear(); if (game.RepeatingFieldFound() && !this._fieldRepetitionFound) { this._autoSimulationSpeed = 1; if (this._simulationSpeedSlider != null) { _simulationSpeedSlider.value = 1; } this._fieldRepetitionFound = true; _displayInformationText.text = "Field Repetition found"; } foreach (CellCoords coordinates in newCells.Keys) { if (!LivingCells.ContainsKey(coordinates)) { GameObject createdCell = Instantiate(LifeCellPrefab, new Vector3(coordinates.x, FieldHeight, coordinates.y), Quaternion.identity, LifeCellContainer.transform) as GameObject; LifeCell createdLifeCell = createdCell.GetComponent <LifeCell>(); createdLifeCell.x = coordinates.x; createdLifeCell.y = coordinates.y; DisplayLifeCells.Add(createdLifeCell); } } this._displayIteration.text = String.Format("Iteration: {0}", game.GetIterationNumber()); if (_autoSimulationActive) { StartCoroutine(NextIterationDelay()); } //coroutine if auto simulation }
public void IsAlive_Alive_Test() { // Arange bool expectedIsAlive = true; // Acts LifeCell testCell = new LifeCell(true); // Assert Assert.AreEqual(expectedIsAlive, testCell.IsAlive); }
public void IsAlive_Dead_Test() { // Arange bool expectedIsAlive = false; // Acts LifeCell testCell = new LifeCell(false); // Assert Assert.AreEqual(expectedIsAlive, testCell.IsAlive); }
public void UpdateCellState_ShouldStayDead_Test() { // Arrange LifeCell testCell = new LifeCell(false); PrivateObject privateObject = new PrivateObject(testCell); privateObject.SetField("shouldBorn", false); bool expectedIsBorn = false; // Act privateObject.Invoke("UpdateState"); // Assert bool isBorn = Convert.ToBoolean(privateObject.GetProperty("IsAlive")); Assert.AreEqual(expectedIsBorn, isBorn); }
public void UpdateCellState_ShouldDie_Test() { // Arrange LifeCell testCell = new LifeCell(true); PrivateObject privateObject = new PrivateObject(testCell); privateObject.SetField("shouldDie", true); bool expectedIsDead = true; // Act privateObject.Invoke("UpdateState"); // Assert bool isDead = !Convert.ToBoolean(privateObject.GetProperty("IsAlive")); Assert.AreEqual(expectedIsDead, isDead); }
public static LifeCell[,] GetSeed(Position size, List <Position> aliveCellPositions) { var seed = new LifeCell[size._x, size._y]; for (int x = 0; x < size._x; x++) { for (int y = 0; y < size._y; y++) { var cellPosition = new Position(x, y); seed[x, y] = new LifeCell(cellPosition); foreach (var item in aliveCellPositions) { if (item._x == x && item._y == y) { seed[x, y].IsAlive = true; } } } } return(seed); }
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); } } }
public float Continuous(LifeCell cell, float sum) { float nextstate; float avg = sum / 8f; if (avg == 1f) { nextstate = 0f; cell.isLive = false; } else if (avg == 0f) { nextstate = 1f; cell.isLive = true; } else { nextstate = cell.state + avg - cell.preState; cell.isLive = nextstate > 0.5f; } nextstate = Mathf.Clamp(nextstate, 0f, 1f); return(nextstate); }
public float VichniacVote(LifeCell cell, float sum) { float nextstate; if (cell.isLive) { sum++; } if (sum <= 4) { cell.isLive = false; } else { cell.isLive = true; } if (sum == 4 || sum == 5) { cell.isLive = !cell.isLive; } nextstate = cell.isLive ? 1f : 0f; return(nextstate); }
public void CalculateNeighbours_CornerCell_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(true, 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(0); PrivateObject privateObject = new PrivateObject(testCell); int expectedNeighbours = 2; // Act int testedNeighbours = Convert.ToInt32(privateObject.Invoke("CalculateNeighbours")); // Assert Assert.AreEqual(expectedNeighbours, testedNeighbours); }
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); }
private LifeCell[,] GetLifeCellBoard(int[][] board) { int height = board.Length; int width = board[0].Length; //LifeCell[][] temp = new LifeCell[height][width];//error LifeCell[,] temp = new LifeCell[height, width]; for (int row = 0; row < height; row++) { for (int col = 0; col < width; col++) { temp[row, col] = new LifeCell(); temp[row, col].Status = board[row][col]; temp[row, col].NeighborCount = 0; //////////////////// // 0 * * // 0 x 0 // * * 0 //////////////////// if (row > 0) { if (board[row - 1][col] == 1) { temp[row, col].NeighborCount++; } if (col > 0) { if (board[row - 1][col - 1] == 1) { temp[row, col].NeighborCount++; } } } if (row < (height - 1)) { if (board[row + 1][col] == 1) { temp[row, col].NeighborCount++; } if (col < (width - 1)) { if (board[row + 1][col + 1] == 1) { temp[row, col].NeighborCount++; } } } //////////////////// // * 0 0 // * x * // 0 0 * //////////////////// if (col > 0) { if (board[row][col - 1] == 1) { temp[row, col].NeighborCount++; } if (row < (height - 1)) { if (board[row + 1][col - 1] == 1) { temp[row, col].NeighborCount++; } } } if (col < (width - 1)) { if (board[row][col + 1] == 1) { temp[row, col].NeighborCount++; } if (row > 0) { if (board[row - 1][col + 1] == 1) { temp[row, col].NeighborCount++; } } } } } return(temp); }
override internal void SetUpCell(LifeCell cell) { base.SetUpCell(cell); ((PixelCell)cell).texture = texture; ((PixelCell)cell).size = size; }
override internal void SetUpCell(LifeCell cell) { base.SetUpCell(cell); ((VertexHeightCell)cell).mesh = meshFilter.mesh; }
override internal void SetUpCell(LifeCell cell) { base.SetUpCell(cell); }
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); }
internal virtual void SetUpCell(LifeCell cell) { }