public void GridRowPositiveTest() { Row row = new Row(); Cell cell1 = new Cell(true); Cell cell2 = new Cell(true); Cell cell3 = new Cell(true); Cell cell4 = new Cell(false); row.AddCell(cell1); row.AddCell(cell2); row.AddCell(cell3); row.InsertCell(2, cell4, 1); Assert.AreEqual(row.Cells[2].IsAlive, false); }
/// <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> /// Loads the Grid with the pattern given as parameter. /// Internally loads the pattern for each row /// </summary> /// <param name="pattern">The pattern based on which the grid is to be formed</param> public void LoadPattern(String pattern) { pattern = pattern.Trim(); //finds pattern for each row char[] splitPattern = "\n".ToCharArray(); String[] rowPatterns = pattern.Split(splitPattern); numberOfRows = rowPatterns.Length; for (int i = 0; i < numberOfRows; i++) { //Initializes the row with the pattern Row row = new Row(rowPatterns[i]); rows.Add(row); } //Links up the previousRow and Last row for each of the rows for (int i = 1; i < numberOfRows; i++) { rows[i].PreviousRow = rows[i - 1]; } for (int i = 0; i < numberOfRows - 1; i++) { rows[i].NextRow = rows[i + 1]; } numberOfColumns = rows[0].GetColumnCount(); }
/// <summary> /// This checks if any additional columns and rows are required for regeneration /// </summary> private void CheckForAdditionalColumnsAndRows() { //If a new row is needed due to the first row, create a new row and //insert the new row at the beginning if (rows[0].IsNewRowNeeded()) { int count = rows[0].GetColumnCount(); Row row = new Row(count); //Set the previous row and current row for the new row rows[0].PreviousRow = row; row.NextRow = rows[0]; //Add the new row AddRowToStart(row); } //If a new row is needed due to the last row, create a new row and //insert the new row at the end if (rows[numberOfRows - 1].IsNewRowNeeded()) { Row row = new Row(numberOfColumns); //Set the previous row and current row for the new row row.PreviousRow = rows[numberOfRows - 1]; rows[numberOfRows - 1].NextRow = row; //Add the new row AddRowToEnd(row); } //If a new column is needed at the left, create a new column to the Left if (IsNewColumnNeededAtLeft()) { AddNewColumnToLeft(); numberOfColumns++; } //If a new column is needed at the right, create a new column to the right if (IsNewColumnNeededAtRight()) { AddNewColumnToRight(); numberOfColumns++; } }
/// <summary> /// Adds the given row at the beginning of the grid, if required in case of regeneration /// </summary> /// <param name="row">Row to be inserted at the beginning</param> private void AddRowToStart(Row row) { List<Row> rowNew = new List<Row>(); rows.Insert(0, row); numberOfRows++; }
/// <summary> /// Adds the given row at the end of the grid, if required in case of regeneration /// </summary> /// <param name="row">Row to be inserted at the end</param> private void AddRowToEnd(Row row) { rows.Add(row); numberOfRows++; }
/// <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); } rowCreatedFlag = true; } int XAxis = (rowId == -1) ? 0 : outputGrid.RowCount - 1; outputGrid[XAxis, j].IsAlive = true; } } }
/// <summary> /// Inserts a new row in the grid at specified index /// </summary> /// <param name="index"></param> /// <param name="row"></param> public void InsertRow(int index, Row row) { if (index < 0 || index >= RowCount) throw new ArgumentOutOfRangeException("Invalid Index value: must be greater than or equal to zero and less than Row count"); GridObj.Insert(index, row); }
/// <summary> /// Add a new row in grid at the end in row list /// </summary> /// <param name="row"></param> public void AddRow(Row row) { GridObj.Add(row); }
/// <summary> /// Setup grid by 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("Grid's 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; }
public void NumberofColumnsEqualsNumberofCells() { var r = new Row(4, 7); Assert.AreEqual(4, r.NumberOfColumns()); }