private static void CreateAndInitializeThreexThreeGrid() { ThreexThreeGrid = new Grid(3, 3); ThreexThreeGrid.AddCell(new Cell() { RowIndex = 0, ColIndex = 0 }); ThreexThreeGrid.AddCell(new Cell() { RowIndex = 0, ColIndex = 1 }); ThreexThreeGrid.AddCell(new Cell() { RowIndex = 0, ColIndex = 2 }); ThreexThreeGrid.AddCell(new Cell() { RowIndex = 1, ColIndex = 0 }); ThreexThreeGrid.AddCell(new Cell() { RowIndex = 1, ColIndex = 1 }); ThreexThreeGrid.AddCell(new Cell() { RowIndex = 1, ColIndex = 2 }); ThreexThreeGrid.AddCell(new Cell() { RowIndex = 2, ColIndex = 0 }); ThreexThreeGrid.AddCell(new Cell() { RowIndex = 2, ColIndex = 1 }); ThreexThreeGrid.AddCell(new Cell() { RowIndex = 2, ColIndex = 2 }); }
public void Test_Cells_ValidCellsAreAdded_ReturnsCollectionOfAddedCells() { var grid = new Grid(2, 2); var cell0 = new Cell { RowIndex = 0, ColIndex = 0 }; var cell1 = new Cell { RowIndex = 0, ColIndex = 1 }; var cell2 = new Cell { RowIndex = 1, ColIndex = 0 }; var cell3 = new Cell { RowIndex = 1, ColIndex = 1 }; grid.AddCell(cell0); grid.AddCell(cell1); grid.AddCell(cell2); grid.AddCell(cell3); var cells = grid.Cells; Assert.That(cells.Count(), Is.EqualTo(4), "Count should have been 4"); Assert.That(cells.ElementAt(0), Is.EqualTo(cell0), "Cell added using AddCell and cell retrieved using Cells should be same"); Assert.That(cells.ElementAt(1), Is.EqualTo(cell1), "Cell added using AddCell and cell retrieved using Cells should be same"); Assert.That(cells.ElementAt(2), Is.EqualTo(cell2), "Cell added using AddCell and cell retrieved using Cells should be same"); Assert.That(cells.ElementAt(3), Is.EqualTo(cell3), "Cell added using AddCell and cell retrieved using Cells should be same"); }
/// <summary> /// gets a deep copy of the <paramref name="grid"/> /// </summary> /// <param name="grid"></param> /// <returns></returns> public static IGrid<ICell> GetDeepCopy(this IGrid<ICell> grid) { var gridCopy = new Grid(grid.NumberOfRows, grid.NumberOfColumns); foreach (var cell in grid.Cells) { var cellCopy = new Cell { RowIndex = cell.RowIndex, ColIndex = cell.ColIndex, IsAlive = cell.IsAlive }; gridCopy.AddCell(cellCopy); } return gridCopy; }
public void Test_AddCell_CellHasRowIndexEqualToFour_ThrowsArgumentOutOfRangeException() { var grid = new Grid(4, 4); var cell = new Cell { RowIndex = 4, ColIndex = 1 }; Assert.Throws<ArgumentOutOfRangeException>(() => grid.AddCell(cell), "ArgumentOutOfRangeException should be thrown"); }
// Use this for initialization void Awake() { MeshSimple ms = new MeshSimple(fileName); mt = new MeshTopo(ms); Mesh m = ms.GetMesh(); gameObject.GetComponent<MeshCollider>().sharedMesh = m; grid = new Grid(); foreach( Triangle t in mt.triangles) { grid.AddCell(t); } }
private static void CreateAndInitializeFivexFiveGridForBlinkerOscillatorPattern() { FivexFiveGridForBlinkerOscillatorPattern = new Grid(5, 5); for (int rowIndex = 0; rowIndex < 5; rowIndex++) { for (int colIndex = 0; colIndex < 5; colIndex++) { var cell = new Cell { RowIndex = rowIndex, ColIndex = colIndex, IsAlive = false }; FivexFiveGridForBlinkerOscillatorPattern.AddCell(cell); } } FivexFiveGridForBlinkerOscillatorPattern.GetCellByIndex(2, 1).IsAlive = true; FivexFiveGridForBlinkerOscillatorPattern.GetCellByIndex(2, 2).IsAlive = true; FivexFiveGridForBlinkerOscillatorPattern.GetCellByIndex(2, 3).IsAlive = true; }
private static void CreateAndInitializeSixxSixGridForToadOscillatorPattern() { SixxSixGridForToadOscillatorPattern = new Grid(6, 6); for (int rowIndex = 0; rowIndex < 6; rowIndex++) { for (int colIndex = 0; colIndex < 6; colIndex++) { var cell = new Cell { RowIndex = rowIndex, ColIndex = colIndex, IsAlive = false }; SixxSixGridForToadOscillatorPattern.AddCell(cell); } } SixxSixGridForToadOscillatorPattern.GetCellByIndex(2, 2).IsAlive = true; SixxSixGridForToadOscillatorPattern.GetCellByIndex(2, 3).IsAlive = true; SixxSixGridForToadOscillatorPattern.GetCellByIndex(2, 4).IsAlive = true; SixxSixGridForToadOscillatorPattern.GetCellByIndex(3, 1).IsAlive = true; SixxSixGridForToadOscillatorPattern.GetCellByIndex(3, 2).IsAlive = true; SixxSixGridForToadOscillatorPattern.GetCellByIndex(3, 3).IsAlive = true; }
public void GetLiveCellsThatShouldDieWhenTheyHaveMoreThanThreeNeighbours() { var grid = new Grid(5, 5); int[][] graph = { new[] { 1, 1, 1, 0, 0 }, new[] { 0, 1, 1, 0, 0 }, new[] { 0, 0, 0, 0, 0 }, new[] { 0, 0, 0, 0, 0 }, new[] { 0, 0, 0, 0, 0 } }; _testHelper.TransformGraphToCells(graph).ForEach(cell => grid.AddCell(cell)); var allLiveNeighboursOfAliveCells = new List <Cell>(); foreach (var livingCell in grid.LivingCells) { allLiveNeighboursOfAliveCells.AddRange(grid.GetLiveNeighboursOfACell(livingCell)); } int[][] expectedDeadCellsGraph = { new[] { 0, 1, 0, 0, 0 }, new[] { 0, 1, 0, 0, 0 }, new[] { 0, 0, 0, 0, 0 }, new[] { 0, 0, 0, 0, 0 }, new[] { 0, 0, 0, 0, 0 } }; var expectedDeadCells = _testHelper.TransformGraphToCells(expectedDeadCellsGraph); var neighboursOfCellsCount = grid.GetLiveCellsAndItsNumberOfLiveNeighboursDict(); var actualDeadCells = _rules.GetLiveCellsThatShouldDie(neighboursOfCellsCount); expectedDeadCells.Should().BeEquivalentTo(actualDeadCells); Assert.Equal(2, actualDeadCells.Count); }
public void GetLiveCellThatShouldDieWhenItHasNoNeighbours() { var rules = new DeadEvolutionRules(); var grid = new Grid(5, 5); var cellFour = new Cell(3, 3); grid.AddCell(cellFour); var neighboursOfAliveCell = new List <IEnumerable <Cell> > { grid.GetLiveNeighboursOfLivingCell(cellFour), }; var expectedDeadCells = new List <Cell> { cellFour }; var cellsThatShouldDie = rules.GetLiveCellsThatShouldDie(neighboursOfAliveCell, new List <Cell> { cellFour }); expectedDeadCells.Should().BeEquivalentTo(cellsThatShouldDie); Assert.Equal(1, cellsThatShouldDie.Count); }
public void GetNoDeadCellsThatShouldBecomeAliveWhenTheyDoNotHaveThreeLiveNeighbours() { var grid = new Grid(5, 5); int[][] graph = { new[] { 0, 0, 0, 0, 0 }, new[] { 0, 1, 0, 1, 0 }, new[] { 1, 0, 0, 0, 0 }, new[] { 0, 0, 0, 0, 0 }, new[] { 0, 0, 0, 0, 0 } }; _testHelper.TransformGraphToCells(graph).ForEach(cell => grid.AddCell(cell)); var allDeadNeighboursOfAliveCells = new List <Cell>(); foreach (var livingCell in grid.LivingCells) { allDeadNeighboursOfAliveCells.AddRange(grid.GetDeadNeighboursOfACell(livingCell)); } int[][] expectedLiveCellsGraph = { new[] { 0, 0, 0, 0, 0 }, new[] { 0, 0, 0, 0, 0 }, new[] { 0, 0, 0, 0, 0 }, new[] { 0, 0, 0, 0, 0 }, new[] { 0, 0, 0, 0, 0 } }; var expectedLiveCells = _testHelper.TransformGraphToCells(expectedLiveCellsGraph); var cellsThatShouldLive = _rules.GetDeadCellsThatShouldLive(allDeadNeighboursOfAliveCells); expectedLiveCells.Should().BeEquivalentTo(expectedLiveCells); Assert.Empty(cellsThatShouldLive); }
public void GetTheLivingNeighboursOfALivingCellWithSparseNeighbours() { var grid = new Grid(5, 5); var cellTarget = new Cell(1, 1); grid.AddCell(cellTarget); grid.AddCell(new Cell(1, 2)); grid.AddCell(new Cell(4, 4)); grid.AddCell(new Cell(1, 3)); grid.AddCell(new Cell(3, 1)); grid.AddCell(new Cell(0, 2)); var expectedNeighbourCells = new List <Cell> { new Cell(0, 2), new Cell(1, 2) }; var actualNeighbourCells = grid.GetLiveNeighboursOfLivingCell(cellTarget); expectedNeighbourCells.Should().BeEquivalentTo(actualNeighbourCells); Assert.Equal(2, actualNeighbourCells.Count()); }
public void Test_AddCell_NullParam_ThrowsArgumentNullException() { var grid = new Grid(4, 4); Assert.Throws<ArgumentNullException>(() => grid.AddCell(null), "ArgumentNullException should be thrown if a null cell is added"); }
public void Test_GetCellByIndex_ValidCellIsAdded_ReturnsSameCellAddedUsingAddCell() { var grid = new Grid(4, 4); var cell = new Cell { RowIndex = 2, ColIndex = 2 }; grid.AddCell(cell); var returnedCell = grid.GetCellByIndex(2, 2); Assert.That(returnedCell, Is.EqualTo(cell), "Cell added using AddCell and cell retrieved using GetCellByIndex should be same"); }
public void Test_GetCellByIndex_RowIndexEqualToFour_ThrowsArgumentOutOfRangeException() { var grid = new Grid(4, 4); var cell = new Cell { RowIndex = 1, ColIndex = 1 }; grid.AddCell(cell); Assert.Throws<ArgumentOutOfRangeException>(() => grid.GetCellByIndex(4,1), "ArgumentOutOfRangeException should be thrown"); }
private static void CreateAndInitializeTwoxTwoGrid() { TwoxTwoGrid = new Grid(2, 2); TwoxTwoGrid.AddCell(new Cell() { RowIndex = 0, ColIndex = 0 }); TwoxTwoGrid.AddCell(new Cell() { RowIndex = 0, ColIndex = 1 }); TwoxTwoGrid.AddCell(new Cell() { RowIndex = 1, ColIndex = 0 }); TwoxTwoGrid.AddCell(new Cell() { RowIndex = 1, ColIndex = 1 }); }
void Start() { int n ; for (n = 0; n < _containers.Length; ++n) { GameObject container = _containers[n].gameObject; float size = container.transform.localScale.x; float cellSize = size / (float)_width; var behaviour = container.GetComponent<ContainerBehaviour>(); Grid grid = new Grid(_width, _height, cellSize); for (int i = 0; i < _height; ++i) { for (int j = 0; j < _width; ++j) { Color c = _data.GetPixel(j, i + n * _height); if (aprxEqual(c, Color.red)) { float x = (float)j * cellSize + cellSize * 0.5f + container.transform.position.x - size * 0.5f; float y = (float)i * cellSize + cellSize * 0.5f + container.transform.position.y - size * 0.5f; Transform cell = (Transform)Instantiate(_redCellPrefab, new Vector3(x, y, 0), Quaternion.identity); cell.parent = container.transform; cell.transform.localScale = new Vector3(cell.transform.localScale.x * cellSize, cell.transform.localScale.y * cellSize, 1f); grid.AddCell(new Cell(j, _height - i - 1, 1, cell)); } if (aprxEqual(c, Color.green)) { float x = (float)j * cellSize + cellSize * 0.5f + container.transform.position.x - size * 0.5f; float y = (float)i * cellSize + cellSize * 0.5f + container.transform.position.y - size * 0.5f; Transform cell = (Transform)Instantiate(_greenCellPrefab, new Vector3(x, y, 0), Quaternion.identity); cell.parent = container.transform; cell.transform.localScale = new Vector3(cell.transform.localScale.x * cellSize, cell.transform.localScale.y * cellSize, 1f); grid.AddCell(new Cell(j, _height - i - 1, 2, cell)); } if (aprxEqual(c, Color.blue)) { float x = (float)j * cellSize + cellSize * 0.5f + container.transform.position.x - size * 0.5f; float y = (float)i * cellSize + cellSize * 0.5f + container.transform.position.y - size * 0.5f; Transform cell = (Transform)Instantiate(_blueCellPrefab, new Vector3(x, y, 0), Quaternion.identity); cell.parent = container.transform; cell.transform.localScale = new Vector3(cell.transform.localScale.x * cellSize, cell.transform.localScale.y * cellSize, 1f); grid.AddCell(new Cell(j, _height - i - 1, 3, cell)); } } } grid.Container = container.transform; grid.Update(); behaviour.InitData(grid, _containers); } n = 0; foreach (Transform c in _containers) { CreateContainer(c.gameObject, n); ++n; } }