/// <summary> /// Set target grid schema similar to source grid schema /// </summary> /// <param name="sourceGrid"></param> /// <param name="targetGrid"></param> private static void MatchSchema(Grid sourceGrid, Grid targetGrid) { while (targetGrid.RowCount < sourceGrid.RowCount) { Row newRow = new Row(); for (int k = 0; k < targetGrid.ColumnCount; k++) { Cell newCell = new Cell(false); newRow.AddCell(newCell); } targetGrid.AddRow(newRow); } while (targetGrid.ColumnCount < sourceGrid.ColumnCount) { Cell cell = new Cell(false); for (int k = 0; k < targetGrid.RowCount; k++) { targetGrid[k].AddCell(cell); } targetGrid.ColumnCount += 1; } }
/// <summary> /// Check if rule satisfies to expand column /// </summary> /// <param name="inputGrid"></param> /// <param name="outputGrid"></param> /// <param name="colId"></param> private static void CheckColumnGrowth(Grid inputGrid, Grid outputGrid, int colId) { //Create a whole new column in the beginning or end if rule is satified for any of the cell Boolean columnCreatedFlag = false; //Boolean IsPreviousCellsFilled = false; // start with the index 1 until 1 less than last index as index 0 and last index cannot have 3 live adjacent cell in any case // This index 0 and last index must be included if rule is changed in future; dead can alive with 2 live adjacent cells for (int i = 1; i < inputGrid.RowCount - 1; i++) { if (Rule.CountAliveNeighbours(inputGrid, new CoOrdinates(i, colId)) == 3) { if (columnCreatedFlag == false) { //if (IsPreviousCellsFilled == false) //{ for (int k = 0; k < outputGrid.RowCount; k++) { // Fill all cells with false Cell newDeadCell = new Cell(false); if (colId == -1) { outputGrid[k].InsertCell(0, newDeadCell, outputGrid.ColumnCount); } else { outputGrid[k].AddCell(newDeadCell); } } // IsPreviousCellsFilled = true; //} // increment column count to 1 outputGrid.ColumnCount += 1; columnCreatedFlag = true; } int yAxis = (colId == -1) ? 0 : outputGrid.ColumnCount - 1; outputGrid[i, yAxis].IsAlive = true; } } }
/// <summary> /// Insert a cell into specified index position /// </summary> /// <param name="index"></param> /// <param name="cell"></param> /// <param name="ColumnCount"></param> public void InsertCell(int index, Cell cell, int ColumnCount) { if (index < 0 || index >= ColumnCount) throw new ArgumentOutOfRangeException("Invalid Index value: must be greater than zero and less than Column count"); Cells.Insert(index, cell); }
/// <summary> /// Add a cell at the end of the row /// </summary> /// <param name="cell"></param> public void AddCell(Cell cell) { Cells.Add(cell); }
/// <summary> /// Setup grid using row and column count /// </summary> /// <param name="rows"></param> /// <param name="columns"></param> private void Setup(int rows, int columns) { if (rows <= 0 || columns <= 0) throw new ArgumentOutOfRangeException("Row and Column size must be greater than zero"); GridObj = new List<Row>(); for (int i = 0; i < rows; i++) { Row row = new Row(); for (int j = 0; j < columns; j++) { Cell cell = new Cell(false); row.AddCell(cell); } GridObj.Add(row); } ColumnCount = columns; RowCount = rows; }
/// <summary> /// Evaluate Cell state in next generation /// </summary> /// <param name="cell"></param> /// <param name="liveNeighbourCount"></param> /// <returns>returns true if alive otherwise false</returns> private static Boolean IsAliveInNextState(Cell cell, int liveNeighbourCount) { Boolean alive = false; if (cell.IsAlive) { // if cell is alive and 2 or 3 ajacent cells are alive then set it to alive in next generation if (liveNeighbourCount == 2 || liveNeighbourCount == 3) { alive = true; } } // if cell is dead and 3 adjacent cells are alive then set it to alive in next generation else if (liveNeighbourCount == 3) { alive = true; } return alive; }
/// <summary> /// Check if rule satisfies to expand row /// </summary> /// <param name="inputGrid"></param> /// <param name="outputGrid"></param> /// <param name="rowId"></param> private static void CheckRowGrowth(Grid inputGrid, Grid outputGrid, int rowId) { //Create a whole new row in the beginning or end if rule is satified for any of the cell Boolean rowCreatedFlag = false; //Boolean IsPreviousCellsFilled = false; // start with the index 1 until 1 less than last index as index 0 and last index cannot have 3 live adjacent cell in any case // This index 0 and last index must be included if rule is changed in future; dead can alive with 2 live adjacent cells for (int j = 1; j < inputGrid.ColumnCount - 1; j++) { if (Rule.CountAliveNeighbours(inputGrid, new CoOrdinates(rowId, j)) == 3) { if (rowCreatedFlag == false) { Row newRow = new Row(); //if (IsPreviousCellsFilled == false) //{ for (int k = 0; k < outputGrid.ColumnCount; k++) { // Fill all cells with false Cell newDeadCell = new Cell(false); newRow.AddCell(newDeadCell); } //IsPreviousCellsFilled = true; //} if (rowId == -1) { outputGrid.InsertRow(0, newRow); } else { outputGrid.AddRow(newRow); } //outputGrid.RowCount += 1; rowCreatedFlag = true; } int XAxis = (rowId == -1) ? 0 : outputGrid.RowCount - 1; outputGrid[XAxis, j].IsAlive = true; } } //for (int j = 1; j < inputGrid.ColumnCount - 1; j++) //{ // if (Rule.CountAliveNeighbours(inputGrid, new CoOrdinates(rowId, j)) == 3) // { // if (rowCreatedFlag == false) // { // Row newRow = new Row(); // //if (IsPreviousCellsFilled == false) // //{ // for (int k = 0; k < outputGrid.ColumnCount; k++) // { // // Fill all cells with false // Cell newDeadCell = new Cell(false); // newRow.AddCell(newDeadCell); // } // //IsPreviousCellsFilled = true; // //} // if (rowId == -1) // { // outputGrid.InsertRow(0, newRow); // } // else // { // outputGrid.AddRow(newRow); // } // rowCreatedFlag = true; // } // int XAxis = (rowId == -1) ? 0 : outputGrid.RowCount - 1; // outputGrid[XAxis, j].IsAlive = true; // } //} }