/// <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> /// 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> /// 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 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; // } //} }
/// <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> /// 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; // } //} }